12cab237bSDimitry 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" 162cab237bSDimitry Andric #include "LiveRangeCalc.h" 172cab237bSDimitry Andric #include "llvm/ADT/ArrayRef.h" 182cab237bSDimitry Andric #include "llvm/ADT/DenseSet.h" 192cab237bSDimitry Andric #include "llvm/ADT/None.h" 202cab237bSDimitry Andric #include "llvm/ADT/STLExtras.h" 212cab237bSDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 222cab237bSDimitry Andric #include "llvm/ADT/SmallVector.h" 233b0f4066SDimitry Andric #include "llvm/ADT/Statistic.h" 242cab237bSDimitry Andric #include "llvm/CodeGen/LiveInterval.h" 252cab237bSDimitry Andric #include "llvm/CodeGen/LiveIntervals.h" 26dff0c46cSDimitry Andric #include "llvm/CodeGen/LiveRangeEdit.h" 272cab237bSDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 283ca95b02SDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" 292754fe60SDimitry Andric #include "llvm/CodeGen/MachineDominators.h" 302cab237bSDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 312cab237bSDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 32e580952dSDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 336122f3e6SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h" 342cab237bSDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 35e580952dSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 362cab237bSDimitry Andric #include "llvm/CodeGen/SlotIndexes.h" 372cab237bSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 382cab237bSDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h" 392cab237bSDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 402cab237bSDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 41139f7f9bSDimitry Andric #include "llvm/CodeGen/VirtRegMap.h" 422cab237bSDimitry Andric #include "llvm/IR/DebugLoc.h" 432cab237bSDimitry Andric #include "llvm/MC/LaneBitmask.h" 442cab237bSDimitry Andric #include "llvm/Support/Allocator.h" 452cab237bSDimitry Andric #include "llvm/Support/BlockFrequency.h" 462cab237bSDimitry Andric #include "llvm/Support/Compiler.h" 47e580952dSDimitry Andric #include "llvm/Support/Debug.h" 482cab237bSDimitry Andric #include "llvm/Support/ErrorHandling.h" 49e580952dSDimitry Andric #include "llvm/Support/raw_ostream.h" 502cab237bSDimitry Andric #include <algorithm> 512cab237bSDimitry Andric #include <cassert> 522cab237bSDimitry Andric #include <iterator> 532cab237bSDimitry Andric #include <limits> 542cab237bSDimitry Andric #include <tuple> 552cab237bSDimitry Andric #include <utility> 56e580952dSDimitry Andric 57e580952dSDimitry Andric using namespace llvm; 58e580952dSDimitry Andric 5991bc56edSDimitry Andric #define DEBUG_TYPE "regalloc" 6091bc56edSDimitry Andric 613b0f4066SDimitry Andric STATISTIC(NumFinished, "Number of splits finished"); 623b0f4066SDimitry Andric STATISTIC(NumSimple, "Number of splits that were simple"); 63bd5abe19SDimitry Andric STATISTIC(NumCopies, "Number of copies inserted for splitting"); 64bd5abe19SDimitry Andric STATISTIC(NumRemats, "Number of rematerialized defs for splitting"); 65bd5abe19SDimitry Andric STATISTIC(NumRepairs, "Number of invalid live ranges repaired"); 66e580952dSDimitry Andric 67e580952dSDimitry Andric //===----------------------------------------------------------------------===// 683ca95b02SDimitry Andric // Last Insert Point Analysis 693ca95b02SDimitry Andric //===----------------------------------------------------------------------===// 703ca95b02SDimitry Andric 713ca95b02SDimitry Andric InsertPointAnalysis::InsertPointAnalysis(const LiveIntervals &lis, 723ca95b02SDimitry Andric unsigned BBNum) 733ca95b02SDimitry Andric : LIS(lis), LastInsertPoint(BBNum) {} 743ca95b02SDimitry Andric 753ca95b02SDimitry Andric SlotIndex 763ca95b02SDimitry Andric InsertPointAnalysis::computeLastInsertPoint(const LiveInterval &CurLI, 773ca95b02SDimitry Andric const MachineBasicBlock &MBB) { 783ca95b02SDimitry Andric unsigned Num = MBB.getNumber(); 793ca95b02SDimitry Andric std::pair<SlotIndex, SlotIndex> &LIP = LastInsertPoint[Num]; 803ca95b02SDimitry Andric SlotIndex MBBEnd = LIS.getMBBEndIdx(&MBB); 813ca95b02SDimitry Andric 82c4394386SDimitry Andric SmallVector<const MachineBasicBlock *, 1> EHPadSuccessors; 833ca95b02SDimitry Andric for (const MachineBasicBlock *SMBB : MBB.successors()) 843ca95b02SDimitry Andric if (SMBB->isEHPad()) 85c4394386SDimitry Andric EHPadSuccessors.push_back(SMBB); 863ca95b02SDimitry Andric 873ca95b02SDimitry Andric // Compute insert points on the first call. The pair is independent of the 883ca95b02SDimitry Andric // current live interval. 893ca95b02SDimitry Andric if (!LIP.first.isValid()) { 903ca95b02SDimitry Andric MachineBasicBlock::const_iterator FirstTerm = MBB.getFirstTerminator(); 913ca95b02SDimitry Andric if (FirstTerm == MBB.end()) 923ca95b02SDimitry Andric LIP.first = MBBEnd; 933ca95b02SDimitry Andric else 943ca95b02SDimitry Andric LIP.first = LIS.getInstructionIndex(*FirstTerm); 953ca95b02SDimitry Andric 963ca95b02SDimitry Andric // If there is a landing pad successor, also find the call instruction. 97c4394386SDimitry Andric if (EHPadSuccessors.empty()) 983ca95b02SDimitry Andric return LIP.first; 993ca95b02SDimitry Andric // There may not be a call instruction (?) in which case we ignore LPad. 1003ca95b02SDimitry Andric LIP.second = LIP.first; 1013ca95b02SDimitry Andric for (MachineBasicBlock::const_iterator I = MBB.end(), E = MBB.begin(); 1023ca95b02SDimitry Andric I != E;) { 1033ca95b02SDimitry Andric --I; 1043ca95b02SDimitry Andric if (I->isCall()) { 1053ca95b02SDimitry Andric LIP.second = LIS.getInstructionIndex(*I); 1063ca95b02SDimitry Andric break; 1073ca95b02SDimitry Andric } 1083ca95b02SDimitry Andric } 1093ca95b02SDimitry Andric } 1103ca95b02SDimitry Andric 1113ca95b02SDimitry Andric // If CurLI is live into a landing pad successor, move the last insert point 1123ca95b02SDimitry Andric // back to the call that may throw. 1133ca95b02SDimitry Andric if (!LIP.second) 1143ca95b02SDimitry Andric return LIP.first; 1153ca95b02SDimitry Andric 116c4394386SDimitry Andric if (none_of(EHPadSuccessors, [&](const MachineBasicBlock *EHPad) { 1173ca95b02SDimitry Andric return LIS.isLiveInToMBB(CurLI, EHPad); 1183ca95b02SDimitry Andric })) 1193ca95b02SDimitry Andric return LIP.first; 1203ca95b02SDimitry Andric 1213ca95b02SDimitry Andric // Find the value leaving MBB. 1223ca95b02SDimitry Andric const VNInfo *VNI = CurLI.getVNInfoBefore(MBBEnd); 1233ca95b02SDimitry Andric if (!VNI) 1243ca95b02SDimitry Andric return LIP.first; 1253ca95b02SDimitry Andric 1263ca95b02SDimitry Andric // If the value leaving MBB was defined after the call in MBB, it can't 1273ca95b02SDimitry Andric // really be live-in to the landing pad. This can happen if the landing pad 1283ca95b02SDimitry Andric // has a PHI, and this register is undef on the exceptional edge. 1293ca95b02SDimitry Andric // <rdar://problem/10664933> 1303ca95b02SDimitry Andric if (!SlotIndex::isEarlierInstr(VNI->def, LIP.second) && VNI->def < MBBEnd) 1313ca95b02SDimitry Andric return LIP.first; 1323ca95b02SDimitry Andric 1333ca95b02SDimitry Andric // Value is properly live-in to the landing pad. 1343ca95b02SDimitry Andric // Only allow inserts before the call. 1353ca95b02SDimitry Andric return LIP.second; 1363ca95b02SDimitry Andric } 1373ca95b02SDimitry Andric 1383ca95b02SDimitry Andric MachineBasicBlock::iterator 1393ca95b02SDimitry Andric InsertPointAnalysis::getLastInsertPointIter(const LiveInterval &CurLI, 1403ca95b02SDimitry Andric MachineBasicBlock &MBB) { 1413ca95b02SDimitry Andric SlotIndex LIP = getLastInsertPoint(CurLI, MBB); 1423ca95b02SDimitry Andric if (LIP == LIS.getMBBEndIdx(&MBB)) 1433ca95b02SDimitry Andric return MBB.end(); 1443ca95b02SDimitry Andric return LIS.getInstructionFromIndex(LIP); 1453ca95b02SDimitry Andric } 1463ca95b02SDimitry Andric 1473ca95b02SDimitry Andric //===----------------------------------------------------------------------===// 148e580952dSDimitry Andric // Split Analysis 149e580952dSDimitry Andric //===----------------------------------------------------------------------===// 150e580952dSDimitry Andric 15139d628a0SDimitry Andric SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis, 152e580952dSDimitry Andric const MachineLoopInfo &mli) 15339d628a0SDimitry Andric : MF(vrm.getMachineFunction()), VRM(vrm), LIS(lis), Loops(mli), 1542cab237bSDimitry Andric TII(*MF.getSubtarget().getInstrInfo()), IPA(lis, MF.getNumBlockIDs()) {} 155e580952dSDimitry Andric 156e580952dSDimitry Andric void SplitAnalysis::clear() { 1572754fe60SDimitry Andric UseSlots.clear(); 1583b0f4066SDimitry Andric UseBlocks.clear(); 1593b0f4066SDimitry Andric ThroughBlocks.clear(); 16091bc56edSDimitry Andric CurLI = nullptr; 161bd5abe19SDimitry Andric DidRepairRange = false; 162e580952dSDimitry Andric } 163e580952dSDimitry Andric 1642754fe60SDimitry Andric /// analyzeUses - Count instructions, basic blocks, and loops using CurLI. 165e580952dSDimitry Andric void SplitAnalysis::analyzeUses() { 1663b0f4066SDimitry Andric assert(UseSlots.empty() && "Call clear first"); 1673b0f4066SDimitry Andric 1683b0f4066SDimitry Andric // First get all the defs from the interval values. This provides the correct 1693b0f4066SDimitry Andric // slots for early clobbers. 17039d628a0SDimitry Andric for (const VNInfo *VNI : CurLI->valnos) 17139d628a0SDimitry Andric if (!VNI->isPHIDef() && !VNI->isUnused()) 17239d628a0SDimitry Andric UseSlots.push_back(VNI->def); 1733b0f4066SDimitry Andric 1743b0f4066SDimitry Andric // Get use slots form the use-def chain. 1752754fe60SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 17691bc56edSDimitry Andric for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg)) 17791bc56edSDimitry Andric if (!MO.isUndef()) 1783ca95b02SDimitry Andric UseSlots.push_back(LIS.getInstructionIndex(*MO.getParent()).getRegSlot()); 1793b0f4066SDimitry Andric 1802754fe60SDimitry Andric array_pod_sort(UseSlots.begin(), UseSlots.end()); 1813b0f4066SDimitry Andric 1823b0f4066SDimitry Andric // Remove duplicates, keeping the smaller slot for each instruction. 1833b0f4066SDimitry Andric // That is what we want for early clobbers. 1843b0f4066SDimitry Andric UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(), 1853b0f4066SDimitry Andric SlotIndex::isSameInstr), 1863b0f4066SDimitry Andric UseSlots.end()); 1873b0f4066SDimitry Andric 1883b0f4066SDimitry Andric // Compute per-live block info. 1893b0f4066SDimitry Andric if (!calcLiveBlockInfo()) { 1903b0f4066SDimitry Andric // FIXME: calcLiveBlockInfo found inconsistencies in the live range. 19117a519f9SDimitry Andric // I am looking at you, RegisterCoalescer! 192bd5abe19SDimitry Andric DidRepairRange = true; 193bd5abe19SDimitry Andric ++NumRepairs; 1943b0f4066SDimitry Andric DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n"); 1953b0f4066SDimitry Andric const_cast<LiveIntervals&>(LIS) 1963b0f4066SDimitry Andric .shrinkToUses(const_cast<LiveInterval*>(CurLI)); 1973b0f4066SDimitry Andric UseBlocks.clear(); 1983b0f4066SDimitry Andric ThroughBlocks.clear(); 1993b0f4066SDimitry Andric bool fixed = calcLiveBlockInfo(); 2003b0f4066SDimitry Andric (void)fixed; 2013b0f4066SDimitry Andric assert(fixed && "Couldn't fix broken live interval"); 2023b0f4066SDimitry Andric } 2033b0f4066SDimitry Andric 2043b0f4066SDimitry Andric DEBUG(dbgs() << "Analyze counted " 2053b0f4066SDimitry Andric << UseSlots.size() << " instrs in " 2063b0f4066SDimitry Andric << UseBlocks.size() << " blocks, through " 2073b0f4066SDimitry Andric << NumThroughBlocks << " blocks.\n"); 208e580952dSDimitry Andric } 209e580952dSDimitry Andric 2102754fe60SDimitry Andric /// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks 2112754fe60SDimitry Andric /// where CurLI is live. 2123b0f4066SDimitry Andric bool SplitAnalysis::calcLiveBlockInfo() { 2133b0f4066SDimitry Andric ThroughBlocks.resize(MF.getNumBlockIDs()); 214bd5abe19SDimitry Andric NumThroughBlocks = NumGapBlocks = 0; 2152754fe60SDimitry Andric if (CurLI->empty()) 2163b0f4066SDimitry Andric return true; 217e580952dSDimitry Andric 2182754fe60SDimitry Andric LiveInterval::const_iterator LVI = CurLI->begin(); 2192754fe60SDimitry Andric LiveInterval::const_iterator LVE = CurLI->end(); 220e580952dSDimitry Andric 2212754fe60SDimitry Andric SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE; 2222754fe60SDimitry Andric UseI = UseSlots.begin(); 2232754fe60SDimitry Andric UseE = UseSlots.end(); 2242754fe60SDimitry Andric 2252754fe60SDimitry Andric // Loop over basic blocks where CurLI is live. 2267d523365SDimitry Andric MachineFunction::iterator MFI = 2277d523365SDimitry Andric LIS.getMBBFromIndex(LVI->start)->getIterator(); 2282cab237bSDimitry Andric while (true) { 2292754fe60SDimitry Andric BlockInfo BI; 2307d523365SDimitry Andric BI.MBB = &*MFI; 2312754fe60SDimitry Andric SlotIndex Start, Stop; 23291bc56edSDimitry Andric std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB); 2332754fe60SDimitry Andric 234bd5abe19SDimitry Andric // If the block contains no uses, the range must be live through. At one 23517a519f9SDimitry Andric // point, RegisterCoalescer could create dangling ranges that ended 236bd5abe19SDimitry Andric // mid-block. 237bd5abe19SDimitry Andric if (UseI == UseE || *UseI >= Stop) { 238bd5abe19SDimitry Andric ++NumThroughBlocks; 239bd5abe19SDimitry Andric ThroughBlocks.set(BI.MBB->getNumber()); 240bd5abe19SDimitry Andric // The range shouldn't end mid-block if there are no uses. This shouldn't 241bd5abe19SDimitry Andric // happen. 242bd5abe19SDimitry Andric if (LVI->end < Stop) 243bd5abe19SDimitry Andric return false; 244bd5abe19SDimitry Andric } else { 245bd5abe19SDimitry Andric // This block has uses. Find the first and last uses in the block. 2466122f3e6SDimitry Andric BI.FirstInstr = *UseI; 2476122f3e6SDimitry Andric assert(BI.FirstInstr >= Start); 2482754fe60SDimitry Andric do ++UseI; 2492754fe60SDimitry Andric while (UseI != UseE && *UseI < Stop); 2506122f3e6SDimitry Andric BI.LastInstr = UseI[-1]; 2516122f3e6SDimitry Andric assert(BI.LastInstr < Stop); 252bd5abe19SDimitry Andric 253bd5abe19SDimitry Andric // LVI is the first live segment overlapping MBB. 254bd5abe19SDimitry Andric BI.LiveIn = LVI->start <= Start; 255e580952dSDimitry Andric 2566122f3e6SDimitry Andric // When not live in, the first use should be a def. 2576122f3e6SDimitry Andric if (!BI.LiveIn) { 258f785676fSDimitry Andric assert(LVI->start == LVI->valno->def && "Dangling Segment start"); 2596122f3e6SDimitry Andric assert(LVI->start == BI.FirstInstr && "First instr should be a def"); 2606122f3e6SDimitry Andric BI.FirstDef = BI.FirstInstr; 2616122f3e6SDimitry Andric } 2626122f3e6SDimitry Andric 2632754fe60SDimitry Andric // Look for gaps in the live range. 2642754fe60SDimitry Andric BI.LiveOut = true; 2652754fe60SDimitry Andric while (LVI->end < Stop) { 2662754fe60SDimitry Andric SlotIndex LastStop = LVI->end; 2672754fe60SDimitry Andric if (++LVI == LVE || LVI->start >= Stop) { 2682754fe60SDimitry Andric BI.LiveOut = false; 2696122f3e6SDimitry Andric BI.LastInstr = LastStop; 270e580952dSDimitry Andric break; 271e580952dSDimitry Andric } 2726122f3e6SDimitry Andric 2732754fe60SDimitry Andric if (LastStop < LVI->start) { 274bd5abe19SDimitry Andric // There is a gap in the live range. Create duplicate entries for the 275bd5abe19SDimitry Andric // live-in snippet and the live-out snippet. 276bd5abe19SDimitry Andric ++NumGapBlocks; 277bd5abe19SDimitry Andric 278bd5abe19SDimitry Andric // Push the Live-in part. 279bd5abe19SDimitry Andric BI.LiveOut = false; 280bd5abe19SDimitry Andric UseBlocks.push_back(BI); 2816122f3e6SDimitry Andric UseBlocks.back().LastInstr = LastStop; 282bd5abe19SDimitry Andric 283bd5abe19SDimitry Andric // Set up BI for the live-out part. 284bd5abe19SDimitry Andric BI.LiveIn = false; 285bd5abe19SDimitry Andric BI.LiveOut = true; 2866122f3e6SDimitry Andric BI.FirstInstr = BI.FirstDef = LVI->start; 287e580952dSDimitry Andric } 288e580952dSDimitry Andric 289f785676fSDimitry Andric // A Segment that starts in the middle of the block must be a def. 290f785676fSDimitry Andric assert(LVI->start == LVI->valno->def && "Dangling Segment start"); 2916122f3e6SDimitry Andric if (!BI.FirstDef) 2926122f3e6SDimitry Andric BI.FirstDef = LVI->start; 2936122f3e6SDimitry Andric } 2946122f3e6SDimitry Andric 2953b0f4066SDimitry Andric UseBlocks.push_back(BI); 296e580952dSDimitry Andric 2972754fe60SDimitry Andric // LVI is now at LVE or LVI->end >= Stop. 2982754fe60SDimitry Andric if (LVI == LVE) 2992754fe60SDimitry Andric break; 300bd5abe19SDimitry Andric } 3012754fe60SDimitry Andric 3022754fe60SDimitry Andric // Live segment ends exactly at Stop. Move to the next segment. 3032754fe60SDimitry Andric if (LVI->end == Stop && ++LVI == LVE) 3042754fe60SDimitry Andric break; 3052754fe60SDimitry Andric 3062754fe60SDimitry Andric // Pick the next basic block. 3072754fe60SDimitry Andric if (LVI->start < Stop) 3082754fe60SDimitry Andric ++MFI; 3092754fe60SDimitry Andric else 3107d523365SDimitry Andric MFI = LIS.getMBBFromIndex(LVI->start)->getIterator(); 3112754fe60SDimitry Andric } 312bd5abe19SDimitry Andric 313bd5abe19SDimitry Andric assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count"); 3143b0f4066SDimitry Andric return true; 3153b0f4066SDimitry Andric } 3163b0f4066SDimitry Andric 3173b0f4066SDimitry Andric unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const { 3183b0f4066SDimitry Andric if (cli->empty()) 3193b0f4066SDimitry Andric return 0; 3203b0f4066SDimitry Andric LiveInterval *li = const_cast<LiveInterval*>(cli); 3213b0f4066SDimitry Andric LiveInterval::iterator LVI = li->begin(); 3223b0f4066SDimitry Andric LiveInterval::iterator LVE = li->end(); 3233b0f4066SDimitry Andric unsigned Count = 0; 3243b0f4066SDimitry Andric 3253b0f4066SDimitry Andric // Loop over basic blocks where li is live. 3267d523365SDimitry Andric MachineFunction::const_iterator MFI = 3277d523365SDimitry Andric LIS.getMBBFromIndex(LVI->start)->getIterator(); 3287d523365SDimitry Andric SlotIndex Stop = LIS.getMBBEndIdx(&*MFI); 3292cab237bSDimitry Andric while (true) { 3303b0f4066SDimitry Andric ++Count; 3313b0f4066SDimitry Andric LVI = li->advanceTo(LVI, Stop); 3323b0f4066SDimitry Andric if (LVI == LVE) 3333b0f4066SDimitry Andric return Count; 3343b0f4066SDimitry Andric do { 3353b0f4066SDimitry Andric ++MFI; 3367d523365SDimitry Andric Stop = LIS.getMBBEndIdx(&*MFI); 3373b0f4066SDimitry Andric } while (Stop <= LVI->start); 3383b0f4066SDimitry Andric } 339e580952dSDimitry Andric } 340e580952dSDimitry Andric 341dd6029ffSDimitry Andric bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const { 342dd6029ffSDimitry Andric unsigned OrigReg = VRM.getOriginal(CurLI->reg); 343dd6029ffSDimitry Andric const LiveInterval &Orig = LIS.getInterval(OrigReg); 344dd6029ffSDimitry Andric assert(!Orig.empty() && "Splitting empty interval?"); 345dd6029ffSDimitry Andric LiveInterval::const_iterator I = Orig.find(Idx); 346dd6029ffSDimitry Andric 347dd6029ffSDimitry Andric // Range containing Idx should begin at Idx. 348dd6029ffSDimitry Andric if (I != Orig.end() && I->start <= Idx) 349dd6029ffSDimitry Andric return I->start == Idx; 350dd6029ffSDimitry Andric 351dd6029ffSDimitry Andric // Range does not contain Idx, previous must end at Idx. 352dd6029ffSDimitry Andric return I != Orig.begin() && (--I)->end == Idx; 353dd6029ffSDimitry Andric } 354dd6029ffSDimitry Andric 355e580952dSDimitry Andric void SplitAnalysis::analyze(const LiveInterval *li) { 356e580952dSDimitry Andric clear(); 3572754fe60SDimitry Andric CurLI = li; 358e580952dSDimitry Andric analyzeUses(); 359e580952dSDimitry Andric } 360e580952dSDimitry Andric 361e580952dSDimitry Andric //===----------------------------------------------------------------------===// 3623b0f4066SDimitry Andric // Split Editor 363e580952dSDimitry Andric //===----------------------------------------------------------------------===// 364e580952dSDimitry Andric 3653b0f4066SDimitry Andric /// Create a new SplitEditor for editing the LiveInterval analyzed by SA. 3663ca95b02SDimitry Andric SplitEditor::SplitEditor(SplitAnalysis &sa, AliasAnalysis &aa, 3673ca95b02SDimitry Andric LiveIntervals &lis, VirtRegMap &vrm, 368f785676fSDimitry Andric MachineDominatorTree &mdt, 369f785676fSDimitry Andric MachineBlockFrequencyInfo &mbfi) 3703ca95b02SDimitry Andric : SA(sa), AA(aa), LIS(lis), VRM(vrm), 3713ca95b02SDimitry Andric MRI(vrm.getMachineFunction().getRegInfo()), MDT(mdt), 3723ca95b02SDimitry Andric TII(*vrm.getMachineFunction().getSubtarget().getInstrInfo()), 37339d628a0SDimitry Andric TRI(*vrm.getMachineFunction().getSubtarget().getRegisterInfo()), 3742cab237bSDimitry Andric MBFI(mbfi), RegAssign(Allocator) {} 375e580952dSDimitry Andric 3766122f3e6SDimitry Andric void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) { 3776122f3e6SDimitry Andric Edit = &LRE; 3786122f3e6SDimitry Andric SpillMode = SM; 3793b0f4066SDimitry Andric OpenIdx = 0; 3803b0f4066SDimitry Andric RegAssign.clear(); 3812754fe60SDimitry Andric Values.clear(); 3823b0f4066SDimitry Andric 3836122f3e6SDimitry Andric // Reset the LiveRangeCalc instances needed for this spill mode. 3847ae0e2c9SDimitry Andric LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT, 3857ae0e2c9SDimitry Andric &LIS.getVNInfoAllocator()); 3866122f3e6SDimitry Andric if (SpillMode) 3877ae0e2c9SDimitry Andric LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT, 3887ae0e2c9SDimitry Andric &LIS.getVNInfoAllocator()); 3893b0f4066SDimitry Andric 3903b0f4066SDimitry Andric // We don't need an AliasAnalysis since we will only be performing 3913b0f4066SDimitry Andric // cheap-as-a-copy remats anyway. 39291bc56edSDimitry Andric Edit->anyRematerializable(nullptr); 3932754fe60SDimitry Andric } 3942754fe60SDimitry Andric 3953861d79fSDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 3963ca95b02SDimitry Andric LLVM_DUMP_METHOD void SplitEditor::dump() const { 3973b0f4066SDimitry Andric if (RegAssign.empty()) { 3983b0f4066SDimitry Andric dbgs() << " empty\n"; 3993b0f4066SDimitry Andric return; 4002754fe60SDimitry Andric } 4012754fe60SDimitry Andric 4023b0f4066SDimitry Andric for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I) 4033b0f4066SDimitry Andric dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value(); 4043b0f4066SDimitry Andric dbgs() << '\n'; 4053b0f4066SDimitry Andric } 4063861d79fSDimitry Andric #endif 4073b0f4066SDimitry Andric 408d88c1a5aSDimitry Andric LiveInterval::SubRange &SplitEditor::getSubRangeForMask(LaneBitmask LM, 409d88c1a5aSDimitry Andric LiveInterval &LI) { 410d88c1a5aSDimitry Andric for (LiveInterval::SubRange &S : LI.subranges()) 411d88c1a5aSDimitry Andric if (S.LaneMask == LM) 412d88c1a5aSDimitry Andric return S; 413d88c1a5aSDimitry Andric llvm_unreachable("SubRange for this mask not found"); 414d88c1a5aSDimitry Andric } 415d88c1a5aSDimitry Andric 416d88c1a5aSDimitry Andric void SplitEditor::addDeadDef(LiveInterval &LI, VNInfo *VNI, bool Original) { 417d88c1a5aSDimitry Andric if (!LI.hasSubRanges()) { 418d88c1a5aSDimitry Andric LI.createDeadDef(VNI); 419d88c1a5aSDimitry Andric return; 420d88c1a5aSDimitry Andric } 421d88c1a5aSDimitry Andric 422d88c1a5aSDimitry Andric SlotIndex Def = VNI->def; 423d88c1a5aSDimitry Andric if (Original) { 424d88c1a5aSDimitry Andric // If we are transferring a def from the original interval, make sure 425d88c1a5aSDimitry Andric // to only update the subranges for which the original subranges had 426d88c1a5aSDimitry Andric // a def at this location. 427d88c1a5aSDimitry Andric for (LiveInterval::SubRange &S : LI.subranges()) { 428d88c1a5aSDimitry Andric auto &PS = getSubRangeForMask(S.LaneMask, Edit->getParent()); 429d88c1a5aSDimitry Andric VNInfo *PV = PS.getVNInfoAt(Def); 430d88c1a5aSDimitry Andric if (PV != nullptr && PV->def == Def) 431d88c1a5aSDimitry Andric S.createDeadDef(Def, LIS.getVNInfoAllocator()); 432d88c1a5aSDimitry Andric } 433d88c1a5aSDimitry Andric } else { 434d88c1a5aSDimitry Andric // This is a new def: either from rematerialization, or from an inserted 435d88c1a5aSDimitry Andric // copy. Since rematerialization can regenerate a definition of a sub- 436d88c1a5aSDimitry Andric // register, we need to check which subranges need to be updated. 437d88c1a5aSDimitry Andric const MachineInstr *DefMI = LIS.getInstructionFromIndex(Def); 438d88c1a5aSDimitry Andric assert(DefMI != nullptr); 439d88c1a5aSDimitry Andric LaneBitmask LM; 440d88c1a5aSDimitry Andric for (const MachineOperand &DefOp : DefMI->defs()) { 441d88c1a5aSDimitry Andric unsigned R = DefOp.getReg(); 442d88c1a5aSDimitry Andric if (R != LI.reg) 443d88c1a5aSDimitry Andric continue; 444d88c1a5aSDimitry Andric if (unsigned SR = DefOp.getSubReg()) 445d88c1a5aSDimitry Andric LM |= TRI.getSubRegIndexLaneMask(SR); 446d88c1a5aSDimitry Andric else { 447d88c1a5aSDimitry Andric LM = MRI.getMaxLaneMaskForVReg(R); 448d88c1a5aSDimitry Andric break; 449d88c1a5aSDimitry Andric } 450d88c1a5aSDimitry Andric } 451d88c1a5aSDimitry Andric for (LiveInterval::SubRange &S : LI.subranges()) 452d88c1a5aSDimitry Andric if ((S.LaneMask & LM).any()) 453d88c1a5aSDimitry Andric S.createDeadDef(Def, LIS.getVNInfoAllocator()); 454d88c1a5aSDimitry Andric } 455d88c1a5aSDimitry Andric } 456d88c1a5aSDimitry Andric 4573b0f4066SDimitry Andric VNInfo *SplitEditor::defValue(unsigned RegIdx, 4583b0f4066SDimitry Andric const VNInfo *ParentVNI, 459d88c1a5aSDimitry Andric SlotIndex Idx, 460d88c1a5aSDimitry Andric bool Original) { 461e580952dSDimitry Andric assert(ParentVNI && "Mapping NULL value"); 462e580952dSDimitry Andric assert(Idx.isValid() && "Invalid SlotIndex"); 4633b0f4066SDimitry Andric assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI"); 464f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx)); 4652754fe60SDimitry Andric 4662754fe60SDimitry Andric // Create a new value. 467dff0c46cSDimitry Andric VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator()); 4682754fe60SDimitry Andric 469d88c1a5aSDimitry Andric bool Force = LI->hasSubRanges(); 470d88c1a5aSDimitry Andric ValueForcePair FP(Force ? nullptr : VNI, Force); 471e580952dSDimitry Andric // Use insert for lookup, so we can add missing values with a second lookup. 472e580952dSDimitry Andric std::pair<ValueMap::iterator, bool> InsP = 473d88c1a5aSDimitry Andric Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), FP)); 4742754fe60SDimitry Andric 475d88c1a5aSDimitry Andric // This was the first time (RegIdx, ParentVNI) was mapped, and it is not 476d88c1a5aSDimitry Andric // forced. Keep it as a simple def without any liveness. 477d88c1a5aSDimitry Andric if (!Force && InsP.second) 4783b0f4066SDimitry Andric return VNI; 4793b0f4066SDimitry Andric 4803b0f4066SDimitry Andric // If the previous value was a simple mapping, add liveness for it now. 4816122f3e6SDimitry Andric if (VNInfo *OldVNI = InsP.first->second.getPointer()) { 482d88c1a5aSDimitry Andric addDeadDef(*LI, OldVNI, Original); 483d88c1a5aSDimitry Andric 484d88c1a5aSDimitry Andric // No longer a simple mapping. Switch to a complex mapping. If the 485d88c1a5aSDimitry Andric // interval has subranges, make it a forced mapping. 486d88c1a5aSDimitry Andric InsP.first->second = ValueForcePair(nullptr, Force); 4873b0f4066SDimitry Andric } 4883b0f4066SDimitry Andric 4893b0f4066SDimitry Andric // This is a complex mapping, add liveness for VNI 490d88c1a5aSDimitry Andric addDeadDef(*LI, VNI, Original); 4912754fe60SDimitry Andric return VNI; 4922754fe60SDimitry Andric } 4932754fe60SDimitry Andric 4946122f3e6SDimitry Andric void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) { 4952754fe60SDimitry Andric assert(ParentVNI && "Mapping NULL value"); 4966122f3e6SDimitry Andric ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)]; 4976122f3e6SDimitry Andric VNInfo *VNI = VFP.getPointer(); 4983b0f4066SDimitry Andric 4996122f3e6SDimitry Andric // ParentVNI was either unmapped or already complex mapped. Either way, just 5006122f3e6SDimitry Andric // set the force bit. 5016122f3e6SDimitry Andric if (!VNI) { 5026122f3e6SDimitry Andric VFP.setInt(true); 5033b0f4066SDimitry Andric return; 5046122f3e6SDimitry Andric } 5053b0f4066SDimitry Andric 5063b0f4066SDimitry Andric // This was previously a single mapping. Make sure the old def is represented 5073b0f4066SDimitry Andric // by a trivial live range. 508d88c1a5aSDimitry Andric addDeadDef(LIS.getInterval(Edit->get(RegIdx)), VNI, false); 509d88c1a5aSDimitry Andric 5106122f3e6SDimitry Andric // Mark as complex mapped, forced. 51191bc56edSDimitry Andric VFP = ValueForcePair(nullptr, true); 512e580952dSDimitry Andric } 513e580952dSDimitry Andric 5147a7e6055SDimitry Andric SlotIndex SplitEditor::buildSingleSubRegCopy(unsigned FromReg, unsigned ToReg, 5157a7e6055SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore, 5167a7e6055SDimitry Andric unsigned SubIdx, LiveInterval &DestLI, bool Late, SlotIndex Def) { 5177a7e6055SDimitry Andric const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY); 5187a7e6055SDimitry Andric bool FirstCopy = !Def.isValid(); 5197a7e6055SDimitry Andric MachineInstr *CopyMI = BuildMI(MBB, InsertBefore, DebugLoc(), Desc) 5207a7e6055SDimitry Andric .addReg(ToReg, RegState::Define | getUndefRegState(FirstCopy) 5217a7e6055SDimitry Andric | getInternalReadRegState(!FirstCopy), SubIdx) 5227a7e6055SDimitry Andric .addReg(FromReg, 0, SubIdx); 5237a7e6055SDimitry Andric 5247a7e6055SDimitry Andric BumpPtrAllocator &Allocator = LIS.getVNInfoAllocator(); 5257a7e6055SDimitry Andric if (FirstCopy) { 5267a7e6055SDimitry Andric SlotIndexes &Indexes = *LIS.getSlotIndexes(); 5277a7e6055SDimitry Andric Def = Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot(); 5287a7e6055SDimitry Andric } else { 5297a7e6055SDimitry Andric CopyMI->bundleWithPred(); 5307a7e6055SDimitry Andric } 5317a7e6055SDimitry Andric LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubIdx); 5327a7e6055SDimitry Andric DestLI.refineSubRanges(Allocator, LaneMask, 5337a7e6055SDimitry Andric [Def, &Allocator](LiveInterval::SubRange& SR) { 5347a7e6055SDimitry Andric SR.createDeadDef(Def, Allocator); 5357a7e6055SDimitry Andric }); 5367a7e6055SDimitry Andric return Def; 5377a7e6055SDimitry Andric } 5387a7e6055SDimitry Andric 5397a7e6055SDimitry Andric SlotIndex SplitEditor::buildCopy(unsigned FromReg, unsigned ToReg, 5407a7e6055SDimitry Andric LaneBitmask LaneMask, MachineBasicBlock &MBB, 5417a7e6055SDimitry Andric MachineBasicBlock::iterator InsertBefore, bool Late, unsigned RegIdx) { 5427a7e6055SDimitry Andric const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY); 5437a7e6055SDimitry Andric if (LaneMask.all() || LaneMask == MRI.getMaxLaneMaskForVReg(FromReg)) { 5447a7e6055SDimitry Andric // The full vreg is copied. 5457a7e6055SDimitry Andric MachineInstr *CopyMI = 5467a7e6055SDimitry Andric BuildMI(MBB, InsertBefore, DebugLoc(), Desc, ToReg).addReg(FromReg); 5477a7e6055SDimitry Andric SlotIndexes &Indexes = *LIS.getSlotIndexes(); 5487a7e6055SDimitry Andric return Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot(); 5497a7e6055SDimitry Andric } 5507a7e6055SDimitry Andric 5517a7e6055SDimitry Andric // Only a subset of lanes needs to be copied. The following is a simple 5527a7e6055SDimitry Andric // heuristic to construct a sequence of COPYs. We could add a target 5537a7e6055SDimitry Andric // specific callback if this turns out to be suboptimal. 5547a7e6055SDimitry Andric LiveInterval &DestLI = LIS.getInterval(Edit->get(RegIdx)); 5557a7e6055SDimitry Andric 5567a7e6055SDimitry Andric // First pass: Try to find a perfectly matching subregister index. If none 5577a7e6055SDimitry Andric // exists find the one covering the most lanemask bits. 5587a7e6055SDimitry Andric SmallVector<unsigned, 8> PossibleIndexes; 5597a7e6055SDimitry Andric unsigned BestIdx = 0; 5607a7e6055SDimitry Andric unsigned BestCover = 0; 5617a7e6055SDimitry Andric const TargetRegisterClass *RC = MRI.getRegClass(FromReg); 5627a7e6055SDimitry Andric assert(RC == MRI.getRegClass(ToReg) && "Should have same reg class"); 5637a7e6055SDimitry Andric for (unsigned Idx = 1, E = TRI.getNumSubRegIndices(); Idx < E; ++Idx) { 5647a7e6055SDimitry Andric // Is this index even compatible with the given class? 5657a7e6055SDimitry Andric if (TRI.getSubClassWithSubReg(RC, Idx) != RC) 5667a7e6055SDimitry Andric continue; 5677a7e6055SDimitry Andric LaneBitmask SubRegMask = TRI.getSubRegIndexLaneMask(Idx); 5687a7e6055SDimitry Andric // Early exit if we found a perfect match. 5697a7e6055SDimitry Andric if (SubRegMask == LaneMask) { 5707a7e6055SDimitry Andric BestIdx = Idx; 5717a7e6055SDimitry Andric break; 5727a7e6055SDimitry Andric } 5737a7e6055SDimitry Andric 5747a7e6055SDimitry Andric // The index must not cover any lanes outside \p LaneMask. 5757a7e6055SDimitry Andric if ((SubRegMask & ~LaneMask).any()) 5767a7e6055SDimitry Andric continue; 5777a7e6055SDimitry Andric 5782cab237bSDimitry Andric unsigned PopCount = SubRegMask.getNumLanes(); 5797a7e6055SDimitry Andric PossibleIndexes.push_back(Idx); 5807a7e6055SDimitry Andric if (PopCount > BestCover) { 5817a7e6055SDimitry Andric BestCover = PopCount; 5827a7e6055SDimitry Andric BestIdx = Idx; 5837a7e6055SDimitry Andric } 5847a7e6055SDimitry Andric } 5857a7e6055SDimitry Andric 5867a7e6055SDimitry Andric // Abort if we cannot possibly implement the COPY with the given indexes. 5877a7e6055SDimitry Andric if (BestIdx == 0) 5887a7e6055SDimitry Andric report_fatal_error("Impossible to implement partial COPY"); 5897a7e6055SDimitry Andric 5907a7e6055SDimitry Andric SlotIndex Def = buildSingleSubRegCopy(FromReg, ToReg, MBB, InsertBefore, 5917a7e6055SDimitry Andric BestIdx, DestLI, Late, SlotIndex()); 5927a7e6055SDimitry Andric 5937a7e6055SDimitry Andric // Greedy heuristic: Keep iterating keeping the best covering subreg index 5947a7e6055SDimitry Andric // each time. 59524d58133SDimitry Andric LaneBitmask LanesLeft = LaneMask & ~(TRI.getSubRegIndexLaneMask(BestIdx)); 5967a7e6055SDimitry Andric while (LanesLeft.any()) { 5977a7e6055SDimitry Andric unsigned BestIdx = 0; 5982cab237bSDimitry Andric int BestCover = std::numeric_limits<int>::min(); 5997a7e6055SDimitry Andric for (unsigned Idx : PossibleIndexes) { 6007a7e6055SDimitry Andric LaneBitmask SubRegMask = TRI.getSubRegIndexLaneMask(Idx); 6017a7e6055SDimitry Andric // Early exit if we found a perfect match. 6027a7e6055SDimitry Andric if (SubRegMask == LanesLeft) { 6037a7e6055SDimitry Andric BestIdx = Idx; 6047a7e6055SDimitry Andric break; 6057a7e6055SDimitry Andric } 6067a7e6055SDimitry Andric 6077a7e6055SDimitry Andric // Try to cover as much of the remaining lanes as possible but 6087a7e6055SDimitry Andric // as few of the already covered lanes as possible. 6092cab237bSDimitry Andric int Cover = (SubRegMask & LanesLeft).getNumLanes() 6102cab237bSDimitry Andric - (SubRegMask & ~LanesLeft).getNumLanes(); 6117a7e6055SDimitry Andric if (Cover > BestCover) { 6127a7e6055SDimitry Andric BestCover = Cover; 6137a7e6055SDimitry Andric BestIdx = Idx; 6147a7e6055SDimitry Andric } 6157a7e6055SDimitry Andric } 6167a7e6055SDimitry Andric 6177a7e6055SDimitry Andric if (BestIdx == 0) 6187a7e6055SDimitry Andric report_fatal_error("Impossible to implement partial COPY"); 6197a7e6055SDimitry Andric 6207a7e6055SDimitry Andric buildSingleSubRegCopy(FromReg, ToReg, MBB, InsertBefore, BestIdx, 6217a7e6055SDimitry Andric DestLI, Late, Def); 6227a7e6055SDimitry Andric LanesLeft &= ~TRI.getSubRegIndexLaneMask(BestIdx); 6237a7e6055SDimitry Andric } 6247a7e6055SDimitry Andric 6257a7e6055SDimitry Andric return Def; 6267a7e6055SDimitry Andric } 6277a7e6055SDimitry Andric 6282754fe60SDimitry Andric VNInfo *SplitEditor::defFromParent(unsigned RegIdx, 6292754fe60SDimitry Andric VNInfo *ParentVNI, 6302754fe60SDimitry Andric SlotIndex UseIdx, 631e580952dSDimitry Andric MachineBasicBlock &MBB, 632e580952dSDimitry Andric MachineBasicBlock::iterator I) { 6332754fe60SDimitry Andric SlotIndex Def; 634f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx)); 6353b0f4066SDimitry Andric 6363b0f4066SDimitry Andric // We may be trying to avoid interference that ends at a deleted instruction, 6373b0f4066SDimitry Andric // so always begin RegIdx 0 early and all others late. 6383b0f4066SDimitry Andric bool Late = RegIdx != 0; 6392754fe60SDimitry Andric 6402754fe60SDimitry Andric // Attempt cheap-as-a-copy rematerialization. 6413ca95b02SDimitry Andric unsigned Original = VRM.getOriginal(Edit->get(RegIdx)); 6423ca95b02SDimitry Andric LiveInterval &OrigLI = LIS.getInterval(Original); 6433ca95b02SDimitry Andric VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx); 644d88c1a5aSDimitry Andric 6457a7e6055SDimitry Andric unsigned Reg = LI->reg; 646d88c1a5aSDimitry Andric bool DidRemat = false; 647d88c1a5aSDimitry Andric if (OrigVNI) { 6482754fe60SDimitry Andric LiveRangeEdit::Remat RM(ParentVNI); 6493ca95b02SDimitry Andric RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def); 6503ca95b02SDimitry Andric if (Edit->canRematerializeAt(RM, OrigVNI, UseIdx, true)) { 6517a7e6055SDimitry Andric Def = Edit->rematerializeAt(MBB, I, Reg, RM, TRI, Late); 652bd5abe19SDimitry Andric ++NumRemats; 653d88c1a5aSDimitry Andric DidRemat = true; 654d88c1a5aSDimitry Andric } 655d88c1a5aSDimitry Andric } 656d88c1a5aSDimitry Andric if (!DidRemat) { 6577a7e6055SDimitry Andric LaneBitmask LaneMask; 6587a7e6055SDimitry Andric if (LI->hasSubRanges()) { 6597a7e6055SDimitry Andric LaneMask = LaneBitmask::getNone(); 6607a7e6055SDimitry Andric for (LiveInterval::SubRange &S : LI->subranges()) 6617a7e6055SDimitry Andric LaneMask |= S.LaneMask; 6627a7e6055SDimitry Andric } else { 6637a7e6055SDimitry Andric LaneMask = LaneBitmask::getAll(); 6647a7e6055SDimitry Andric } 6657a7e6055SDimitry Andric 666bd5abe19SDimitry Andric ++NumCopies; 6677a7e6055SDimitry Andric Def = buildCopy(Edit->getReg(), Reg, LaneMask, MBB, I, Late, RegIdx); 6682754fe60SDimitry Andric } 6692754fe60SDimitry Andric 6702754fe60SDimitry Andric // Define the value in Reg. 671d88c1a5aSDimitry Andric return defValue(RegIdx, ParentVNI, Def, false); 672e580952dSDimitry Andric } 673e580952dSDimitry Andric 674e580952dSDimitry Andric /// Create a new virtual register and live interval. 6753b0f4066SDimitry Andric unsigned SplitEditor::openIntv() { 6762754fe60SDimitry Andric // Create the complement as index 0. 6773b0f4066SDimitry Andric if (Edit->empty()) 678f785676fSDimitry Andric Edit->createEmptyInterval(); 679e580952dSDimitry Andric 6802754fe60SDimitry Andric // Create the open interval. 6813b0f4066SDimitry Andric OpenIdx = Edit->size(); 682f785676fSDimitry Andric Edit->createEmptyInterval(); 6833b0f4066SDimitry Andric return OpenIdx; 6843b0f4066SDimitry Andric } 6853b0f4066SDimitry Andric 6863b0f4066SDimitry Andric void SplitEditor::selectIntv(unsigned Idx) { 6873b0f4066SDimitry Andric assert(Idx != 0 && "Cannot select the complement interval"); 6883b0f4066SDimitry Andric assert(Idx < Edit->size() && "Can only select previously opened interval"); 68917a519f9SDimitry Andric DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n'); 6903b0f4066SDimitry Andric OpenIdx = Idx; 6912754fe60SDimitry Andric } 692e580952dSDimitry Andric 6932754fe60SDimitry Andric SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) { 6942754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before enterIntvBefore"); 6952754fe60SDimitry Andric DEBUG(dbgs() << " enterIntvBefore " << Idx); 6962754fe60SDimitry Andric Idx = Idx.getBaseIndex(); 6973b0f4066SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx); 6982754fe60SDimitry Andric if (!ParentVNI) { 6992754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 7002754fe60SDimitry Andric return Idx; 7012754fe60SDimitry Andric } 7022754fe60SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 7032754fe60SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Idx); 704e580952dSDimitry Andric assert(MI && "enterIntvBefore called with invalid index"); 705e580952dSDimitry Andric 7062754fe60SDimitry Andric VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI); 7072754fe60SDimitry Andric return VNI->def; 708e580952dSDimitry Andric } 709e580952dSDimitry Andric 71017a519f9SDimitry Andric SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) { 71117a519f9SDimitry Andric assert(OpenIdx && "openIntv not called before enterIntvAfter"); 71217a519f9SDimitry Andric DEBUG(dbgs() << " enterIntvAfter " << Idx); 71317a519f9SDimitry Andric Idx = Idx.getBoundaryIndex(); 71417a519f9SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx); 71517a519f9SDimitry Andric if (!ParentVNI) { 71617a519f9SDimitry Andric DEBUG(dbgs() << ": not live\n"); 71717a519f9SDimitry Andric return Idx; 71817a519f9SDimitry Andric } 71917a519f9SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 72017a519f9SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Idx); 72117a519f9SDimitry Andric assert(MI && "enterIntvAfter called with invalid index"); 72217a519f9SDimitry Andric 72317a519f9SDimitry Andric VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), 72491bc56edSDimitry Andric std::next(MachineBasicBlock::iterator(MI))); 72517a519f9SDimitry Andric return VNI->def; 72617a519f9SDimitry Andric } 72717a519f9SDimitry Andric 7282754fe60SDimitry Andric SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) { 7292754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before enterIntvAtEnd"); 7302754fe60SDimitry Andric SlotIndex End = LIS.getMBBEndIdx(&MBB); 7312754fe60SDimitry Andric SlotIndex Last = End.getPrevSlot(); 7322cab237bSDimitry Andric DEBUG(dbgs() << " enterIntvAtEnd " << printMBBReference(MBB) << ", " 7332cab237bSDimitry Andric << Last); 7343b0f4066SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last); 7352754fe60SDimitry Andric if (!ParentVNI) { 7362754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 7372754fe60SDimitry Andric return End; 7382754fe60SDimitry Andric } 7392754fe60SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id); 7402754fe60SDimitry Andric VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB, 741dff0c46cSDimitry Andric SA.getLastSplitPointIter(&MBB)); 7422754fe60SDimitry Andric RegAssign.insert(VNI->def, End, OpenIdx); 7432754fe60SDimitry Andric DEBUG(dump()); 7442754fe60SDimitry Andric return VNI->def; 745e580952dSDimitry Andric } 746e580952dSDimitry Andric 7472754fe60SDimitry Andric /// useIntv - indicate that all instructions in MBB should use OpenLI. 748e580952dSDimitry Andric void SplitEditor::useIntv(const MachineBasicBlock &MBB) { 7492754fe60SDimitry Andric useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB)); 750e580952dSDimitry Andric } 751e580952dSDimitry Andric 752e580952dSDimitry Andric void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) { 7532754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before useIntv"); 7542754fe60SDimitry Andric DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):"); 7552754fe60SDimitry Andric RegAssign.insert(Start, End, OpenIdx); 7562754fe60SDimitry Andric DEBUG(dump()); 757e580952dSDimitry Andric } 758e580952dSDimitry Andric 7592754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) { 7602754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before leaveIntvAfter"); 7612754fe60SDimitry Andric DEBUG(dbgs() << " leaveIntvAfter " << Idx); 7622754fe60SDimitry Andric 7632754fe60SDimitry Andric // The interval must be live beyond the instruction at Idx. 7646122f3e6SDimitry Andric SlotIndex Boundary = Idx.getBoundaryIndex(); 7656122f3e6SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary); 7662754fe60SDimitry Andric if (!ParentVNI) { 7672754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 7686122f3e6SDimitry Andric return Boundary.getNextSlot(); 7692754fe60SDimitry Andric } 7702754fe60SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 7716122f3e6SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Boundary); 7722754fe60SDimitry Andric assert(MI && "No instruction at index"); 7736122f3e6SDimitry Andric 7746122f3e6SDimitry Andric // In spill mode, make live ranges as short as possible by inserting the copy 7756122f3e6SDimitry Andric // before MI. This is only possible if that instruction doesn't redefine the 7766122f3e6SDimitry Andric // value. The inserted COPY is not a kill, and we don't need to recompute 7776122f3e6SDimitry Andric // the source live range. The spiller also won't try to hoist this copy. 7786122f3e6SDimitry Andric if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) && 7796122f3e6SDimitry Andric MI->readsVirtualRegister(Edit->getReg())) { 7806122f3e6SDimitry Andric forceRecompute(0, ParentVNI); 7816122f3e6SDimitry Andric defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI); 7826122f3e6SDimitry Andric return Idx; 7836122f3e6SDimitry Andric } 7846122f3e6SDimitry Andric 7856122f3e6SDimitry Andric VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(), 78691bc56edSDimitry Andric std::next(MachineBasicBlock::iterator(MI))); 7872754fe60SDimitry Andric return VNI->def; 788e580952dSDimitry Andric } 789e580952dSDimitry Andric 7902754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) { 7912754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before leaveIntvBefore"); 7922754fe60SDimitry Andric DEBUG(dbgs() << " leaveIntvBefore " << Idx); 793e580952dSDimitry Andric 7942754fe60SDimitry Andric // The interval must be live into the instruction at Idx. 7956122f3e6SDimitry Andric Idx = Idx.getBaseIndex(); 7963b0f4066SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx); 7972754fe60SDimitry Andric if (!ParentVNI) { 7982754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 7992754fe60SDimitry Andric return Idx.getNextSlot(); 8002754fe60SDimitry Andric } 8012754fe60SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 8022754fe60SDimitry Andric 8032754fe60SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Idx); 8042754fe60SDimitry Andric assert(MI && "No instruction at index"); 8052754fe60SDimitry Andric VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI); 8062754fe60SDimitry Andric return VNI->def; 807e580952dSDimitry Andric } 808e580952dSDimitry Andric 8092754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) { 8102754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before leaveIntvAtTop"); 8112754fe60SDimitry Andric SlotIndex Start = LIS.getMBBStartIdx(&MBB); 8122cab237bSDimitry Andric DEBUG(dbgs() << " leaveIntvAtTop " << printMBBReference(MBB) << ", " 8132cab237bSDimitry Andric << Start); 8142754fe60SDimitry Andric 8153b0f4066SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start); 8162754fe60SDimitry Andric if (!ParentVNI) { 8172754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 8182754fe60SDimitry Andric return Start; 819e580952dSDimitry Andric } 820e580952dSDimitry Andric 8212754fe60SDimitry Andric VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB, 822d88c1a5aSDimitry Andric MBB.SkipPHIsLabelsAndDebug(MBB.begin())); 8232754fe60SDimitry Andric RegAssign.insert(Start, VNI->def, OpenIdx); 8242754fe60SDimitry Andric DEBUG(dump()); 8252754fe60SDimitry Andric return VNI->def; 826e580952dSDimitry Andric } 827e580952dSDimitry Andric 8282754fe60SDimitry Andric void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) { 8292754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before overlapIntv"); 8303b0f4066SDimitry Andric const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start); 831dff0c46cSDimitry Andric assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) && 8322754fe60SDimitry Andric "Parent changes value in extended range"); 8332754fe60SDimitry Andric assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) && 8342754fe60SDimitry Andric "Range cannot span basic blocks"); 835e580952dSDimitry Andric 8366122f3e6SDimitry Andric // The complement interval will be extended as needed by LRCalc.extend(). 8373b0f4066SDimitry Andric if (ParentVNI) 8386122f3e6SDimitry Andric forceRecompute(0, ParentVNI); 8392754fe60SDimitry Andric DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):"); 8402754fe60SDimitry Andric RegAssign.insert(Start, End, OpenIdx); 8412754fe60SDimitry Andric DEBUG(dump()); 842e580952dSDimitry Andric } 843e580952dSDimitry Andric 8446122f3e6SDimitry Andric //===----------------------------------------------------------------------===// 8456122f3e6SDimitry Andric // Spill modes 8466122f3e6SDimitry Andric //===----------------------------------------------------------------------===// 8476122f3e6SDimitry Andric 8486122f3e6SDimitry Andric void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) { 849f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(0)); 8506122f3e6SDimitry Andric DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n"); 8516122f3e6SDimitry Andric RegAssignMap::iterator AssignI; 8526122f3e6SDimitry Andric AssignI.setMap(RegAssign); 8536122f3e6SDimitry Andric 8546122f3e6SDimitry Andric for (unsigned i = 0, e = Copies.size(); i != e; ++i) { 855ff0cc061SDimitry Andric SlotIndex Def = Copies[i]->def; 8566122f3e6SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Def); 8576122f3e6SDimitry Andric assert(MI && "No instruction for back-copy"); 8586122f3e6SDimitry Andric 8596122f3e6SDimitry Andric MachineBasicBlock *MBB = MI->getParent(); 8606122f3e6SDimitry Andric MachineBasicBlock::iterator MBBI(MI); 8616122f3e6SDimitry Andric bool AtBegin; 8626122f3e6SDimitry Andric do AtBegin = MBBI == MBB->begin(); 8636122f3e6SDimitry Andric while (!AtBegin && (--MBBI)->isDebugValue()); 8646122f3e6SDimitry Andric 8656122f3e6SDimitry Andric DEBUG(dbgs() << "Removing " << Def << '\t' << *MI); 866ff0cc061SDimitry Andric LIS.removeVRegDefAt(*LI, Def); 8673ca95b02SDimitry Andric LIS.RemoveMachineInstrFromMaps(*MI); 8686122f3e6SDimitry Andric MI->eraseFromParent(); 8696122f3e6SDimitry Andric 870ff0cc061SDimitry Andric // Adjust RegAssign if a register assignment is killed at Def. We want to 871ff0cc061SDimitry Andric // avoid calculating the live range of the source register if possible. 8727ae0e2c9SDimitry Andric AssignI.find(Def.getPrevSlot()); 8736122f3e6SDimitry Andric if (!AssignI.valid() || AssignI.start() >= Def) 8746122f3e6SDimitry Andric continue; 8756122f3e6SDimitry Andric // If MI doesn't kill the assigned register, just leave it. 8766122f3e6SDimitry Andric if (AssignI.stop() != Def) 8776122f3e6SDimitry Andric continue; 8786122f3e6SDimitry Andric unsigned RegIdx = AssignI.value(); 8796122f3e6SDimitry Andric if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) { 8806122f3e6SDimitry Andric DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx << '\n'); 8816122f3e6SDimitry Andric forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def)); 8826122f3e6SDimitry Andric } else { 8833ca95b02SDimitry Andric SlotIndex Kill = LIS.getInstructionIndex(*MBBI).getRegSlot(); 8846122f3e6SDimitry Andric DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI); 8856122f3e6SDimitry Andric AssignI.setStop(Kill); 8866122f3e6SDimitry Andric } 8876122f3e6SDimitry Andric } 8886122f3e6SDimitry Andric } 8896122f3e6SDimitry Andric 8906122f3e6SDimitry Andric MachineBasicBlock* 8916122f3e6SDimitry Andric SplitEditor::findShallowDominator(MachineBasicBlock *MBB, 8926122f3e6SDimitry Andric MachineBasicBlock *DefMBB) { 8936122f3e6SDimitry Andric if (MBB == DefMBB) 8946122f3e6SDimitry Andric return MBB; 8956122f3e6SDimitry Andric assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def."); 8966122f3e6SDimitry Andric 8976122f3e6SDimitry Andric const MachineLoopInfo &Loops = SA.Loops; 8986122f3e6SDimitry Andric const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB); 8996122f3e6SDimitry Andric MachineDomTreeNode *DefDomNode = MDT[DefMBB]; 9006122f3e6SDimitry Andric 9016122f3e6SDimitry Andric // Best candidate so far. 9026122f3e6SDimitry Andric MachineBasicBlock *BestMBB = MBB; 9032cab237bSDimitry Andric unsigned BestDepth = std::numeric_limits<unsigned>::max(); 9046122f3e6SDimitry Andric 9052cab237bSDimitry Andric while (true) { 9066122f3e6SDimitry Andric const MachineLoop *Loop = Loops.getLoopFor(MBB); 9076122f3e6SDimitry Andric 9086122f3e6SDimitry Andric // MBB isn't in a loop, it doesn't get any better. All dominators have a 9096122f3e6SDimitry Andric // higher frequency by definition. 9106122f3e6SDimitry Andric if (!Loop) { 9112cab237bSDimitry Andric DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB) << " dominates " 9122cab237bSDimitry Andric << printMBBReference(*MBB) << " at depth 0\n"); 9136122f3e6SDimitry Andric return MBB; 9146122f3e6SDimitry Andric } 9156122f3e6SDimitry Andric 9166122f3e6SDimitry Andric // We'll never be able to exit the DefLoop. 9176122f3e6SDimitry Andric if (Loop == DefLoop) { 9182cab237bSDimitry Andric DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB) << " dominates " 9192cab237bSDimitry Andric << printMBBReference(*MBB) << " in the same loop\n"); 9206122f3e6SDimitry Andric return MBB; 9216122f3e6SDimitry Andric } 9226122f3e6SDimitry Andric 9236122f3e6SDimitry Andric // Least busy dominator seen so far. 9246122f3e6SDimitry Andric unsigned Depth = Loop->getLoopDepth(); 9256122f3e6SDimitry Andric if (Depth < BestDepth) { 9266122f3e6SDimitry Andric BestMBB = MBB; 9276122f3e6SDimitry Andric BestDepth = Depth; 9282cab237bSDimitry Andric DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB) << " dominates " 9292cab237bSDimitry Andric << printMBBReference(*MBB) << " at depth " << Depth << '\n'); 9306122f3e6SDimitry Andric } 9316122f3e6SDimitry Andric 9326122f3e6SDimitry Andric // Leave loop by going to the immediate dominator of the loop header. 9336122f3e6SDimitry Andric // This is a bigger stride than simply walking up the dominator tree. 9346122f3e6SDimitry Andric MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom(); 9356122f3e6SDimitry Andric 9366122f3e6SDimitry Andric // Too far up the dominator tree? 9376122f3e6SDimitry Andric if (!IDom || !MDT.dominates(DefDomNode, IDom)) 9386122f3e6SDimitry Andric return BestMBB; 9396122f3e6SDimitry Andric 9406122f3e6SDimitry Andric MBB = IDom->getBlock(); 9416122f3e6SDimitry Andric } 9426122f3e6SDimitry Andric } 9436122f3e6SDimitry Andric 9443ca95b02SDimitry Andric void SplitEditor::computeRedundantBackCopies( 9453ca95b02SDimitry Andric DenseSet<unsigned> &NotToHoistSet, SmallVectorImpl<VNInfo *> &BackCopies) { 9463ca95b02SDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(0)); 9473ca95b02SDimitry Andric LiveInterval *Parent = &Edit->getParent(); 9483ca95b02SDimitry Andric SmallVector<SmallPtrSet<VNInfo *, 8>, 8> EqualVNs(Parent->getNumValNums()); 9493ca95b02SDimitry Andric SmallPtrSet<VNInfo *, 8> DominatedVNIs; 9503ca95b02SDimitry Andric 9513ca95b02SDimitry Andric // Aggregate VNIs having the same value as ParentVNI. 9523ca95b02SDimitry Andric for (VNInfo *VNI : LI->valnos) { 9533ca95b02SDimitry Andric if (VNI->isUnused()) 9543ca95b02SDimitry Andric continue; 9553ca95b02SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def); 9563ca95b02SDimitry Andric EqualVNs[ParentVNI->id].insert(VNI); 9573ca95b02SDimitry Andric } 9583ca95b02SDimitry Andric 9593ca95b02SDimitry Andric // For VNI aggregation of each ParentVNI, collect dominated, i.e., 9603ca95b02SDimitry Andric // redundant VNIs to BackCopies. 9613ca95b02SDimitry Andric for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) { 9623ca95b02SDimitry Andric VNInfo *ParentVNI = Parent->getValNumInfo(i); 9633ca95b02SDimitry Andric if (!NotToHoistSet.count(ParentVNI->id)) 9643ca95b02SDimitry Andric continue; 9653ca95b02SDimitry Andric SmallPtrSetIterator<VNInfo *> It1 = EqualVNs[ParentVNI->id].begin(); 9663ca95b02SDimitry Andric SmallPtrSetIterator<VNInfo *> It2 = It1; 9673ca95b02SDimitry Andric for (; It1 != EqualVNs[ParentVNI->id].end(); ++It1) { 9683ca95b02SDimitry Andric It2 = It1; 9693ca95b02SDimitry Andric for (++It2; It2 != EqualVNs[ParentVNI->id].end(); ++It2) { 9703ca95b02SDimitry Andric if (DominatedVNIs.count(*It1) || DominatedVNIs.count(*It2)) 9713ca95b02SDimitry Andric continue; 9723ca95b02SDimitry Andric 9733ca95b02SDimitry Andric MachineBasicBlock *MBB1 = LIS.getMBBFromIndex((*It1)->def); 9743ca95b02SDimitry Andric MachineBasicBlock *MBB2 = LIS.getMBBFromIndex((*It2)->def); 9753ca95b02SDimitry Andric if (MBB1 == MBB2) { 9763ca95b02SDimitry Andric DominatedVNIs.insert((*It1)->def < (*It2)->def ? (*It2) : (*It1)); 9773ca95b02SDimitry Andric } else if (MDT.dominates(MBB1, MBB2)) { 9783ca95b02SDimitry Andric DominatedVNIs.insert(*It2); 9793ca95b02SDimitry Andric } else if (MDT.dominates(MBB2, MBB1)) { 9803ca95b02SDimitry Andric DominatedVNIs.insert(*It1); 9813ca95b02SDimitry Andric } 9823ca95b02SDimitry Andric } 9833ca95b02SDimitry Andric } 9843ca95b02SDimitry Andric if (!DominatedVNIs.empty()) { 9853ca95b02SDimitry Andric forceRecompute(0, ParentVNI); 9863ca95b02SDimitry Andric for (auto VNI : DominatedVNIs) { 9873ca95b02SDimitry Andric BackCopies.push_back(VNI); 9883ca95b02SDimitry Andric } 9893ca95b02SDimitry Andric DominatedVNIs.clear(); 9903ca95b02SDimitry Andric } 9913ca95b02SDimitry Andric } 9923ca95b02SDimitry Andric } 9933ca95b02SDimitry Andric 9943ca95b02SDimitry Andric /// For SM_Size mode, find a common dominator for all the back-copies for 9953ca95b02SDimitry Andric /// the same ParentVNI and hoist the backcopies to the dominator BB. 9963ca95b02SDimitry Andric /// For SM_Speed mode, if the common dominator is hot and it is not beneficial 9973ca95b02SDimitry Andric /// to do the hoisting, simply remove the dominated backcopies for the same 9983ca95b02SDimitry Andric /// ParentVNI. 9993ca95b02SDimitry Andric void SplitEditor::hoistCopies() { 10006122f3e6SDimitry Andric // Get the complement interval, always RegIdx 0. 1001f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(0)); 10026122f3e6SDimitry Andric LiveInterval *Parent = &Edit->getParent(); 10036122f3e6SDimitry Andric 10046122f3e6SDimitry Andric // Track the nearest common dominator for all back-copies for each ParentVNI, 10056122f3e6SDimitry Andric // indexed by ParentVNI->id. 10062cab237bSDimitry Andric using DomPair = std::pair<MachineBasicBlock *, SlotIndex>; 10076122f3e6SDimitry Andric SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums()); 10083ca95b02SDimitry Andric // The total cost of all the back-copies for each ParentVNI. 10093ca95b02SDimitry Andric SmallVector<BlockFrequency, 8> Costs(Parent->getNumValNums()); 10103ca95b02SDimitry Andric // The ParentVNI->id set for which hoisting back-copies are not beneficial 10113ca95b02SDimitry Andric // for Speed. 10123ca95b02SDimitry Andric DenseSet<unsigned> NotToHoistSet; 10136122f3e6SDimitry Andric 10146122f3e6SDimitry Andric // Find the nearest common dominator for parent values with multiple 10156122f3e6SDimitry Andric // back-copies. If a single back-copy dominates, put it in DomPair.second. 101639d628a0SDimitry Andric for (VNInfo *VNI : LI->valnos) { 10177ae0e2c9SDimitry Andric if (VNI->isUnused()) 10187ae0e2c9SDimitry Andric continue; 10196122f3e6SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def); 10206122f3e6SDimitry Andric assert(ParentVNI && "Parent not live at complement def"); 10216122f3e6SDimitry Andric 10226122f3e6SDimitry Andric // Don't hoist remats. The complement is probably going to disappear 10236122f3e6SDimitry Andric // completely anyway. 10246122f3e6SDimitry Andric if (Edit->didRematerialize(ParentVNI)) 10256122f3e6SDimitry Andric continue; 10266122f3e6SDimitry Andric 10276122f3e6SDimitry Andric MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def); 10283ca95b02SDimitry Andric 10296122f3e6SDimitry Andric DomPair &Dom = NearestDom[ParentVNI->id]; 10306122f3e6SDimitry Andric 10316122f3e6SDimitry Andric // Keep directly defined parent values. This is either a PHI or an 10326122f3e6SDimitry Andric // instruction in the complement range. All other copies of ParentVNI 10336122f3e6SDimitry Andric // should be eliminated. 10346122f3e6SDimitry Andric if (VNI->def == ParentVNI->def) { 10356122f3e6SDimitry Andric DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n'); 10366122f3e6SDimitry Andric Dom = DomPair(ValMBB, VNI->def); 10376122f3e6SDimitry Andric continue; 10386122f3e6SDimitry Andric } 10396122f3e6SDimitry Andric // Skip the singly mapped values. There is nothing to gain from hoisting a 10406122f3e6SDimitry Andric // single back-copy. 10416122f3e6SDimitry Andric if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) { 10426122f3e6SDimitry Andric DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n'); 10436122f3e6SDimitry Andric continue; 10446122f3e6SDimitry Andric } 10456122f3e6SDimitry Andric 10466122f3e6SDimitry Andric if (!Dom.first) { 10476122f3e6SDimitry Andric // First time we see ParentVNI. VNI dominates itself. 10486122f3e6SDimitry Andric Dom = DomPair(ValMBB, VNI->def); 10496122f3e6SDimitry Andric } else if (Dom.first == ValMBB) { 10506122f3e6SDimitry Andric // Two defs in the same block. Pick the earlier def. 10516122f3e6SDimitry Andric if (!Dom.second.isValid() || VNI->def < Dom.second) 10526122f3e6SDimitry Andric Dom.second = VNI->def; 10536122f3e6SDimitry Andric } else { 10546122f3e6SDimitry Andric // Different basic blocks. Check if one dominates. 10556122f3e6SDimitry Andric MachineBasicBlock *Near = 10566122f3e6SDimitry Andric MDT.findNearestCommonDominator(Dom.first, ValMBB); 10576122f3e6SDimitry Andric if (Near == ValMBB) 10586122f3e6SDimitry Andric // Def ValMBB dominates. 10596122f3e6SDimitry Andric Dom = DomPair(ValMBB, VNI->def); 10606122f3e6SDimitry Andric else if (Near != Dom.first) 10616122f3e6SDimitry Andric // None dominate. Hoist to common dominator, need new def. 10626122f3e6SDimitry Andric Dom = DomPair(Near, SlotIndex()); 10633ca95b02SDimitry Andric Costs[ParentVNI->id] += MBFI.getBlockFreq(ValMBB); 10646122f3e6SDimitry Andric } 10656122f3e6SDimitry Andric 10666122f3e6SDimitry Andric DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def 10676122f3e6SDimitry Andric << " for parent " << ParentVNI->id << '@' << ParentVNI->def 10682cab237bSDimitry Andric << " hoist to " << printMBBReference(*Dom.first) << ' ' 10696122f3e6SDimitry Andric << Dom.second << '\n'); 10706122f3e6SDimitry Andric } 10716122f3e6SDimitry Andric 10726122f3e6SDimitry Andric // Insert the hoisted copies. 10736122f3e6SDimitry Andric for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) { 10746122f3e6SDimitry Andric DomPair &Dom = NearestDom[i]; 10756122f3e6SDimitry Andric if (!Dom.first || Dom.second.isValid()) 10766122f3e6SDimitry Andric continue; 10776122f3e6SDimitry Andric // This value needs a hoisted copy inserted at the end of Dom.first. 10786122f3e6SDimitry Andric VNInfo *ParentVNI = Parent->getValNumInfo(i); 10796122f3e6SDimitry Andric MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def); 10806122f3e6SDimitry Andric // Get a less loopy dominator than Dom.first. 10816122f3e6SDimitry Andric Dom.first = findShallowDominator(Dom.first, DefMBB); 10823ca95b02SDimitry Andric if (SpillMode == SM_Speed && 10833ca95b02SDimitry Andric MBFI.getBlockFreq(Dom.first) > Costs[ParentVNI->id]) { 10843ca95b02SDimitry Andric NotToHoistSet.insert(ParentVNI->id); 10853ca95b02SDimitry Andric continue; 10863ca95b02SDimitry Andric } 10876122f3e6SDimitry Andric SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot(); 10886122f3e6SDimitry Andric Dom.second = 10896122f3e6SDimitry Andric defFromParent(0, ParentVNI, Last, *Dom.first, 1090dff0c46cSDimitry Andric SA.getLastSplitPointIter(Dom.first))->def; 10916122f3e6SDimitry Andric } 10926122f3e6SDimitry Andric 10936122f3e6SDimitry Andric // Remove redundant back-copies that are now known to be dominated by another 10946122f3e6SDimitry Andric // def with the same value. 10956122f3e6SDimitry Andric SmallVector<VNInfo*, 8> BackCopies; 109639d628a0SDimitry Andric for (VNInfo *VNI : LI->valnos) { 10977ae0e2c9SDimitry Andric if (VNI->isUnused()) 10987ae0e2c9SDimitry Andric continue; 10996122f3e6SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def); 11006122f3e6SDimitry Andric const DomPair &Dom = NearestDom[ParentVNI->id]; 11013ca95b02SDimitry Andric if (!Dom.first || Dom.second == VNI->def || 11023ca95b02SDimitry Andric NotToHoistSet.count(ParentVNI->id)) 11036122f3e6SDimitry Andric continue; 11046122f3e6SDimitry Andric BackCopies.push_back(VNI); 11056122f3e6SDimitry Andric forceRecompute(0, ParentVNI); 11066122f3e6SDimitry Andric } 11073ca95b02SDimitry Andric 11083ca95b02SDimitry Andric // If it is not beneficial to hoist all the BackCopies, simply remove 11093ca95b02SDimitry Andric // redundant BackCopies in speed mode. 11103ca95b02SDimitry Andric if (SpillMode == SM_Speed && !NotToHoistSet.empty()) 11113ca95b02SDimitry Andric computeRedundantBackCopies(NotToHoistSet, BackCopies); 11123ca95b02SDimitry Andric 11136122f3e6SDimitry Andric removeBackCopies(BackCopies); 11146122f3e6SDimitry Andric } 11156122f3e6SDimitry Andric 11163b0f4066SDimitry Andric /// transferValues - Transfer all possible values to the new live ranges. 11176122f3e6SDimitry Andric /// Values that were rematerialized are left alone, they need LRCalc.extend(). 11183b0f4066SDimitry Andric bool SplitEditor::transferValues() { 11193b0f4066SDimitry Andric bool Skipped = false; 11203b0f4066SDimitry Andric RegAssignMap::const_iterator AssignI = RegAssign.begin(); 112139d628a0SDimitry Andric for (const LiveRange::Segment &S : Edit->getParent()) { 112239d628a0SDimitry Andric DEBUG(dbgs() << " blit " << S << ':'); 112339d628a0SDimitry Andric VNInfo *ParentVNI = S.valno; 11243b0f4066SDimitry Andric // RegAssign has holes where RegIdx 0 should be used. 112539d628a0SDimitry Andric SlotIndex Start = S.start; 11263b0f4066SDimitry Andric AssignI.advanceTo(Start); 11273b0f4066SDimitry Andric do { 11283b0f4066SDimitry Andric unsigned RegIdx; 112939d628a0SDimitry Andric SlotIndex End = S.end; 11303b0f4066SDimitry Andric if (!AssignI.valid()) { 11313b0f4066SDimitry Andric RegIdx = 0; 11323b0f4066SDimitry Andric } else if (AssignI.start() <= Start) { 11333b0f4066SDimitry Andric RegIdx = AssignI.value(); 11343b0f4066SDimitry Andric if (AssignI.stop() < End) { 11353b0f4066SDimitry Andric End = AssignI.stop(); 11363b0f4066SDimitry Andric ++AssignI; 11373b0f4066SDimitry Andric } 11383b0f4066SDimitry Andric } else { 11393b0f4066SDimitry Andric RegIdx = 0; 11403b0f4066SDimitry Andric End = std::min(End, AssignI.start()); 1141e580952dSDimitry Andric } 1142e580952dSDimitry Andric 11433b0f4066SDimitry Andric // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI. 1144d88c1a5aSDimitry Andric DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx 11452cab237bSDimitry Andric << '(' << printReg(Edit->get(RegIdx)) << ')'); 1146d88c1a5aSDimitry Andric LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx)); 11473b0f4066SDimitry Andric 11483b0f4066SDimitry Andric // Check for a simply defined value that can be blitted directly. 11496122f3e6SDimitry Andric ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id)); 11506122f3e6SDimitry Andric if (VNInfo *VNI = VFP.getPointer()) { 11513b0f4066SDimitry Andric DEBUG(dbgs() << ':' << VNI->id); 1152d88c1a5aSDimitry Andric LI.addSegment(LiveInterval::Segment(Start, End, VNI)); 11533b0f4066SDimitry Andric Start = End; 11543b0f4066SDimitry Andric continue; 11553b0f4066SDimitry Andric } 11563b0f4066SDimitry Andric 11576122f3e6SDimitry Andric // Skip values with forced recomputation. 11586122f3e6SDimitry Andric if (VFP.getInt()) { 11596122f3e6SDimitry Andric DEBUG(dbgs() << "(recalc)"); 11603b0f4066SDimitry Andric Skipped = true; 11613b0f4066SDimitry Andric Start = End; 11623b0f4066SDimitry Andric continue; 11633b0f4066SDimitry Andric } 11643b0f4066SDimitry Andric 11656122f3e6SDimitry Andric LiveRangeCalc &LRC = getLRCalc(RegIdx); 11663b0f4066SDimitry Andric 11673b0f4066SDimitry Andric // This value has multiple defs in RegIdx, but it wasn't rematerialized, 11683b0f4066SDimitry Andric // so the live range is accurate. Add live-in blocks in [Start;End) to the 11693b0f4066SDimitry Andric // LiveInBlocks. 11707d523365SDimitry Andric MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator(); 11713b0f4066SDimitry Andric SlotIndex BlockStart, BlockEnd; 11727d523365SDimitry Andric std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(&*MBB); 11733b0f4066SDimitry Andric 11743b0f4066SDimitry Andric // The first block may be live-in, or it may have its own def. 11753b0f4066SDimitry Andric if (Start != BlockStart) { 1176d88c1a5aSDimitry Andric VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End)); 11773b0f4066SDimitry Andric assert(VNI && "Missing def for complex mapped value"); 11782cab237bSDimitry Andric DEBUG(dbgs() << ':' << VNI->id << "*" << printMBBReference(*MBB)); 11793b0f4066SDimitry Andric // MBB has its own def. Is it also live-out? 11806122f3e6SDimitry Andric if (BlockEnd <= End) 11817d523365SDimitry Andric LRC.setLiveOutValue(&*MBB, VNI); 11826122f3e6SDimitry Andric 11833b0f4066SDimitry Andric // Skip to the next block for live-in. 11843b0f4066SDimitry Andric ++MBB; 11853b0f4066SDimitry Andric BlockStart = BlockEnd; 11863b0f4066SDimitry Andric } 11873b0f4066SDimitry Andric 11883b0f4066SDimitry Andric // Handle the live-in blocks covered by [Start;End). 11893b0f4066SDimitry Andric assert(Start <= BlockStart && "Expected live-in block"); 11903b0f4066SDimitry Andric while (BlockStart < End) { 11912cab237bSDimitry Andric DEBUG(dbgs() << ">" << printMBBReference(*MBB)); 11927d523365SDimitry Andric BlockEnd = LIS.getMBBEndIdx(&*MBB); 11933b0f4066SDimitry Andric if (BlockStart == ParentVNI->def) { 11943b0f4066SDimitry Andric // This block has the def of a parent PHI, so it isn't live-in. 11953b0f4066SDimitry Andric assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?"); 1196d88c1a5aSDimitry Andric VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End)); 11973b0f4066SDimitry Andric assert(VNI && "Missing def for complex mapped parent PHI"); 11986122f3e6SDimitry Andric if (End >= BlockEnd) 11997d523365SDimitry Andric LRC.setLiveOutValue(&*MBB, VNI); // Live-out as well. 12003b0f4066SDimitry Andric } else { 12016122f3e6SDimitry Andric // This block needs a live-in value. The last block covered may not 12026122f3e6SDimitry Andric // be live-out. 12033b0f4066SDimitry Andric if (End < BlockEnd) 1204d88c1a5aSDimitry Andric LRC.addLiveInBlock(LI, MDT[&*MBB], End); 12053b0f4066SDimitry Andric else { 12066122f3e6SDimitry Andric // Live-through, and we don't know the value. 1207d88c1a5aSDimitry Andric LRC.addLiveInBlock(LI, MDT[&*MBB]); 12087d523365SDimitry Andric LRC.setLiveOutValue(&*MBB, nullptr); 12093b0f4066SDimitry Andric } 12103b0f4066SDimitry Andric } 12113b0f4066SDimitry Andric BlockStart = BlockEnd; 12123b0f4066SDimitry Andric ++MBB; 12133b0f4066SDimitry Andric } 12143b0f4066SDimitry Andric Start = End; 121539d628a0SDimitry Andric } while (Start != S.end); 12163b0f4066SDimitry Andric DEBUG(dbgs() << '\n'); 12173b0f4066SDimitry Andric } 12183b0f4066SDimitry Andric 12197ae0e2c9SDimitry Andric LRCalc[0].calculateValues(); 12206122f3e6SDimitry Andric if (SpillMode) 12217ae0e2c9SDimitry Andric LRCalc[1].calculateValues(); 12223b0f4066SDimitry Andric 12233b0f4066SDimitry Andric return Skipped; 12243b0f4066SDimitry Andric } 12253b0f4066SDimitry Andric 1226d88c1a5aSDimitry Andric static bool removeDeadSegment(SlotIndex Def, LiveRange &LR) { 1227d88c1a5aSDimitry Andric const LiveRange::Segment *Seg = LR.getSegmentContaining(Def); 1228d88c1a5aSDimitry Andric if (Seg == nullptr) 1229d88c1a5aSDimitry Andric return true; 1230d88c1a5aSDimitry Andric if (Seg->end != Def.getDeadSlot()) 1231d88c1a5aSDimitry Andric return false; 12323ca95b02SDimitry Andric // This is a dead PHI. Remove it. 1233d88c1a5aSDimitry Andric LR.removeSegment(*Seg, true); 1234d88c1a5aSDimitry Andric return true; 12353ca95b02SDimitry Andric } 12363ca95b02SDimitry Andric 1237d88c1a5aSDimitry Andric void SplitEditor::extendPHIRange(MachineBasicBlock &B, LiveRangeCalc &LRC, 1238d88c1a5aSDimitry Andric LiveRange &LR, LaneBitmask LM, 1239d88c1a5aSDimitry Andric ArrayRef<SlotIndex> Undefs) { 1240d88c1a5aSDimitry Andric for (MachineBasicBlock *P : B.predecessors()) { 1241d88c1a5aSDimitry Andric SlotIndex End = LIS.getMBBEndIdx(P); 12426122f3e6SDimitry Andric SlotIndex LastUse = End.getPrevSlot(); 12433b0f4066SDimitry Andric // The predecessor may not have a live-out value. That is OK, like an 12443b0f4066SDimitry Andric // undef PHI operand. 1245d88c1a5aSDimitry Andric LiveInterval &PLI = Edit->getParent(); 1246d88c1a5aSDimitry Andric // Need the cast because the inputs to ?: would otherwise be deemed 1247d88c1a5aSDimitry Andric // "incompatible": SubRange vs LiveInterval. 1248d88c1a5aSDimitry Andric LiveRange &PSR = !LM.all() ? getSubRangeForMask(LM, PLI) 1249d88c1a5aSDimitry Andric : static_cast<LiveRange&>(PLI); 1250d88c1a5aSDimitry Andric if (PSR.liveAt(LastUse)) 1251d88c1a5aSDimitry Andric LRC.extend(LR, End, /*PhysReg=*/0, Undefs); 12523b0f4066SDimitry Andric } 12533b0f4066SDimitry Andric } 1254d88c1a5aSDimitry Andric 1255d88c1a5aSDimitry Andric void SplitEditor::extendPHIKillRanges() { 1256d88c1a5aSDimitry Andric // Extend live ranges to be live-out for successor PHI values. 1257d88c1a5aSDimitry Andric 1258d88c1a5aSDimitry Andric // Visit each PHI def slot in the parent live interval. If the def is dead, 1259d88c1a5aSDimitry Andric // remove it. Otherwise, extend the live interval to reach the end indexes 1260d88c1a5aSDimitry Andric // of all predecessor blocks. 1261d88c1a5aSDimitry Andric 1262d88c1a5aSDimitry Andric LiveInterval &ParentLI = Edit->getParent(); 1263d88c1a5aSDimitry Andric for (const VNInfo *V : ParentLI.valnos) { 1264d88c1a5aSDimitry Andric if (V->isUnused() || !V->isPHIDef()) 1265d88c1a5aSDimitry Andric continue; 1266d88c1a5aSDimitry Andric 1267d88c1a5aSDimitry Andric unsigned RegIdx = RegAssign.lookup(V->def); 1268d88c1a5aSDimitry Andric LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx)); 1269d88c1a5aSDimitry Andric LiveRangeCalc &LRC = getLRCalc(RegIdx); 1270d88c1a5aSDimitry Andric MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def); 1271d88c1a5aSDimitry Andric if (!removeDeadSegment(V->def, LI)) 1272d88c1a5aSDimitry Andric extendPHIRange(B, LRC, LI, LaneBitmask::getAll(), /*Undefs=*/{}); 1273d88c1a5aSDimitry Andric } 1274d88c1a5aSDimitry Andric 1275d88c1a5aSDimitry Andric SmallVector<SlotIndex, 4> Undefs; 1276d88c1a5aSDimitry Andric LiveRangeCalc SubLRC; 1277d88c1a5aSDimitry Andric 1278d88c1a5aSDimitry Andric for (LiveInterval::SubRange &PS : ParentLI.subranges()) { 1279d88c1a5aSDimitry Andric for (const VNInfo *V : PS.valnos) { 1280d88c1a5aSDimitry Andric if (V->isUnused() || !V->isPHIDef()) 1281d88c1a5aSDimitry Andric continue; 1282d88c1a5aSDimitry Andric unsigned RegIdx = RegAssign.lookup(V->def); 1283d88c1a5aSDimitry Andric LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx)); 1284d88c1a5aSDimitry Andric LiveInterval::SubRange &S = getSubRangeForMask(PS.LaneMask, LI); 1285d88c1a5aSDimitry Andric if (removeDeadSegment(V->def, S)) 1286d88c1a5aSDimitry Andric continue; 1287d88c1a5aSDimitry Andric 1288d88c1a5aSDimitry Andric MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def); 1289d88c1a5aSDimitry Andric SubLRC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT, 1290d88c1a5aSDimitry Andric &LIS.getVNInfoAllocator()); 1291d88c1a5aSDimitry Andric Undefs.clear(); 1292d88c1a5aSDimitry Andric LI.computeSubRangeUndefs(Undefs, PS.LaneMask, MRI, *LIS.getSlotIndexes()); 1293d88c1a5aSDimitry Andric extendPHIRange(B, SubLRC, S, PS.LaneMask, Undefs); 1294d88c1a5aSDimitry Andric } 12953b0f4066SDimitry Andric } 12963b0f4066SDimitry Andric } 12973b0f4066SDimitry Andric 12983b0f4066SDimitry Andric /// rewriteAssigned - Rewrite all uses of Edit->getReg(). 12993b0f4066SDimitry Andric void SplitEditor::rewriteAssigned(bool ExtendRanges) { 1300d88c1a5aSDimitry Andric struct ExtPoint { 1301d88c1a5aSDimitry Andric ExtPoint(const MachineOperand &O, unsigned R, SlotIndex N) 1302d88c1a5aSDimitry Andric : MO(O), RegIdx(R), Next(N) {} 13032cab237bSDimitry Andric 1304d88c1a5aSDimitry Andric MachineOperand MO; 1305d88c1a5aSDimitry Andric unsigned RegIdx; 1306d88c1a5aSDimitry Andric SlotIndex Next; 1307d88c1a5aSDimitry Andric }; 1308d88c1a5aSDimitry Andric 1309d88c1a5aSDimitry Andric SmallVector<ExtPoint,4> ExtPoints; 1310d88c1a5aSDimitry Andric 13113b0f4066SDimitry Andric for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()), 13122754fe60SDimitry Andric RE = MRI.reg_end(); RI != RE;) { 131391bc56edSDimitry Andric MachineOperand &MO = *RI; 1314e580952dSDimitry Andric MachineInstr *MI = MO.getParent(); 1315e580952dSDimitry Andric ++RI; 13162754fe60SDimitry Andric // LiveDebugVariables should have handled all DBG_VALUE instructions. 1317e580952dSDimitry Andric if (MI->isDebugValue()) { 1318e580952dSDimitry Andric DEBUG(dbgs() << "Zapping " << *MI); 1319e580952dSDimitry Andric MO.setReg(0); 1320e580952dSDimitry Andric continue; 1321e580952dSDimitry Andric } 13222754fe60SDimitry Andric 13236122f3e6SDimitry Andric // <undef> operands don't really read the register, so it doesn't matter 13246122f3e6SDimitry Andric // which register we choose. When the use operand is tied to a def, we must 13256122f3e6SDimitry Andric // use the same register as the def, so just do that always. 13263ca95b02SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(*MI); 13276122f3e6SDimitry Andric if (MO.isDef() || MO.isUndef()) 1328dff0c46cSDimitry Andric Idx = Idx.getRegSlot(MO.isEarlyClobber()); 13292754fe60SDimitry Andric 13302754fe60SDimitry Andric // Rewrite to the mapped register at Idx. 13312754fe60SDimitry Andric unsigned RegIdx = RegAssign.lookup(Idx); 1332d88c1a5aSDimitry Andric LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx)); 1333d88c1a5aSDimitry Andric MO.setReg(LI.reg); 13342cab237bSDimitry Andric DEBUG(dbgs() << " rewr " << printMBBReference(*MI->getParent()) << '\t' 13352754fe60SDimitry Andric << Idx << ':' << RegIdx << '\t' << *MI); 13362754fe60SDimitry Andric 13373b0f4066SDimitry Andric // Extend liveness to Idx if the instruction reads reg. 13386122f3e6SDimitry Andric if (!ExtendRanges || MO.isUndef()) 13392754fe60SDimitry Andric continue; 13403b0f4066SDimitry Andric 13413b0f4066SDimitry Andric // Skip instructions that don't read Reg. 13423b0f4066SDimitry Andric if (MO.isDef()) { 13433b0f4066SDimitry Andric if (!MO.getSubReg() && !MO.isEarlyClobber()) 13443b0f4066SDimitry Andric continue; 1345d88c1a5aSDimitry Andric // We may want to extend a live range for a partial redef, or for a use 13463b0f4066SDimitry Andric // tied to an early clobber. 13473b0f4066SDimitry Andric Idx = Idx.getPrevSlot(); 13483b0f4066SDimitry Andric if (!Edit->getParent().liveAt(Idx)) 13493b0f4066SDimitry Andric continue; 13503b0f4066SDimitry Andric } else 1351dff0c46cSDimitry Andric Idx = Idx.getRegSlot(true); 13523b0f4066SDimitry Andric 1353d88c1a5aSDimitry Andric SlotIndex Next = Idx.getNextSlot(); 1354d88c1a5aSDimitry Andric if (LI.hasSubRanges()) { 1355d88c1a5aSDimitry Andric // We have to delay extending subranges until we have seen all operands 1356d88c1a5aSDimitry Andric // defining the register. This is because a <def,read-undef> operand 1357d88c1a5aSDimitry Andric // will create an "undef" point, and we cannot extend any subranges 1358d88c1a5aSDimitry Andric // until all of them have been accounted for. 1359d88c1a5aSDimitry Andric if (MO.isUse()) 1360d88c1a5aSDimitry Andric ExtPoints.push_back(ExtPoint(MO, RegIdx, Next)); 1361d88c1a5aSDimitry Andric } else { 1362d88c1a5aSDimitry Andric LiveRangeCalc &LRC = getLRCalc(RegIdx); 1363d88c1a5aSDimitry Andric LRC.extend(LI, Next, 0, ArrayRef<SlotIndex>()); 1364d88c1a5aSDimitry Andric } 1365d88c1a5aSDimitry Andric } 1366d88c1a5aSDimitry Andric 1367d88c1a5aSDimitry Andric for (ExtPoint &EP : ExtPoints) { 1368d88c1a5aSDimitry Andric LiveInterval &LI = LIS.getInterval(Edit->get(EP.RegIdx)); 1369d88c1a5aSDimitry Andric assert(LI.hasSubRanges()); 1370d88c1a5aSDimitry Andric 1371d88c1a5aSDimitry Andric LiveRangeCalc SubLRC; 1372d88c1a5aSDimitry Andric unsigned Reg = EP.MO.getReg(), Sub = EP.MO.getSubReg(); 1373d88c1a5aSDimitry Andric LaneBitmask LM = Sub != 0 ? TRI.getSubRegIndexLaneMask(Sub) 1374d88c1a5aSDimitry Andric : MRI.getMaxLaneMaskForVReg(Reg); 1375d88c1a5aSDimitry Andric for (LiveInterval::SubRange &S : LI.subranges()) { 1376d88c1a5aSDimitry Andric if ((S.LaneMask & LM).none()) 1377d88c1a5aSDimitry Andric continue; 1378d88c1a5aSDimitry Andric // The problem here can be that the new register may have been created 1379d88c1a5aSDimitry Andric // for a partially defined original register. For example: 13802cab237bSDimitry Andric // %0:subreg_hireg<def,read-undef> = ... 1381d88c1a5aSDimitry Andric // ... 13822cab237bSDimitry Andric // %1 = COPY %0 1383d88c1a5aSDimitry Andric if (S.empty()) 1384d88c1a5aSDimitry Andric continue; 1385d88c1a5aSDimitry Andric SubLRC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT, 1386d88c1a5aSDimitry Andric &LIS.getVNInfoAllocator()); 1387d88c1a5aSDimitry Andric SmallVector<SlotIndex, 4> Undefs; 1388d88c1a5aSDimitry Andric LI.computeSubRangeUndefs(Undefs, S.LaneMask, MRI, *LIS.getSlotIndexes()); 1389d88c1a5aSDimitry Andric SubLRC.extend(S, EP.Next, 0, Undefs); 1390d88c1a5aSDimitry Andric } 1391d88c1a5aSDimitry Andric } 1392d88c1a5aSDimitry Andric 1393d88c1a5aSDimitry Andric for (unsigned R : *Edit) { 1394d88c1a5aSDimitry Andric LiveInterval &LI = LIS.getInterval(R); 1395d88c1a5aSDimitry Andric if (!LI.hasSubRanges()) 1396d88c1a5aSDimitry Andric continue; 1397d88c1a5aSDimitry Andric LI.clear(); 1398d88c1a5aSDimitry Andric LI.removeEmptySubRanges(); 1399d88c1a5aSDimitry Andric LIS.constructMainRangeFromSubranges(LI); 1400e580952dSDimitry Andric } 1401e580952dSDimitry Andric } 1402e580952dSDimitry Andric 14033b0f4066SDimitry Andric void SplitEditor::deleteRematVictims() { 14043b0f4066SDimitry Andric SmallVector<MachineInstr*, 8> Dead; 14053b0f4066SDimitry Andric for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){ 1406f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(*I); 140739d628a0SDimitry Andric for (const LiveRange::Segment &S : LI->segments) { 1408dff0c46cSDimitry Andric // Dead defs end at the dead slot. 140939d628a0SDimitry Andric if (S.end != S.valno->def.getDeadSlot()) 14103b0f4066SDimitry Andric continue; 14113ca95b02SDimitry Andric if (S.valno->isPHIDef()) 14123ca95b02SDimitry Andric continue; 141339d628a0SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def); 14143b0f4066SDimitry Andric assert(MI && "Missing instruction for dead def"); 14153b0f4066SDimitry Andric MI->addRegisterDead(LI->reg, &TRI); 14163b0f4066SDimitry Andric 14173b0f4066SDimitry Andric if (!MI->allDefsAreDead()) 14183b0f4066SDimitry Andric continue; 14193b0f4066SDimitry Andric 14203b0f4066SDimitry Andric DEBUG(dbgs() << "All defs dead: " << *MI); 14213b0f4066SDimitry Andric Dead.push_back(MI); 14223b0f4066SDimitry Andric } 14233b0f4066SDimitry Andric } 14243b0f4066SDimitry Andric 14253b0f4066SDimitry Andric if (Dead.empty()) 14263b0f4066SDimitry Andric return; 14273b0f4066SDimitry Andric 14283ca95b02SDimitry Andric Edit->eliminateDeadDefs(Dead, None, &AA); 14293b0f4066SDimitry Andric } 14303b0f4066SDimitry Andric 14313b0f4066SDimitry Andric void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) { 14323b0f4066SDimitry Andric ++NumFinished; 14332754fe60SDimitry Andric 14342754fe60SDimitry Andric // At this point, the live intervals in Edit contain VNInfos corresponding to 14352754fe60SDimitry Andric // the inserted copies. 14362754fe60SDimitry Andric 14372754fe60SDimitry Andric // Add the original defs from the parent interval. 143839d628a0SDimitry Andric for (const VNInfo *ParentVNI : Edit->getParent().valnos) { 14392754fe60SDimitry Andric if (ParentVNI->isUnused()) 14402754fe60SDimitry Andric continue; 14413b0f4066SDimitry Andric unsigned RegIdx = RegAssign.lookup(ParentVNI->def); 1442d88c1a5aSDimitry Andric defValue(RegIdx, ParentVNI, ParentVNI->def, true); 14433b0f4066SDimitry Andric 14446122f3e6SDimitry Andric // Force rematted values to be recomputed everywhere. 14453b0f4066SDimitry Andric // The new live ranges may be truncated. 14463b0f4066SDimitry Andric if (Edit->didRematerialize(ParentVNI)) 14473b0f4066SDimitry Andric for (unsigned i = 0, e = Edit->size(); i != e; ++i) 14486122f3e6SDimitry Andric forceRecompute(i, ParentVNI); 14496122f3e6SDimitry Andric } 14506122f3e6SDimitry Andric 14516122f3e6SDimitry Andric // Hoist back-copies to the complement interval when in spill mode. 14526122f3e6SDimitry Andric switch (SpillMode) { 14536122f3e6SDimitry Andric case SM_Partition: 14546122f3e6SDimitry Andric // Leave all back-copies as is. 14556122f3e6SDimitry Andric break; 14566122f3e6SDimitry Andric case SM_Size: 14576122f3e6SDimitry Andric case SM_Speed: 14583ca95b02SDimitry Andric // hoistCopies will behave differently between size and speed. 14593ca95b02SDimitry Andric hoistCopies(); 14602754fe60SDimitry Andric } 14612754fe60SDimitry Andric 14623b0f4066SDimitry Andric // Transfer the simply mapped values, check if any are skipped. 14633b0f4066SDimitry Andric bool Skipped = transferValues(); 14643ca95b02SDimitry Andric 14653ca95b02SDimitry Andric // Rewrite virtual registers, possibly extending ranges. 14663ca95b02SDimitry Andric rewriteAssigned(Skipped); 14673ca95b02SDimitry Andric 14683b0f4066SDimitry Andric if (Skipped) 14693b0f4066SDimitry Andric extendPHIKillRanges(); 14702754fe60SDimitry Andric else 14713b0f4066SDimitry Andric ++NumSimple; 14722754fe60SDimitry Andric 14733b0f4066SDimitry Andric // Delete defs that were rematted everywhere. 14743b0f4066SDimitry Andric if (Skipped) 14753b0f4066SDimitry Andric deleteRematVictims(); 14762754fe60SDimitry Andric 14772754fe60SDimitry Andric // Get rid of unused values and set phi-kill flags. 1478d88c1a5aSDimitry Andric for (unsigned Reg : *Edit) { 1479d88c1a5aSDimitry Andric LiveInterval &LI = LIS.getInterval(Reg); 1480d88c1a5aSDimitry Andric LI.removeEmptySubRanges(); 1481f785676fSDimitry Andric LI.RenumberValues(); 1482f785676fSDimitry Andric } 14832754fe60SDimitry Andric 14843b0f4066SDimitry Andric // Provide a reverse mapping from original indices to Edit ranges. 14853b0f4066SDimitry Andric if (LRMap) { 14863b0f4066SDimitry Andric LRMap->clear(); 14873b0f4066SDimitry Andric for (unsigned i = 0, e = Edit->size(); i != e; ++i) 14883b0f4066SDimitry Andric LRMap->push_back(i); 14893b0f4066SDimitry Andric } 14903b0f4066SDimitry Andric 14912754fe60SDimitry Andric // Now check if any registers were separated into multiple components. 14922754fe60SDimitry Andric ConnectedVNInfoEqClasses ConEQ(LIS); 14933b0f4066SDimitry Andric for (unsigned i = 0, e = Edit->size(); i != e; ++i) { 14942754fe60SDimitry Andric // Don't use iterators, they are invalidated by create() below. 14957d523365SDimitry Andric unsigned VReg = Edit->get(i); 14967d523365SDimitry Andric LiveInterval &LI = LIS.getInterval(VReg); 14977d523365SDimitry Andric SmallVector<LiveInterval*, 8> SplitLIs; 14987d523365SDimitry Andric LIS.splitSeparateComponents(LI, SplitLIs); 14997d523365SDimitry Andric unsigned Original = VRM.getOriginal(VReg); 15007d523365SDimitry Andric for (LiveInterval *SplitLI : SplitLIs) 15017d523365SDimitry Andric VRM.setIsSplitFromReg(SplitLI->reg, Original); 15027d523365SDimitry Andric 15033b0f4066SDimitry Andric // The new intervals all map back to i. 15043b0f4066SDimitry Andric if (LRMap) 15053b0f4066SDimitry Andric LRMap->resize(Edit->size(), i); 15062754fe60SDimitry Andric } 15072754fe60SDimitry Andric 1508e580952dSDimitry Andric // Calculate spill weight and allocation hints for new intervals. 1509f785676fSDimitry Andric Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI); 15103b0f4066SDimitry Andric 15113b0f4066SDimitry Andric assert(!LRMap || LRMap->size() == Edit->size()); 1512e580952dSDimitry Andric } 1513e580952dSDimitry Andric 1514e580952dSDimitry Andric //===----------------------------------------------------------------------===// 1515e580952dSDimitry Andric // Single Block Splitting 1516e580952dSDimitry Andric //===----------------------------------------------------------------------===// 1517e580952dSDimitry Andric 15186122f3e6SDimitry Andric bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI, 15196122f3e6SDimitry Andric bool SingleInstrs) const { 15206122f3e6SDimitry Andric // Always split for multiple instructions. 15216122f3e6SDimitry Andric if (!BI.isOneInstr()) 15226122f3e6SDimitry Andric return true; 15236122f3e6SDimitry Andric // Don't split for single instructions unless explicitly requested. 15246122f3e6SDimitry Andric if (!SingleInstrs) 15252754fe60SDimitry Andric return false; 15266122f3e6SDimitry Andric // Splitting a live-through range always makes progress. 15276122f3e6SDimitry Andric if (BI.LiveIn && BI.LiveOut) 15286122f3e6SDimitry Andric return true; 15296122f3e6SDimitry Andric // No point in isolating a copy. It has no register class constraints. 15306122f3e6SDimitry Andric if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike()) 15316122f3e6SDimitry Andric return false; 15326122f3e6SDimitry Andric // Finally, don't isolate an end point that was created by earlier splits. 15336122f3e6SDimitry Andric return isOriginalEndpoint(BI.FirstInstr); 1534e580952dSDimitry Andric } 1535e580952dSDimitry Andric 15363b0f4066SDimitry Andric void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) { 15373b0f4066SDimitry Andric openIntv(); 15383b0f4066SDimitry Andric SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber()); 15396122f3e6SDimitry Andric SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr, 15403b0f4066SDimitry Andric LastSplitPoint)); 15416122f3e6SDimitry Andric if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) { 15426122f3e6SDimitry Andric useIntv(SegStart, leaveIntvAfter(BI.LastInstr)); 15433b0f4066SDimitry Andric } else { 15443b0f4066SDimitry Andric // The last use is after the last valid split point. 15453b0f4066SDimitry Andric SlotIndex SegStop = leaveIntvBefore(LastSplitPoint); 15463b0f4066SDimitry Andric useIntv(SegStart, SegStop); 15476122f3e6SDimitry Andric overlapIntv(SegStop, BI.LastInstr); 15483b0f4066SDimitry Andric } 15493b0f4066SDimitry Andric } 15503b0f4066SDimitry Andric 155117a519f9SDimitry Andric //===----------------------------------------------------------------------===// 155217a519f9SDimitry Andric // Global Live Range Splitting Support 155317a519f9SDimitry Andric //===----------------------------------------------------------------------===// 155417a519f9SDimitry Andric 155517a519f9SDimitry Andric // These methods support a method of global live range splitting that uses a 155617a519f9SDimitry Andric // global algorithm to decide intervals for CFG edges. They will insert split 155717a519f9SDimitry Andric // points and color intervals in basic blocks while avoiding interference. 155817a519f9SDimitry Andric // 155917a519f9SDimitry Andric // Note that splitSingleBlock is also useful for blocks where both CFG edges 156017a519f9SDimitry Andric // are on the stack. 156117a519f9SDimitry Andric 156217a519f9SDimitry Andric void SplitEditor::splitLiveThroughBlock(unsigned MBBNum, 156317a519f9SDimitry Andric unsigned IntvIn, SlotIndex LeaveBefore, 156417a519f9SDimitry Andric unsigned IntvOut, SlotIndex EnterAfter){ 156517a519f9SDimitry Andric SlotIndex Start, Stop; 156691bc56edSDimitry Andric std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum); 156717a519f9SDimitry Andric 15682cab237bSDimitry Andric DEBUG(dbgs() << "%bb." << MBBNum << " [" << Start << ';' << Stop << ") intf " 15692cab237bSDimitry Andric << LeaveBefore << '-' << EnterAfter << ", live-through " 15702cab237bSDimitry Andric << IntvIn << " -> " << IntvOut); 157117a519f9SDimitry Andric 157217a519f9SDimitry Andric assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks"); 157317a519f9SDimitry Andric 15746122f3e6SDimitry Andric assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block"); 15756122f3e6SDimitry Andric assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf"); 15766122f3e6SDimitry Andric assert((!EnterAfter || EnterAfter >= Start) && "Interference before block"); 15776122f3e6SDimitry Andric 15786122f3e6SDimitry Andric MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum); 15796122f3e6SDimitry Andric 158017a519f9SDimitry Andric if (!IntvOut) { 158117a519f9SDimitry Andric DEBUG(dbgs() << ", spill on entry.\n"); 158217a519f9SDimitry Andric // 158317a519f9SDimitry Andric // <<<<<<<<< Possible LeaveBefore interference. 158417a519f9SDimitry Andric // |-----------| Live through. 158517a519f9SDimitry Andric // -____________ Spill on entry. 158617a519f9SDimitry Andric // 158717a519f9SDimitry Andric selectIntv(IntvIn); 158817a519f9SDimitry Andric SlotIndex Idx = leaveIntvAtTop(*MBB); 158917a519f9SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); 159017a519f9SDimitry Andric (void)Idx; 159117a519f9SDimitry Andric return; 159217a519f9SDimitry Andric } 159317a519f9SDimitry Andric 159417a519f9SDimitry Andric if (!IntvIn) { 159517a519f9SDimitry Andric DEBUG(dbgs() << ", reload on exit.\n"); 159617a519f9SDimitry Andric // 159717a519f9SDimitry Andric // >>>>>>> Possible EnterAfter interference. 159817a519f9SDimitry Andric // |-----------| Live through. 159917a519f9SDimitry Andric // ___________-- Reload on exit. 160017a519f9SDimitry Andric // 160117a519f9SDimitry Andric selectIntv(IntvOut); 160217a519f9SDimitry Andric SlotIndex Idx = enterIntvAtEnd(*MBB); 160317a519f9SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); 160417a519f9SDimitry Andric (void)Idx; 160517a519f9SDimitry Andric return; 160617a519f9SDimitry Andric } 160717a519f9SDimitry Andric 160817a519f9SDimitry Andric if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) { 160917a519f9SDimitry Andric DEBUG(dbgs() << ", straight through.\n"); 161017a519f9SDimitry Andric // 161117a519f9SDimitry Andric // |-----------| Live through. 161217a519f9SDimitry Andric // ------------- Straight through, same intv, no interference. 161317a519f9SDimitry Andric // 161417a519f9SDimitry Andric selectIntv(IntvOut); 161517a519f9SDimitry Andric useIntv(Start, Stop); 161617a519f9SDimitry Andric return; 161717a519f9SDimitry Andric } 161817a519f9SDimitry Andric 161917a519f9SDimitry Andric // We cannot legally insert splits after LSP. 162017a519f9SDimitry Andric SlotIndex LSP = SA.getLastSplitPoint(MBBNum); 16216122f3e6SDimitry Andric assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf"); 162217a519f9SDimitry Andric 162317a519f9SDimitry Andric if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter || 162417a519f9SDimitry Andric LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) { 162517a519f9SDimitry Andric DEBUG(dbgs() << ", switch avoiding interference.\n"); 162617a519f9SDimitry Andric // 162717a519f9SDimitry Andric // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference. 162817a519f9SDimitry Andric // |-----------| Live through. 162917a519f9SDimitry Andric // ------======= Switch intervals between interference. 163017a519f9SDimitry Andric // 163117a519f9SDimitry Andric selectIntv(IntvOut); 16326122f3e6SDimitry Andric SlotIndex Idx; 16336122f3e6SDimitry Andric if (LeaveBefore && LeaveBefore < LSP) { 16346122f3e6SDimitry Andric Idx = enterIntvBefore(LeaveBefore); 163517a519f9SDimitry Andric useIntv(Idx, Stop); 16366122f3e6SDimitry Andric } else { 16376122f3e6SDimitry Andric Idx = enterIntvAtEnd(*MBB); 16386122f3e6SDimitry Andric } 163917a519f9SDimitry Andric selectIntv(IntvIn); 164017a519f9SDimitry Andric useIntv(Start, Idx); 164117a519f9SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); 164217a519f9SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); 164317a519f9SDimitry Andric return; 164417a519f9SDimitry Andric } 164517a519f9SDimitry Andric 164617a519f9SDimitry Andric DEBUG(dbgs() << ", create local intv for interference.\n"); 164717a519f9SDimitry Andric // 164817a519f9SDimitry Andric // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference. 164917a519f9SDimitry Andric // |-----------| Live through. 165017a519f9SDimitry Andric // ==---------== Switch intervals before/after interference. 165117a519f9SDimitry Andric // 165217a519f9SDimitry Andric assert(LeaveBefore <= EnterAfter && "Missed case"); 165317a519f9SDimitry Andric 165417a519f9SDimitry Andric selectIntv(IntvOut); 165517a519f9SDimitry Andric SlotIndex Idx = enterIntvAfter(EnterAfter); 165617a519f9SDimitry Andric useIntv(Idx, Stop); 165717a519f9SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); 165817a519f9SDimitry Andric 165917a519f9SDimitry Andric selectIntv(IntvIn); 166017a519f9SDimitry Andric Idx = leaveIntvBefore(LeaveBefore); 166117a519f9SDimitry Andric useIntv(Start, Idx); 166217a519f9SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); 166317a519f9SDimitry Andric } 166417a519f9SDimitry Andric 166517a519f9SDimitry Andric void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI, 166617a519f9SDimitry Andric unsigned IntvIn, SlotIndex LeaveBefore) { 166717a519f9SDimitry Andric SlotIndex Start, Stop; 166891bc56edSDimitry Andric std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB); 166917a519f9SDimitry Andric 16702cab237bSDimitry Andric DEBUG(dbgs() << printMBBReference(*BI.MBB) << " [" << Start << ';' << Stop 16716122f3e6SDimitry Andric << "), uses " << BI.FirstInstr << '-' << BI.LastInstr 167217a519f9SDimitry Andric << ", reg-in " << IntvIn << ", leave before " << LeaveBefore 167317a519f9SDimitry Andric << (BI.LiveOut ? ", stack-out" : ", killed in block")); 167417a519f9SDimitry Andric 167517a519f9SDimitry Andric assert(IntvIn && "Must have register in"); 167617a519f9SDimitry Andric assert(BI.LiveIn && "Must be live-in"); 167717a519f9SDimitry Andric assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference"); 167817a519f9SDimitry Andric 16796122f3e6SDimitry Andric if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) { 168017a519f9SDimitry Andric DEBUG(dbgs() << " before interference.\n"); 168117a519f9SDimitry Andric // 168217a519f9SDimitry Andric // <<< Interference after kill. 168317a519f9SDimitry Andric // |---o---x | Killed in block. 168417a519f9SDimitry Andric // ========= Use IntvIn everywhere. 168517a519f9SDimitry Andric // 168617a519f9SDimitry Andric selectIntv(IntvIn); 16876122f3e6SDimitry Andric useIntv(Start, BI.LastInstr); 168817a519f9SDimitry Andric return; 168917a519f9SDimitry Andric } 169017a519f9SDimitry Andric 169117a519f9SDimitry Andric SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber()); 169217a519f9SDimitry Andric 16936122f3e6SDimitry Andric if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) { 169417a519f9SDimitry Andric // 169517a519f9SDimitry Andric // <<< Possible interference after last use. 169617a519f9SDimitry Andric // |---o---o---| Live-out on stack. 169717a519f9SDimitry Andric // =========____ Leave IntvIn after last use. 169817a519f9SDimitry Andric // 169917a519f9SDimitry Andric // < Interference after last use. 170017a519f9SDimitry Andric // |---o---o--o| Live-out on stack, late last use. 170117a519f9SDimitry Andric // ============ Copy to stack after LSP, overlap IntvIn. 170217a519f9SDimitry Andric // \_____ Stack interval is live-out. 170317a519f9SDimitry Andric // 17046122f3e6SDimitry Andric if (BI.LastInstr < LSP) { 170517a519f9SDimitry Andric DEBUG(dbgs() << ", spill after last use before interference.\n"); 170617a519f9SDimitry Andric selectIntv(IntvIn); 17076122f3e6SDimitry Andric SlotIndex Idx = leaveIntvAfter(BI.LastInstr); 170817a519f9SDimitry Andric useIntv(Start, Idx); 170917a519f9SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); 171017a519f9SDimitry Andric } else { 171117a519f9SDimitry Andric DEBUG(dbgs() << ", spill before last split point.\n"); 171217a519f9SDimitry Andric selectIntv(IntvIn); 171317a519f9SDimitry Andric SlotIndex Idx = leaveIntvBefore(LSP); 17146122f3e6SDimitry Andric overlapIntv(Idx, BI.LastInstr); 171517a519f9SDimitry Andric useIntv(Start, Idx); 171617a519f9SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); 171717a519f9SDimitry Andric } 171817a519f9SDimitry Andric return; 171917a519f9SDimitry Andric } 172017a519f9SDimitry Andric 172117a519f9SDimitry Andric // The interference is overlapping somewhere we wanted to use IntvIn. That 172217a519f9SDimitry Andric // means we need to create a local interval that can be allocated a 172317a519f9SDimitry Andric // different register. 172417a519f9SDimitry Andric unsigned LocalIntv = openIntv(); 172517a519f9SDimitry Andric (void)LocalIntv; 172617a519f9SDimitry Andric DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n"); 172717a519f9SDimitry Andric 17286122f3e6SDimitry Andric if (!BI.LiveOut || BI.LastInstr < LSP) { 172917a519f9SDimitry Andric // 173017a519f9SDimitry Andric // <<<<<<< Interference overlapping uses. 173117a519f9SDimitry Andric // |---o---o---| Live-out on stack. 173217a519f9SDimitry Andric // =====----____ Leave IntvIn before interference, then spill. 173317a519f9SDimitry Andric // 17346122f3e6SDimitry Andric SlotIndex To = leaveIntvAfter(BI.LastInstr); 173517a519f9SDimitry Andric SlotIndex From = enterIntvBefore(LeaveBefore); 173617a519f9SDimitry Andric useIntv(From, To); 173717a519f9SDimitry Andric selectIntv(IntvIn); 173817a519f9SDimitry Andric useIntv(Start, From); 173917a519f9SDimitry Andric assert((!LeaveBefore || From <= LeaveBefore) && "Interference"); 174017a519f9SDimitry Andric return; 174117a519f9SDimitry Andric } 174217a519f9SDimitry Andric 174317a519f9SDimitry Andric // <<<<<<< Interference overlapping uses. 174417a519f9SDimitry Andric // |---o---o--o| Live-out on stack, late last use. 174517a519f9SDimitry Andric // =====------- Copy to stack before LSP, overlap LocalIntv. 174617a519f9SDimitry Andric // \_____ Stack interval is live-out. 174717a519f9SDimitry Andric // 174817a519f9SDimitry Andric SlotIndex To = leaveIntvBefore(LSP); 17496122f3e6SDimitry Andric overlapIntv(To, BI.LastInstr); 175017a519f9SDimitry Andric SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore)); 175117a519f9SDimitry Andric useIntv(From, To); 175217a519f9SDimitry Andric selectIntv(IntvIn); 175317a519f9SDimitry Andric useIntv(Start, From); 175417a519f9SDimitry Andric assert((!LeaveBefore || From <= LeaveBefore) && "Interference"); 175517a519f9SDimitry Andric } 175617a519f9SDimitry Andric 175717a519f9SDimitry Andric void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI, 175817a519f9SDimitry Andric unsigned IntvOut, SlotIndex EnterAfter) { 175917a519f9SDimitry Andric SlotIndex Start, Stop; 176091bc56edSDimitry Andric std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB); 176117a519f9SDimitry Andric 17622cab237bSDimitry Andric DEBUG(dbgs() << printMBBReference(*BI.MBB) << " [" << Start << ';' << Stop 17636122f3e6SDimitry Andric << "), uses " << BI.FirstInstr << '-' << BI.LastInstr 176417a519f9SDimitry Andric << ", reg-out " << IntvOut << ", enter after " << EnterAfter 176517a519f9SDimitry Andric << (BI.LiveIn ? ", stack-in" : ", defined in block")); 176617a519f9SDimitry Andric 176717a519f9SDimitry Andric SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber()); 176817a519f9SDimitry Andric 176917a519f9SDimitry Andric assert(IntvOut && "Must have register out"); 177017a519f9SDimitry Andric assert(BI.LiveOut && "Must be live-out"); 177117a519f9SDimitry Andric assert((!EnterAfter || EnterAfter < LSP) && "Bad interference"); 177217a519f9SDimitry Andric 17736122f3e6SDimitry Andric if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) { 177417a519f9SDimitry Andric DEBUG(dbgs() << " after interference.\n"); 177517a519f9SDimitry Andric // 177617a519f9SDimitry Andric // >>>> Interference before def. 177717a519f9SDimitry Andric // | o---o---| Defined in block. 177817a519f9SDimitry Andric // ========= Use IntvOut everywhere. 177917a519f9SDimitry Andric // 178017a519f9SDimitry Andric selectIntv(IntvOut); 17816122f3e6SDimitry Andric useIntv(BI.FirstInstr, Stop); 178217a519f9SDimitry Andric return; 178317a519f9SDimitry Andric } 178417a519f9SDimitry Andric 17856122f3e6SDimitry Andric if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) { 178617a519f9SDimitry Andric DEBUG(dbgs() << ", reload after interference.\n"); 178717a519f9SDimitry Andric // 178817a519f9SDimitry Andric // >>>> Interference before def. 178917a519f9SDimitry Andric // |---o---o---| Live-through, stack-in. 179017a519f9SDimitry Andric // ____========= Enter IntvOut before first use. 179117a519f9SDimitry Andric // 179217a519f9SDimitry Andric selectIntv(IntvOut); 17936122f3e6SDimitry Andric SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr)); 179417a519f9SDimitry Andric useIntv(Idx, Stop); 179517a519f9SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); 179617a519f9SDimitry Andric return; 179717a519f9SDimitry Andric } 179817a519f9SDimitry Andric 179917a519f9SDimitry Andric // The interference is overlapping somewhere we wanted to use IntvOut. That 180017a519f9SDimitry Andric // means we need to create a local interval that can be allocated a 180117a519f9SDimitry Andric // different register. 180217a519f9SDimitry Andric DEBUG(dbgs() << ", interference overlaps uses.\n"); 180317a519f9SDimitry Andric // 180417a519f9SDimitry Andric // >>>>>>> Interference overlapping uses. 180517a519f9SDimitry Andric // |---o---o---| Live-through, stack-in. 180617a519f9SDimitry Andric // ____---====== Create local interval for interference range. 180717a519f9SDimitry Andric // 180817a519f9SDimitry Andric selectIntv(IntvOut); 180917a519f9SDimitry Andric SlotIndex Idx = enterIntvAfter(EnterAfter); 181017a519f9SDimitry Andric useIntv(Idx, Stop); 181117a519f9SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); 181217a519f9SDimitry Andric 181317a519f9SDimitry Andric openIntv(); 18146122f3e6SDimitry Andric SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr)); 181517a519f9SDimitry Andric useIntv(From, Idx); 181617a519f9SDimitry Andric } 1817