1e580952dSDimitry Andric //===---------- SplitKit.cpp - Toolkit for splitting live ranges ----------===// 2e580952dSDimitry Andric // 3e580952dSDimitry Andric // The LLVM Compiler Infrastructure 4e580952dSDimitry Andric // 5e580952dSDimitry Andric // This file is distributed under the University of Illinois Open Source 6e580952dSDimitry Andric // License. See LICENSE.TXT for details. 7e580952dSDimitry Andric // 8e580952dSDimitry Andric //===----------------------------------------------------------------------===// 9e580952dSDimitry Andric // 10e580952dSDimitry Andric // This file contains the SplitAnalysis class as well as mutator functions for 11e580952dSDimitry Andric // live range splitting. 12e580952dSDimitry Andric // 13e580952dSDimitry Andric //===----------------------------------------------------------------------===// 14e580952dSDimitry Andric 152754fe60SDimitry Andric #define DEBUG_TYPE "regalloc" 16e580952dSDimitry Andric #include "SplitKit.h" 172754fe60SDimitry Andric #include "LiveRangeEdit.h" 18e580952dSDimitry Andric #include "VirtRegMap.h" 193b0f4066SDimitry Andric #include "llvm/ADT/Statistic.h" 20e580952dSDimitry Andric #include "llvm/CodeGen/LiveIntervalAnalysis.h" 212754fe60SDimitry Andric #include "llvm/CodeGen/MachineDominators.h" 22e580952dSDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 23e580952dSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 24e580952dSDimitry Andric #include "llvm/Support/Debug.h" 25e580952dSDimitry Andric #include "llvm/Support/raw_ostream.h" 26e580952dSDimitry Andric #include "llvm/Target/TargetInstrInfo.h" 27e580952dSDimitry Andric #include "llvm/Target/TargetMachine.h" 28e580952dSDimitry Andric 29e580952dSDimitry Andric using namespace llvm; 30e580952dSDimitry Andric 313b0f4066SDimitry Andric STATISTIC(NumFinished, "Number of splits finished"); 323b0f4066SDimitry Andric STATISTIC(NumSimple, "Number of splits that were simple"); 33bd5abe19SDimitry Andric STATISTIC(NumCopies, "Number of copies inserted for splitting"); 34bd5abe19SDimitry Andric STATISTIC(NumRemats, "Number of rematerialized defs for splitting"); 35bd5abe19SDimitry Andric STATISTIC(NumRepairs, "Number of invalid live ranges repaired"); 36e580952dSDimitry Andric 37e580952dSDimitry Andric //===----------------------------------------------------------------------===// 38e580952dSDimitry Andric // Split Analysis 39e580952dSDimitry Andric //===----------------------------------------------------------------------===// 40e580952dSDimitry Andric 412754fe60SDimitry Andric SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, 42e580952dSDimitry Andric const LiveIntervals &lis, 43e580952dSDimitry Andric const MachineLoopInfo &mli) 442754fe60SDimitry Andric : MF(vrm.getMachineFunction()), 452754fe60SDimitry Andric VRM(vrm), 462754fe60SDimitry Andric LIS(lis), 472754fe60SDimitry Andric Loops(mli), 482754fe60SDimitry Andric TII(*MF.getTarget().getInstrInfo()), 493b0f4066SDimitry Andric CurLI(0), 503b0f4066SDimitry Andric LastSplitPoint(MF.getNumBlockIDs()) {} 51e580952dSDimitry Andric 52e580952dSDimitry Andric void SplitAnalysis::clear() { 532754fe60SDimitry Andric UseSlots.clear(); 543b0f4066SDimitry Andric UseBlocks.clear(); 553b0f4066SDimitry Andric ThroughBlocks.clear(); 562754fe60SDimitry Andric CurLI = 0; 57bd5abe19SDimitry Andric DidRepairRange = false; 58e580952dSDimitry Andric } 59e580952dSDimitry Andric 603b0f4066SDimitry Andric SlotIndex SplitAnalysis::computeLastSplitPoint(unsigned Num) { 613b0f4066SDimitry Andric const MachineBasicBlock *MBB = MF.getBlockNumbered(Num); 623b0f4066SDimitry Andric const MachineBasicBlock *LPad = MBB->getLandingPadSuccessor(); 633b0f4066SDimitry Andric std::pair<SlotIndex, SlotIndex> &LSP = LastSplitPoint[Num]; 643b0f4066SDimitry Andric 653b0f4066SDimitry Andric // Compute split points on the first call. The pair is independent of the 663b0f4066SDimitry Andric // current live interval. 673b0f4066SDimitry Andric if (!LSP.first.isValid()) { 683b0f4066SDimitry Andric MachineBasicBlock::const_iterator FirstTerm = MBB->getFirstTerminator(); 693b0f4066SDimitry Andric if (FirstTerm == MBB->end()) 703b0f4066SDimitry Andric LSP.first = LIS.getMBBEndIdx(MBB); 713b0f4066SDimitry Andric else 723b0f4066SDimitry Andric LSP.first = LIS.getInstructionIndex(FirstTerm); 733b0f4066SDimitry Andric 743b0f4066SDimitry Andric // If there is a landing pad successor, also find the call instruction. 753b0f4066SDimitry Andric if (!LPad) 763b0f4066SDimitry Andric return LSP.first; 773b0f4066SDimitry Andric // There may not be a call instruction (?) in which case we ignore LPad. 783b0f4066SDimitry Andric LSP.second = LSP.first; 793b0f4066SDimitry Andric for (MachineBasicBlock::const_iterator I = FirstTerm, E = MBB->begin(); 803b0f4066SDimitry Andric I != E; --I) 813b0f4066SDimitry Andric if (I->getDesc().isCall()) { 823b0f4066SDimitry Andric LSP.second = LIS.getInstructionIndex(I); 833b0f4066SDimitry Andric break; 843b0f4066SDimitry Andric } 853b0f4066SDimitry Andric } 863b0f4066SDimitry Andric 873b0f4066SDimitry Andric // If CurLI is live into a landing pad successor, move the last split point 883b0f4066SDimitry Andric // back to the call that may throw. 893b0f4066SDimitry Andric if (LPad && LSP.second.isValid() && LIS.isLiveInToMBB(*CurLI, LPad)) 903b0f4066SDimitry Andric return LSP.second; 913b0f4066SDimitry Andric else 923b0f4066SDimitry Andric return LSP.first; 93e580952dSDimitry Andric } 94e580952dSDimitry Andric 952754fe60SDimitry Andric /// analyzeUses - Count instructions, basic blocks, and loops using CurLI. 96e580952dSDimitry Andric void SplitAnalysis::analyzeUses() { 973b0f4066SDimitry Andric assert(UseSlots.empty() && "Call clear first"); 983b0f4066SDimitry Andric 993b0f4066SDimitry Andric // First get all the defs from the interval values. This provides the correct 1003b0f4066SDimitry Andric // slots for early clobbers. 1013b0f4066SDimitry Andric for (LiveInterval::const_vni_iterator I = CurLI->vni_begin(), 1023b0f4066SDimitry Andric E = CurLI->vni_end(); I != E; ++I) 1033b0f4066SDimitry Andric if (!(*I)->isPHIDef() && !(*I)->isUnused()) 1043b0f4066SDimitry Andric UseSlots.push_back((*I)->def); 1053b0f4066SDimitry Andric 1063b0f4066SDimitry Andric // Get use slots form the use-def chain. 1072754fe60SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 1083b0f4066SDimitry Andric for (MachineRegisterInfo::use_nodbg_iterator 1093b0f4066SDimitry Andric I = MRI.use_nodbg_begin(CurLI->reg), E = MRI.use_nodbg_end(); I != E; 1103b0f4066SDimitry Andric ++I) 1113b0f4066SDimitry Andric if (!I.getOperand().isUndef()) 1123b0f4066SDimitry Andric UseSlots.push_back(LIS.getInstructionIndex(&*I).getDefIndex()); 1133b0f4066SDimitry Andric 1142754fe60SDimitry Andric array_pod_sort(UseSlots.begin(), UseSlots.end()); 1153b0f4066SDimitry Andric 1163b0f4066SDimitry Andric // Remove duplicates, keeping the smaller slot for each instruction. 1173b0f4066SDimitry Andric // That is what we want for early clobbers. 1183b0f4066SDimitry Andric UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(), 1193b0f4066SDimitry Andric SlotIndex::isSameInstr), 1203b0f4066SDimitry Andric UseSlots.end()); 1213b0f4066SDimitry Andric 1223b0f4066SDimitry Andric // Compute per-live block info. 1233b0f4066SDimitry Andric if (!calcLiveBlockInfo()) { 1243b0f4066SDimitry Andric // FIXME: calcLiveBlockInfo found inconsistencies in the live range. 1253b0f4066SDimitry Andric // I am looking at you, SimpleRegisterCoalescing! 126bd5abe19SDimitry Andric DidRepairRange = true; 127bd5abe19SDimitry Andric ++NumRepairs; 1283b0f4066SDimitry Andric DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n"); 1293b0f4066SDimitry Andric const_cast<LiveIntervals&>(LIS) 1303b0f4066SDimitry Andric .shrinkToUses(const_cast<LiveInterval*>(CurLI)); 1313b0f4066SDimitry Andric UseBlocks.clear(); 1323b0f4066SDimitry Andric ThroughBlocks.clear(); 1333b0f4066SDimitry Andric bool fixed = calcLiveBlockInfo(); 1343b0f4066SDimitry Andric (void)fixed; 1353b0f4066SDimitry Andric assert(fixed && "Couldn't fix broken live interval"); 1363b0f4066SDimitry Andric } 1373b0f4066SDimitry Andric 1383b0f4066SDimitry Andric DEBUG(dbgs() << "Analyze counted " 1393b0f4066SDimitry Andric << UseSlots.size() << " instrs in " 1403b0f4066SDimitry Andric << UseBlocks.size() << " blocks, through " 1413b0f4066SDimitry Andric << NumThroughBlocks << " blocks.\n"); 142e580952dSDimitry Andric } 143e580952dSDimitry Andric 1442754fe60SDimitry Andric /// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks 1452754fe60SDimitry Andric /// where CurLI is live. 1463b0f4066SDimitry Andric bool SplitAnalysis::calcLiveBlockInfo() { 1473b0f4066SDimitry Andric ThroughBlocks.resize(MF.getNumBlockIDs()); 148bd5abe19SDimitry Andric NumThroughBlocks = NumGapBlocks = 0; 1492754fe60SDimitry Andric if (CurLI->empty()) 1503b0f4066SDimitry Andric return true; 151e580952dSDimitry Andric 1522754fe60SDimitry Andric LiveInterval::const_iterator LVI = CurLI->begin(); 1532754fe60SDimitry Andric LiveInterval::const_iterator LVE = CurLI->end(); 154e580952dSDimitry Andric 1552754fe60SDimitry Andric SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE; 1562754fe60SDimitry Andric UseI = UseSlots.begin(); 1572754fe60SDimitry Andric UseE = UseSlots.end(); 1582754fe60SDimitry Andric 1592754fe60SDimitry Andric // Loop over basic blocks where CurLI is live. 1602754fe60SDimitry Andric MachineFunction::iterator MFI = LIS.getMBBFromIndex(LVI->start); 1612754fe60SDimitry Andric for (;;) { 1622754fe60SDimitry Andric BlockInfo BI; 1632754fe60SDimitry Andric BI.MBB = MFI; 1642754fe60SDimitry Andric SlotIndex Start, Stop; 1652754fe60SDimitry Andric tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB); 1662754fe60SDimitry Andric 167bd5abe19SDimitry Andric // If the block contains no uses, the range must be live through. At one 168bd5abe19SDimitry Andric // point, SimpleRegisterCoalescing could create dangling ranges that ended 169bd5abe19SDimitry Andric // mid-block. 170bd5abe19SDimitry Andric if (UseI == UseE || *UseI >= Stop) { 171bd5abe19SDimitry Andric ++NumThroughBlocks; 172bd5abe19SDimitry Andric ThroughBlocks.set(BI.MBB->getNumber()); 173bd5abe19SDimitry Andric // The range shouldn't end mid-block if there are no uses. This shouldn't 174bd5abe19SDimitry Andric // happen. 175bd5abe19SDimitry Andric if (LVI->end < Stop) 176bd5abe19SDimitry Andric return false; 177bd5abe19SDimitry Andric } else { 178bd5abe19SDimitry Andric // This block has uses. Find the first and last uses in the block. 1792754fe60SDimitry Andric BI.FirstUse = *UseI; 1802754fe60SDimitry Andric assert(BI.FirstUse >= Start); 1812754fe60SDimitry Andric do ++UseI; 1822754fe60SDimitry Andric while (UseI != UseE && *UseI < Stop); 1832754fe60SDimitry Andric BI.LastUse = UseI[-1]; 1842754fe60SDimitry Andric assert(BI.LastUse < Stop); 185bd5abe19SDimitry Andric 186bd5abe19SDimitry Andric // LVI is the first live segment overlapping MBB. 187bd5abe19SDimitry Andric BI.LiveIn = LVI->start <= Start; 188e580952dSDimitry Andric 1892754fe60SDimitry Andric // Look for gaps in the live range. 1902754fe60SDimitry Andric BI.LiveOut = true; 1912754fe60SDimitry Andric while (LVI->end < Stop) { 1922754fe60SDimitry Andric SlotIndex LastStop = LVI->end; 1932754fe60SDimitry Andric if (++LVI == LVE || LVI->start >= Stop) { 1942754fe60SDimitry Andric BI.LiveOut = false; 195bd5abe19SDimitry Andric BI.LastUse = LastStop; 196e580952dSDimitry Andric break; 197e580952dSDimitry Andric } 1982754fe60SDimitry Andric if (LastStop < LVI->start) { 199bd5abe19SDimitry Andric // There is a gap in the live range. Create duplicate entries for the 200bd5abe19SDimitry Andric // live-in snippet and the live-out snippet. 201bd5abe19SDimitry Andric ++NumGapBlocks; 202bd5abe19SDimitry Andric 203bd5abe19SDimitry Andric // Push the Live-in part. 204bd5abe19SDimitry Andric BI.LiveThrough = false; 205bd5abe19SDimitry Andric BI.LiveOut = false; 206bd5abe19SDimitry Andric UseBlocks.push_back(BI); 207bd5abe19SDimitry Andric UseBlocks.back().LastUse = LastStop; 208bd5abe19SDimitry Andric 209bd5abe19SDimitry Andric // Set up BI for the live-out part. 210bd5abe19SDimitry Andric BI.LiveIn = false; 211bd5abe19SDimitry Andric BI.LiveOut = true; 212bd5abe19SDimitry Andric BI.FirstUse = LVI->start; 213e580952dSDimitry Andric } 214e580952dSDimitry Andric } 215e580952dSDimitry Andric 2162754fe60SDimitry Andric // Don't set LiveThrough when the block has a gap. 217bd5abe19SDimitry Andric BI.LiveThrough = BI.LiveIn && BI.LiveOut; 2183b0f4066SDimitry Andric UseBlocks.push_back(BI); 219e580952dSDimitry Andric 2202754fe60SDimitry Andric // LVI is now at LVE or LVI->end >= Stop. 2212754fe60SDimitry Andric if (LVI == LVE) 2222754fe60SDimitry Andric break; 223bd5abe19SDimitry Andric } 2242754fe60SDimitry Andric 2252754fe60SDimitry Andric // Live segment ends exactly at Stop. Move to the next segment. 2262754fe60SDimitry Andric if (LVI->end == Stop && ++LVI == LVE) 2272754fe60SDimitry Andric break; 2282754fe60SDimitry Andric 2292754fe60SDimitry Andric // Pick the next basic block. 2302754fe60SDimitry Andric if (LVI->start < Stop) 2312754fe60SDimitry Andric ++MFI; 2322754fe60SDimitry Andric else 2332754fe60SDimitry Andric MFI = LIS.getMBBFromIndex(LVI->start); 2342754fe60SDimitry Andric } 235bd5abe19SDimitry Andric 236bd5abe19SDimitry Andric assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count"); 2373b0f4066SDimitry Andric return true; 2383b0f4066SDimitry Andric } 2393b0f4066SDimitry Andric 2403b0f4066SDimitry Andric unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const { 2413b0f4066SDimitry Andric if (cli->empty()) 2423b0f4066SDimitry Andric return 0; 2433b0f4066SDimitry Andric LiveInterval *li = const_cast<LiveInterval*>(cli); 2443b0f4066SDimitry Andric LiveInterval::iterator LVI = li->begin(); 2453b0f4066SDimitry Andric LiveInterval::iterator LVE = li->end(); 2463b0f4066SDimitry Andric unsigned Count = 0; 2473b0f4066SDimitry Andric 2483b0f4066SDimitry Andric // Loop over basic blocks where li is live. 2493b0f4066SDimitry Andric MachineFunction::const_iterator MFI = LIS.getMBBFromIndex(LVI->start); 2503b0f4066SDimitry Andric SlotIndex Stop = LIS.getMBBEndIdx(MFI); 2513b0f4066SDimitry Andric for (;;) { 2523b0f4066SDimitry Andric ++Count; 2533b0f4066SDimitry Andric LVI = li->advanceTo(LVI, Stop); 2543b0f4066SDimitry Andric if (LVI == LVE) 2553b0f4066SDimitry Andric return Count; 2563b0f4066SDimitry Andric do { 2573b0f4066SDimitry Andric ++MFI; 2583b0f4066SDimitry Andric Stop = LIS.getMBBEndIdx(MFI); 2593b0f4066SDimitry Andric } while (Stop <= LVI->start); 2603b0f4066SDimitry Andric } 261e580952dSDimitry Andric } 262e580952dSDimitry Andric 263dd6029ffSDimitry Andric bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const { 264dd6029ffSDimitry Andric unsigned OrigReg = VRM.getOriginal(CurLI->reg); 265dd6029ffSDimitry Andric const LiveInterval &Orig = LIS.getInterval(OrigReg); 266dd6029ffSDimitry Andric assert(!Orig.empty() && "Splitting empty interval?"); 267dd6029ffSDimitry Andric LiveInterval::const_iterator I = Orig.find(Idx); 268dd6029ffSDimitry Andric 269dd6029ffSDimitry Andric // Range containing Idx should begin at Idx. 270dd6029ffSDimitry Andric if (I != Orig.end() && I->start <= Idx) 271dd6029ffSDimitry Andric return I->start == Idx; 272dd6029ffSDimitry Andric 273dd6029ffSDimitry Andric // Range does not contain Idx, previous must end at Idx. 274dd6029ffSDimitry Andric return I != Orig.begin() && (--I)->end == Idx; 275dd6029ffSDimitry Andric } 276dd6029ffSDimitry Andric 277e580952dSDimitry Andric void SplitAnalysis::analyze(const LiveInterval *li) { 278e580952dSDimitry Andric clear(); 2792754fe60SDimitry Andric CurLI = li; 280e580952dSDimitry Andric analyzeUses(); 281e580952dSDimitry Andric } 282e580952dSDimitry Andric 283e580952dSDimitry Andric 284e580952dSDimitry Andric //===----------------------------------------------------------------------===// 2853b0f4066SDimitry Andric // Split Editor 286e580952dSDimitry Andric //===----------------------------------------------------------------------===// 287e580952dSDimitry Andric 2883b0f4066SDimitry Andric /// Create a new SplitEditor for editing the LiveInterval analyzed by SA. 2893b0f4066SDimitry Andric SplitEditor::SplitEditor(SplitAnalysis &sa, 2903b0f4066SDimitry Andric LiveIntervals &lis, 2913b0f4066SDimitry Andric VirtRegMap &vrm, 2923b0f4066SDimitry Andric MachineDominatorTree &mdt) 2933b0f4066SDimitry Andric : SA(sa), LIS(lis), VRM(vrm), 2943b0f4066SDimitry Andric MRI(vrm.getMachineFunction().getRegInfo()), 2953b0f4066SDimitry Andric MDT(mdt), 2963b0f4066SDimitry Andric TII(*vrm.getMachineFunction().getTarget().getInstrInfo()), 2973b0f4066SDimitry Andric TRI(*vrm.getMachineFunction().getTarget().getRegisterInfo()), 2983b0f4066SDimitry Andric Edit(0), 2993b0f4066SDimitry Andric OpenIdx(0), 3003b0f4066SDimitry Andric RegAssign(Allocator) 3013b0f4066SDimitry Andric {} 302e580952dSDimitry Andric 3033b0f4066SDimitry Andric void SplitEditor::reset(LiveRangeEdit &lre) { 3043b0f4066SDimitry Andric Edit = &lre; 3053b0f4066SDimitry Andric OpenIdx = 0; 3063b0f4066SDimitry Andric RegAssign.clear(); 3072754fe60SDimitry Andric Values.clear(); 3083b0f4066SDimitry Andric 3093b0f4066SDimitry Andric // We don't need to clear LiveOutCache, only LiveOutSeen entries are read. 3103b0f4066SDimitry Andric LiveOutSeen.clear(); 3113b0f4066SDimitry Andric 3123b0f4066SDimitry Andric // We don't need an AliasAnalysis since we will only be performing 3133b0f4066SDimitry Andric // cheap-as-a-copy remats anyway. 3143b0f4066SDimitry Andric Edit->anyRematerializable(LIS, TII, 0); 3152754fe60SDimitry Andric } 3162754fe60SDimitry Andric 3173b0f4066SDimitry Andric void SplitEditor::dump() const { 3183b0f4066SDimitry Andric if (RegAssign.empty()) { 3193b0f4066SDimitry Andric dbgs() << " empty\n"; 3203b0f4066SDimitry Andric return; 3212754fe60SDimitry Andric } 3222754fe60SDimitry Andric 3233b0f4066SDimitry Andric for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I) 3243b0f4066SDimitry Andric dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value(); 3253b0f4066SDimitry Andric dbgs() << '\n'; 3263b0f4066SDimitry Andric } 3273b0f4066SDimitry Andric 3283b0f4066SDimitry Andric VNInfo *SplitEditor::defValue(unsigned RegIdx, 3293b0f4066SDimitry Andric const VNInfo *ParentVNI, 3303b0f4066SDimitry Andric SlotIndex Idx) { 331e580952dSDimitry Andric assert(ParentVNI && "Mapping NULL value"); 332e580952dSDimitry Andric assert(Idx.isValid() && "Invalid SlotIndex"); 3333b0f4066SDimitry Andric assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI"); 3343b0f4066SDimitry Andric LiveInterval *LI = Edit->get(RegIdx); 3352754fe60SDimitry Andric 3362754fe60SDimitry Andric // Create a new value. 3372754fe60SDimitry Andric VNInfo *VNI = LI->getNextValue(Idx, 0, LIS.getVNInfoAllocator()); 3382754fe60SDimitry Andric 339e580952dSDimitry Andric // Use insert for lookup, so we can add missing values with a second lookup. 340e580952dSDimitry Andric std::pair<ValueMap::iterator, bool> InsP = 3413b0f4066SDimitry Andric Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), VNI)); 3422754fe60SDimitry Andric 3433b0f4066SDimitry Andric // This was the first time (RegIdx, ParentVNI) was mapped. 3443b0f4066SDimitry Andric // Keep it as a simple def without any liveness. 3453b0f4066SDimitry Andric if (InsP.second) 3463b0f4066SDimitry Andric return VNI; 3473b0f4066SDimitry Andric 3483b0f4066SDimitry Andric // If the previous value was a simple mapping, add liveness for it now. 3493b0f4066SDimitry Andric if (VNInfo *OldVNI = InsP.first->second) { 3503b0f4066SDimitry Andric SlotIndex Def = OldVNI->def; 3513b0f4066SDimitry Andric LI->addRange(LiveRange(Def, Def.getNextSlot(), OldVNI)); 3523b0f4066SDimitry Andric // No longer a simple mapping. 3532754fe60SDimitry Andric InsP.first->second = 0; 3543b0f4066SDimitry Andric } 3553b0f4066SDimitry Andric 3563b0f4066SDimitry Andric // This is a complex mapping, add liveness for VNI 3573b0f4066SDimitry Andric SlotIndex Def = VNI->def; 3583b0f4066SDimitry Andric LI->addRange(LiveRange(Def, Def.getNextSlot(), VNI)); 3592754fe60SDimitry Andric 3602754fe60SDimitry Andric return VNI; 3612754fe60SDimitry Andric } 3622754fe60SDimitry Andric 3633b0f4066SDimitry Andric void SplitEditor::markComplexMapped(unsigned RegIdx, const VNInfo *ParentVNI) { 3642754fe60SDimitry Andric assert(ParentVNI && "Mapping NULL value"); 3653b0f4066SDimitry Andric VNInfo *&VNI = Values[std::make_pair(RegIdx, ParentVNI->id)]; 3663b0f4066SDimitry Andric 3673b0f4066SDimitry Andric // ParentVNI was either unmapped or already complex mapped. Either way. 3683b0f4066SDimitry Andric if (!VNI) 3693b0f4066SDimitry Andric return; 3703b0f4066SDimitry Andric 3713b0f4066SDimitry Andric // This was previously a single mapping. Make sure the old def is represented 3723b0f4066SDimitry Andric // by a trivial live range. 3733b0f4066SDimitry Andric SlotIndex Def = VNI->def; 3743b0f4066SDimitry Andric Edit->get(RegIdx)->addRange(LiveRange(Def, Def.getNextSlot(), VNI)); 3753b0f4066SDimitry Andric VNI = 0; 3763b0f4066SDimitry Andric } 3773b0f4066SDimitry Andric 3783b0f4066SDimitry Andric // extendRange - Extend the live range to reach Idx. 3793b0f4066SDimitry Andric // Potentially create phi-def values. 3803b0f4066SDimitry Andric void SplitEditor::extendRange(unsigned RegIdx, SlotIndex Idx) { 3812754fe60SDimitry Andric assert(Idx.isValid() && "Invalid SlotIndex"); 3822754fe60SDimitry Andric MachineBasicBlock *IdxMBB = LIS.getMBBFromIndex(Idx); 383e580952dSDimitry Andric assert(IdxMBB && "No MBB at Idx"); 3843b0f4066SDimitry Andric LiveInterval *LI = Edit->get(RegIdx); 385e580952dSDimitry Andric 386e580952dSDimitry Andric // Is there a def in the same MBB we can extend? 3873b0f4066SDimitry Andric if (LI->extendInBlock(LIS.getMBBStartIdx(IdxMBB), Idx)) 3883b0f4066SDimitry Andric return; 389e580952dSDimitry Andric 390e580952dSDimitry Andric // Now for the fun part. We know that ParentVNI potentially has multiple defs, 391e580952dSDimitry Andric // and we may need to create even more phi-defs to preserve VNInfo SSA form. 3922754fe60SDimitry Andric // Perform a search for all predecessor blocks where we know the dominating 3933b0f4066SDimitry Andric // VNInfo. 3943b0f4066SDimitry Andric VNInfo *VNI = findReachingDefs(LI, IdxMBB, Idx.getNextSlot()); 3953b0f4066SDimitry Andric 3963b0f4066SDimitry Andric // When there were multiple different values, we may need new PHIs. 3973b0f4066SDimitry Andric if (!VNI) 3983b0f4066SDimitry Andric return updateSSA(); 3993b0f4066SDimitry Andric 4003b0f4066SDimitry Andric // Poor man's SSA update for the single-value case. 4013b0f4066SDimitry Andric LiveOutPair LOP(VNI, MDT[LIS.getMBBFromIndex(VNI->def)]); 4023b0f4066SDimitry Andric for (SmallVectorImpl<LiveInBlock>::iterator I = LiveInBlocks.begin(), 4033b0f4066SDimitry Andric E = LiveInBlocks.end(); I != E; ++I) { 4043b0f4066SDimitry Andric MachineBasicBlock *MBB = I->DomNode->getBlock(); 4053b0f4066SDimitry Andric SlotIndex Start = LIS.getMBBStartIdx(MBB); 4063b0f4066SDimitry Andric if (I->Kill.isValid()) 4073b0f4066SDimitry Andric LI->addRange(LiveRange(Start, I->Kill, VNI)); 4083b0f4066SDimitry Andric else { 4093b0f4066SDimitry Andric LiveOutCache[MBB] = LOP; 4103b0f4066SDimitry Andric LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI)); 4113b0f4066SDimitry Andric } 4123b0f4066SDimitry Andric } 4133b0f4066SDimitry Andric } 4143b0f4066SDimitry Andric 4153b0f4066SDimitry Andric /// findReachingDefs - Search the CFG for known live-out values. 4163b0f4066SDimitry Andric /// Add required live-in blocks to LiveInBlocks. 4173b0f4066SDimitry Andric VNInfo *SplitEditor::findReachingDefs(LiveInterval *LI, 4183b0f4066SDimitry Andric MachineBasicBlock *KillMBB, 4193b0f4066SDimitry Andric SlotIndex Kill) { 4203b0f4066SDimitry Andric // Initialize the live-out cache the first time it is needed. 4213b0f4066SDimitry Andric if (LiveOutSeen.empty()) { 4223b0f4066SDimitry Andric unsigned N = VRM.getMachineFunction().getNumBlockIDs(); 4233b0f4066SDimitry Andric LiveOutSeen.resize(N); 4243b0f4066SDimitry Andric LiveOutCache.resize(N); 4253b0f4066SDimitry Andric } 426e580952dSDimitry Andric 4272754fe60SDimitry Andric // Blocks where LI should be live-in. 4283b0f4066SDimitry Andric SmallVector<MachineBasicBlock*, 16> WorkList(1, KillMBB); 4293b0f4066SDimitry Andric 4303b0f4066SDimitry Andric // Remember if we have seen more than one value. 4313b0f4066SDimitry Andric bool UniqueVNI = true; 4323b0f4066SDimitry Andric VNInfo *TheVNI = 0; 433e580952dSDimitry Andric 4342754fe60SDimitry Andric // Using LiveOutCache as a visited set, perform a BFS for all reaching defs. 4353b0f4066SDimitry Andric for (unsigned i = 0; i != WorkList.size(); ++i) { 4363b0f4066SDimitry Andric MachineBasicBlock *MBB = WorkList[i]; 4373b0f4066SDimitry Andric assert(!MBB->pred_empty() && "Value live-in to entry block?"); 4382754fe60SDimitry Andric for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), 4392754fe60SDimitry Andric PE = MBB->pred_end(); PI != PE; ++PI) { 4402754fe60SDimitry Andric MachineBasicBlock *Pred = *PI; 4413b0f4066SDimitry Andric LiveOutPair &LOP = LiveOutCache[Pred]; 4423b0f4066SDimitry Andric 4432754fe60SDimitry Andric // Is this a known live-out block? 4443b0f4066SDimitry Andric if (LiveOutSeen.test(Pred->getNumber())) { 4453b0f4066SDimitry Andric if (VNInfo *VNI = LOP.first) { 4463b0f4066SDimitry Andric if (TheVNI && TheVNI != VNI) 4473b0f4066SDimitry Andric UniqueVNI = false; 4483b0f4066SDimitry Andric TheVNI = VNI; 4493b0f4066SDimitry Andric } 450e580952dSDimitry Andric continue; 451e580952dSDimitry Andric } 452e580952dSDimitry Andric 4533b0f4066SDimitry Andric // First time. LOP is garbage and must be cleared below. 4543b0f4066SDimitry Andric LiveOutSeen.set(Pred->getNumber()); 4553b0f4066SDimitry Andric 4562754fe60SDimitry Andric // Does Pred provide a live-out value? 4573b0f4066SDimitry Andric SlotIndex Start, Last; 4583b0f4066SDimitry Andric tie(Start, Last) = LIS.getSlotIndexes()->getMBBRange(Pred); 4593b0f4066SDimitry Andric Last = Last.getPrevSlot(); 4603b0f4066SDimitry Andric VNInfo *VNI = LI->extendInBlock(Start, Last); 4612754fe60SDimitry Andric LOP.first = VNI; 4623b0f4066SDimitry Andric if (VNI) { 4633b0f4066SDimitry Andric LOP.second = MDT[LIS.getMBBFromIndex(VNI->def)]; 4643b0f4066SDimitry Andric if (TheVNI && TheVNI != VNI) 4653b0f4066SDimitry Andric UniqueVNI = false; 4663b0f4066SDimitry Andric TheVNI = VNI; 467e580952dSDimitry Andric continue; 468e580952dSDimitry Andric } 4693b0f4066SDimitry Andric LOP.second = 0; 4703b0f4066SDimitry Andric 4712754fe60SDimitry Andric // No, we need a live-in value for Pred as well 4723b0f4066SDimitry Andric if (Pred != KillMBB) 4733b0f4066SDimitry Andric WorkList.push_back(Pred); 4743b0f4066SDimitry Andric else 4753b0f4066SDimitry Andric // Loopback to KillMBB, so value is really live through. 4763b0f4066SDimitry Andric Kill = SlotIndex(); 477e580952dSDimitry Andric } 478e580952dSDimitry Andric } 479e580952dSDimitry Andric 4803b0f4066SDimitry Andric // Transfer WorkList to LiveInBlocks in reverse order. 4813b0f4066SDimitry Andric // This ordering works best with updateSSA(). 4823b0f4066SDimitry Andric LiveInBlocks.clear(); 4833b0f4066SDimitry Andric LiveInBlocks.reserve(WorkList.size()); 4843b0f4066SDimitry Andric while(!WorkList.empty()) 4853b0f4066SDimitry Andric LiveInBlocks.push_back(MDT[WorkList.pop_back_val()]); 4863b0f4066SDimitry Andric 4873b0f4066SDimitry Andric // The kill block may not be live-through. 4883b0f4066SDimitry Andric assert(LiveInBlocks.back().DomNode->getBlock() == KillMBB); 4893b0f4066SDimitry Andric LiveInBlocks.back().Kill = Kill; 4903b0f4066SDimitry Andric 4913b0f4066SDimitry Andric return UniqueVNI ? TheVNI : 0; 4923b0f4066SDimitry Andric } 4933b0f4066SDimitry Andric 4943b0f4066SDimitry Andric void SplitEditor::updateSSA() { 4952754fe60SDimitry Andric // This is essentially the same iterative algorithm that SSAUpdater uses, 4962754fe60SDimitry Andric // except we already have a dominator tree, so we don't have to recompute it. 4972754fe60SDimitry Andric unsigned Changes; 4982754fe60SDimitry Andric do { 4992754fe60SDimitry Andric Changes = 0; 5003b0f4066SDimitry Andric // Propagate live-out values down the dominator tree, inserting phi-defs 5013b0f4066SDimitry Andric // when necessary. 5023b0f4066SDimitry Andric for (SmallVectorImpl<LiveInBlock>::iterator I = LiveInBlocks.begin(), 5033b0f4066SDimitry Andric E = LiveInBlocks.end(); I != E; ++I) { 5043b0f4066SDimitry Andric MachineDomTreeNode *Node = I->DomNode; 5053b0f4066SDimitry Andric // Skip block if the live-in value has already been determined. 5063b0f4066SDimitry Andric if (!Node) 5073b0f4066SDimitry Andric continue; 5082754fe60SDimitry Andric MachineBasicBlock *MBB = Node->getBlock(); 5092754fe60SDimitry Andric MachineDomTreeNode *IDom = Node->getIDom(); 5102754fe60SDimitry Andric LiveOutPair IDomValue; 5113b0f4066SDimitry Andric 5122754fe60SDimitry Andric // We need a live-in value to a block with no immediate dominator? 5132754fe60SDimitry Andric // This is probably an unreachable block that has survived somehow. 5143b0f4066SDimitry Andric bool needPHI = !IDom || !LiveOutSeen.test(IDom->getBlock()->getNumber()); 5152754fe60SDimitry Andric 5163b0f4066SDimitry Andric // IDom dominates all of our predecessors, but it may not be their 5173b0f4066SDimitry Andric // immediate dominator. Check if any of them have live-out values that are 5183b0f4066SDimitry Andric // properly dominated by IDom. If so, we need a phi-def here. 5192754fe60SDimitry Andric if (!needPHI) { 5203b0f4066SDimitry Andric IDomValue = LiveOutCache[IDom->getBlock()]; 5212754fe60SDimitry Andric for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), 5222754fe60SDimitry Andric PE = MBB->pred_end(); PI != PE; ++PI) { 5232754fe60SDimitry Andric LiveOutPair Value = LiveOutCache[*PI]; 5242754fe60SDimitry Andric if (!Value.first || Value.first == IDomValue.first) 5252754fe60SDimitry Andric continue; 5262754fe60SDimitry Andric // This predecessor is carrying something other than IDomValue. 5272754fe60SDimitry Andric // It could be because IDomValue hasn't propagated yet, or it could be 5282754fe60SDimitry Andric // because MBB is in the dominance frontier of that value. 5292754fe60SDimitry Andric if (MDT.dominates(IDom, Value.second)) { 5302754fe60SDimitry Andric needPHI = true; 5312754fe60SDimitry Andric break; 5322754fe60SDimitry Andric } 5332754fe60SDimitry Andric } 5342754fe60SDimitry Andric } 5352754fe60SDimitry Andric 5363b0f4066SDimitry Andric // The value may be live-through even if Kill is set, as can happen when 5373b0f4066SDimitry Andric // we are called from extendRange. In that case LiveOutSeen is true, and 5383b0f4066SDimitry Andric // LiveOutCache indicates a foreign or missing value. 5393b0f4066SDimitry Andric LiveOutPair &LOP = LiveOutCache[MBB]; 5403b0f4066SDimitry Andric 5412754fe60SDimitry Andric // Create a phi-def if required. 5422754fe60SDimitry Andric if (needPHI) { 5432754fe60SDimitry Andric ++Changes; 5442754fe60SDimitry Andric SlotIndex Start = LIS.getMBBStartIdx(MBB); 5453b0f4066SDimitry Andric unsigned RegIdx = RegAssign.lookup(Start); 5463b0f4066SDimitry Andric LiveInterval *LI = Edit->get(RegIdx); 5472754fe60SDimitry Andric VNInfo *VNI = LI->getNextValue(Start, 0, LIS.getVNInfoAllocator()); 5482754fe60SDimitry Andric VNI->setIsPHIDef(true); 5493b0f4066SDimitry Andric I->Value = VNI; 5503b0f4066SDimitry Andric // This block is done, we know the final value. 5513b0f4066SDimitry Andric I->DomNode = 0; 5523b0f4066SDimitry Andric if (I->Kill.isValid()) 5533b0f4066SDimitry Andric LI->addRange(LiveRange(Start, I->Kill, VNI)); 5543b0f4066SDimitry Andric else { 5552754fe60SDimitry Andric LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI)); 5563b0f4066SDimitry Andric LOP = LiveOutPair(VNI, Node); 5572754fe60SDimitry Andric } 5582754fe60SDimitry Andric } else if (IDomValue.first) { 5593b0f4066SDimitry Andric // No phi-def here. Remember incoming value. 5603b0f4066SDimitry Andric I->Value = IDomValue.first; 5613b0f4066SDimitry Andric if (I->Kill.isValid()) 5623b0f4066SDimitry Andric continue; 5632754fe60SDimitry Andric // Propagate IDomValue if needed: 5642754fe60SDimitry Andric // MBB is live-out and doesn't define its own value. 5653b0f4066SDimitry Andric if (LOP.second != Node && LOP.first != IDomValue.first) { 5662754fe60SDimitry Andric ++Changes; 5673b0f4066SDimitry Andric LOP = IDomValue; 5682754fe60SDimitry Andric } 5692754fe60SDimitry Andric } 5702754fe60SDimitry Andric } 5712754fe60SDimitry Andric } while (Changes); 5722754fe60SDimitry Andric 5733b0f4066SDimitry Andric // The values in LiveInBlocks are now accurate. No more phi-defs are needed 5742754fe60SDimitry Andric // for these blocks, so we can color the live ranges. 5753b0f4066SDimitry Andric for (SmallVectorImpl<LiveInBlock>::iterator I = LiveInBlocks.begin(), 5763b0f4066SDimitry Andric E = LiveInBlocks.end(); I != E; ++I) { 5773b0f4066SDimitry Andric if (!I->DomNode) 5783b0f4066SDimitry Andric continue; 5793b0f4066SDimitry Andric assert(I->Value && "No live-in value found"); 5803b0f4066SDimitry Andric MachineBasicBlock *MBB = I->DomNode->getBlock(); 5812754fe60SDimitry Andric SlotIndex Start = LIS.getMBBStartIdx(MBB); 5823b0f4066SDimitry Andric unsigned RegIdx = RegAssign.lookup(Start); 5833b0f4066SDimitry Andric LiveInterval *LI = Edit->get(RegIdx); 5843b0f4066SDimitry Andric LI->addRange(LiveRange(Start, I->Kill.isValid() ? 5853b0f4066SDimitry Andric I->Kill : LIS.getMBBEndIdx(MBB), I->Value)); 5862754fe60SDimitry Andric } 587e580952dSDimitry Andric } 588e580952dSDimitry Andric 5892754fe60SDimitry Andric VNInfo *SplitEditor::defFromParent(unsigned RegIdx, 5902754fe60SDimitry Andric VNInfo *ParentVNI, 5912754fe60SDimitry Andric SlotIndex UseIdx, 592e580952dSDimitry Andric MachineBasicBlock &MBB, 593e580952dSDimitry Andric MachineBasicBlock::iterator I) { 5942754fe60SDimitry Andric MachineInstr *CopyMI = 0; 5952754fe60SDimitry Andric SlotIndex Def; 5963b0f4066SDimitry Andric LiveInterval *LI = Edit->get(RegIdx); 5973b0f4066SDimitry Andric 5983b0f4066SDimitry Andric // We may be trying to avoid interference that ends at a deleted instruction, 5993b0f4066SDimitry Andric // so always begin RegIdx 0 early and all others late. 6003b0f4066SDimitry Andric bool Late = RegIdx != 0; 6012754fe60SDimitry Andric 6022754fe60SDimitry Andric // Attempt cheap-as-a-copy rematerialization. 6032754fe60SDimitry Andric LiveRangeEdit::Remat RM(ParentVNI); 6043b0f4066SDimitry Andric if (Edit->canRematerializeAt(RM, UseIdx, true, LIS)) { 6053b0f4066SDimitry Andric Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, LIS, TII, TRI, Late); 606bd5abe19SDimitry Andric ++NumRemats; 6072754fe60SDimitry Andric } else { 6082754fe60SDimitry Andric // Can't remat, just insert a copy from parent. 6092754fe60SDimitry Andric CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg) 6103b0f4066SDimitry Andric .addReg(Edit->getReg()); 6113b0f4066SDimitry Andric Def = LIS.getSlotIndexes()->insertMachineInstrInMaps(CopyMI, Late) 6123b0f4066SDimitry Andric .getDefIndex(); 613bd5abe19SDimitry Andric ++NumCopies; 6142754fe60SDimitry Andric } 6152754fe60SDimitry Andric 6162754fe60SDimitry Andric // Define the value in Reg. 6173b0f4066SDimitry Andric VNInfo *VNI = defValue(RegIdx, ParentVNI, Def); 6182754fe60SDimitry Andric VNI->setCopy(CopyMI); 6192754fe60SDimitry Andric return VNI; 620e580952dSDimitry Andric } 621e580952dSDimitry Andric 622e580952dSDimitry Andric /// Create a new virtual register and live interval. 6233b0f4066SDimitry Andric unsigned SplitEditor::openIntv() { 6242754fe60SDimitry Andric // Create the complement as index 0. 6253b0f4066SDimitry Andric if (Edit->empty()) 6263b0f4066SDimitry Andric Edit->create(LIS, VRM); 627e580952dSDimitry Andric 6282754fe60SDimitry Andric // Create the open interval. 6293b0f4066SDimitry Andric OpenIdx = Edit->size(); 6303b0f4066SDimitry Andric Edit->create(LIS, VRM); 6313b0f4066SDimitry Andric return OpenIdx; 6323b0f4066SDimitry Andric } 6333b0f4066SDimitry Andric 6343b0f4066SDimitry Andric void SplitEditor::selectIntv(unsigned Idx) { 6353b0f4066SDimitry Andric assert(Idx != 0 && "Cannot select the complement interval"); 6363b0f4066SDimitry Andric assert(Idx < Edit->size() && "Can only select previously opened interval"); 6373b0f4066SDimitry Andric OpenIdx = Idx; 6382754fe60SDimitry Andric } 639e580952dSDimitry Andric 6402754fe60SDimitry Andric SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) { 6412754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before enterIntvBefore"); 6422754fe60SDimitry Andric DEBUG(dbgs() << " enterIntvBefore " << Idx); 6432754fe60SDimitry Andric Idx = Idx.getBaseIndex(); 6443b0f4066SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx); 6452754fe60SDimitry Andric if (!ParentVNI) { 6462754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 6472754fe60SDimitry Andric return Idx; 6482754fe60SDimitry Andric } 6492754fe60SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 6502754fe60SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Idx); 651e580952dSDimitry Andric assert(MI && "enterIntvBefore called with invalid index"); 652e580952dSDimitry Andric 6532754fe60SDimitry Andric VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI); 6542754fe60SDimitry Andric return VNI->def; 655e580952dSDimitry Andric } 656e580952dSDimitry Andric 6572754fe60SDimitry Andric SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) { 6582754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before enterIntvAtEnd"); 6592754fe60SDimitry Andric SlotIndex End = LIS.getMBBEndIdx(&MBB); 6602754fe60SDimitry Andric SlotIndex Last = End.getPrevSlot(); 6612754fe60SDimitry Andric DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last); 6623b0f4066SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last); 6632754fe60SDimitry Andric if (!ParentVNI) { 6642754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 6652754fe60SDimitry Andric return End; 6662754fe60SDimitry Andric } 6672754fe60SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id); 6682754fe60SDimitry Andric VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB, 6693b0f4066SDimitry Andric LIS.getLastSplitPoint(Edit->getParent(), &MBB)); 6702754fe60SDimitry Andric RegAssign.insert(VNI->def, End, OpenIdx); 6712754fe60SDimitry Andric DEBUG(dump()); 6722754fe60SDimitry Andric return VNI->def; 673e580952dSDimitry Andric } 674e580952dSDimitry Andric 6752754fe60SDimitry Andric /// useIntv - indicate that all instructions in MBB should use OpenLI. 676e580952dSDimitry Andric void SplitEditor::useIntv(const MachineBasicBlock &MBB) { 6772754fe60SDimitry Andric useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB)); 678e580952dSDimitry Andric } 679e580952dSDimitry Andric 680e580952dSDimitry Andric void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) { 6812754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before useIntv"); 6822754fe60SDimitry Andric DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):"); 6832754fe60SDimitry Andric RegAssign.insert(Start, End, OpenIdx); 6842754fe60SDimitry Andric DEBUG(dump()); 685e580952dSDimitry Andric } 686e580952dSDimitry Andric 6872754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) { 6882754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before leaveIntvAfter"); 6892754fe60SDimitry Andric DEBUG(dbgs() << " leaveIntvAfter " << Idx); 6902754fe60SDimitry Andric 6912754fe60SDimitry Andric // The interval must be live beyond the instruction at Idx. 6922754fe60SDimitry Andric Idx = Idx.getBoundaryIndex(); 6933b0f4066SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx); 6942754fe60SDimitry Andric if (!ParentVNI) { 6952754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 6962754fe60SDimitry Andric return Idx.getNextSlot(); 6972754fe60SDimitry Andric } 6982754fe60SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 6992754fe60SDimitry Andric 7002754fe60SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Idx); 7012754fe60SDimitry Andric assert(MI && "No instruction at index"); 7022754fe60SDimitry Andric VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), 7032754fe60SDimitry Andric llvm::next(MachineBasicBlock::iterator(MI))); 7042754fe60SDimitry Andric return VNI->def; 705e580952dSDimitry Andric } 706e580952dSDimitry Andric 7072754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) { 7082754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before leaveIntvBefore"); 7092754fe60SDimitry Andric DEBUG(dbgs() << " leaveIntvBefore " << Idx); 710e580952dSDimitry Andric 7112754fe60SDimitry Andric // The interval must be live into the instruction at Idx. 7122754fe60SDimitry Andric Idx = Idx.getBoundaryIndex(); 7133b0f4066SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx); 7142754fe60SDimitry Andric if (!ParentVNI) { 7152754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 7162754fe60SDimitry Andric return Idx.getNextSlot(); 7172754fe60SDimitry Andric } 7182754fe60SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 7192754fe60SDimitry Andric 7202754fe60SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Idx); 7212754fe60SDimitry Andric assert(MI && "No instruction at index"); 7222754fe60SDimitry Andric VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI); 7232754fe60SDimitry Andric return VNI->def; 724e580952dSDimitry Andric } 725e580952dSDimitry Andric 7262754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) { 7272754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before leaveIntvAtTop"); 7282754fe60SDimitry Andric SlotIndex Start = LIS.getMBBStartIdx(&MBB); 7292754fe60SDimitry Andric DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start); 7302754fe60SDimitry Andric 7313b0f4066SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start); 7322754fe60SDimitry Andric if (!ParentVNI) { 7332754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 7342754fe60SDimitry Andric return Start; 735e580952dSDimitry Andric } 736e580952dSDimitry Andric 7372754fe60SDimitry Andric VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB, 7382754fe60SDimitry Andric MBB.SkipPHIsAndLabels(MBB.begin())); 7392754fe60SDimitry Andric RegAssign.insert(Start, VNI->def, OpenIdx); 7402754fe60SDimitry Andric DEBUG(dump()); 7412754fe60SDimitry Andric return VNI->def; 742e580952dSDimitry Andric } 743e580952dSDimitry Andric 7442754fe60SDimitry Andric void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) { 7452754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before overlapIntv"); 7463b0f4066SDimitry Andric const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start); 7473b0f4066SDimitry Andric assert(ParentVNI == Edit->getParent().getVNInfoAt(End.getPrevSlot()) && 7482754fe60SDimitry Andric "Parent changes value in extended range"); 7492754fe60SDimitry Andric assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) && 7502754fe60SDimitry Andric "Range cannot span basic blocks"); 751e580952dSDimitry Andric 7523b0f4066SDimitry Andric // The complement interval will be extended as needed by extendRange(). 7533b0f4066SDimitry Andric if (ParentVNI) 7543b0f4066SDimitry Andric markComplexMapped(0, ParentVNI); 7552754fe60SDimitry Andric DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):"); 7562754fe60SDimitry Andric RegAssign.insert(Start, End, OpenIdx); 7572754fe60SDimitry Andric DEBUG(dump()); 758e580952dSDimitry Andric } 759e580952dSDimitry Andric 7603b0f4066SDimitry Andric /// transferValues - Transfer all possible values to the new live ranges. 7613b0f4066SDimitry Andric /// Values that were rematerialized are left alone, they need extendRange(). 7623b0f4066SDimitry Andric bool SplitEditor::transferValues() { 7633b0f4066SDimitry Andric bool Skipped = false; 7643b0f4066SDimitry Andric LiveInBlocks.clear(); 7653b0f4066SDimitry Andric RegAssignMap::const_iterator AssignI = RegAssign.begin(); 7663b0f4066SDimitry Andric for (LiveInterval::const_iterator ParentI = Edit->getParent().begin(), 7673b0f4066SDimitry Andric ParentE = Edit->getParent().end(); ParentI != ParentE; ++ParentI) { 7683b0f4066SDimitry Andric DEBUG(dbgs() << " blit " << *ParentI << ':'); 7693b0f4066SDimitry Andric VNInfo *ParentVNI = ParentI->valno; 7703b0f4066SDimitry Andric // RegAssign has holes where RegIdx 0 should be used. 7713b0f4066SDimitry Andric SlotIndex Start = ParentI->start; 7723b0f4066SDimitry Andric AssignI.advanceTo(Start); 7733b0f4066SDimitry Andric do { 7743b0f4066SDimitry Andric unsigned RegIdx; 7753b0f4066SDimitry Andric SlotIndex End = ParentI->end; 7763b0f4066SDimitry Andric if (!AssignI.valid()) { 7773b0f4066SDimitry Andric RegIdx = 0; 7783b0f4066SDimitry Andric } else if (AssignI.start() <= Start) { 7793b0f4066SDimitry Andric RegIdx = AssignI.value(); 7803b0f4066SDimitry Andric if (AssignI.stop() < End) { 7813b0f4066SDimitry Andric End = AssignI.stop(); 7823b0f4066SDimitry Andric ++AssignI; 7833b0f4066SDimitry Andric } 7843b0f4066SDimitry Andric } else { 7853b0f4066SDimitry Andric RegIdx = 0; 7863b0f4066SDimitry Andric End = std::min(End, AssignI.start()); 787e580952dSDimitry Andric } 788e580952dSDimitry Andric 7893b0f4066SDimitry Andric // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI. 7903b0f4066SDimitry Andric DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx); 7913b0f4066SDimitry Andric LiveInterval *LI = Edit->get(RegIdx); 7923b0f4066SDimitry Andric 7933b0f4066SDimitry Andric // Check for a simply defined value that can be blitted directly. 7943b0f4066SDimitry Andric if (VNInfo *VNI = Values.lookup(std::make_pair(RegIdx, ParentVNI->id))) { 7953b0f4066SDimitry Andric DEBUG(dbgs() << ':' << VNI->id); 7963b0f4066SDimitry Andric LI->addRange(LiveRange(Start, End, VNI)); 7973b0f4066SDimitry Andric Start = End; 7983b0f4066SDimitry Andric continue; 7993b0f4066SDimitry Andric } 8003b0f4066SDimitry Andric 8013b0f4066SDimitry Andric // Skip rematerialized values, we need to use extendRange() and 8023b0f4066SDimitry Andric // extendPHIKillRanges() to completely recompute the live ranges. 8033b0f4066SDimitry Andric if (Edit->didRematerialize(ParentVNI)) { 8043b0f4066SDimitry Andric DEBUG(dbgs() << "(remat)"); 8053b0f4066SDimitry Andric Skipped = true; 8063b0f4066SDimitry Andric Start = End; 8073b0f4066SDimitry Andric continue; 8083b0f4066SDimitry Andric } 8093b0f4066SDimitry Andric 8103b0f4066SDimitry Andric // Initialize the live-out cache the first time it is needed. 8113b0f4066SDimitry Andric if (LiveOutSeen.empty()) { 8123b0f4066SDimitry Andric unsigned N = VRM.getMachineFunction().getNumBlockIDs(); 8133b0f4066SDimitry Andric LiveOutSeen.resize(N); 8143b0f4066SDimitry Andric LiveOutCache.resize(N); 8153b0f4066SDimitry Andric } 8163b0f4066SDimitry Andric 8173b0f4066SDimitry Andric // This value has multiple defs in RegIdx, but it wasn't rematerialized, 8183b0f4066SDimitry Andric // so the live range is accurate. Add live-in blocks in [Start;End) to the 8193b0f4066SDimitry Andric // LiveInBlocks. 8203b0f4066SDimitry Andric MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start); 8213b0f4066SDimitry Andric SlotIndex BlockStart, BlockEnd; 8223b0f4066SDimitry Andric tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(MBB); 8233b0f4066SDimitry Andric 8243b0f4066SDimitry Andric // The first block may be live-in, or it may have its own def. 8253b0f4066SDimitry Andric if (Start != BlockStart) { 8263b0f4066SDimitry Andric VNInfo *VNI = LI->extendInBlock(BlockStart, 8273b0f4066SDimitry Andric std::min(BlockEnd, End).getPrevSlot()); 8283b0f4066SDimitry Andric assert(VNI && "Missing def for complex mapped value"); 8293b0f4066SDimitry Andric DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber()); 8303b0f4066SDimitry Andric // MBB has its own def. Is it also live-out? 8313b0f4066SDimitry Andric if (BlockEnd <= End) { 8323b0f4066SDimitry Andric LiveOutSeen.set(MBB->getNumber()); 8333b0f4066SDimitry Andric LiveOutCache[MBB] = LiveOutPair(VNI, MDT[MBB]); 8343b0f4066SDimitry Andric } 8353b0f4066SDimitry Andric // Skip to the next block for live-in. 8363b0f4066SDimitry Andric ++MBB; 8373b0f4066SDimitry Andric BlockStart = BlockEnd; 8383b0f4066SDimitry Andric } 8393b0f4066SDimitry Andric 8403b0f4066SDimitry Andric // Handle the live-in blocks covered by [Start;End). 8413b0f4066SDimitry Andric assert(Start <= BlockStart && "Expected live-in block"); 8423b0f4066SDimitry Andric while (BlockStart < End) { 8433b0f4066SDimitry Andric DEBUG(dbgs() << ">BB#" << MBB->getNumber()); 8443b0f4066SDimitry Andric BlockEnd = LIS.getMBBEndIdx(MBB); 8453b0f4066SDimitry Andric if (BlockStart == ParentVNI->def) { 8463b0f4066SDimitry Andric // This block has the def of a parent PHI, so it isn't live-in. 8473b0f4066SDimitry Andric assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?"); 8483b0f4066SDimitry Andric VNInfo *VNI = LI->extendInBlock(BlockStart, 8493b0f4066SDimitry Andric std::min(BlockEnd, End).getPrevSlot()); 8503b0f4066SDimitry Andric assert(VNI && "Missing def for complex mapped parent PHI"); 8513b0f4066SDimitry Andric if (End >= BlockEnd) { 8523b0f4066SDimitry Andric // Live-out as well. 8533b0f4066SDimitry Andric LiveOutSeen.set(MBB->getNumber()); 8543b0f4066SDimitry Andric LiveOutCache[MBB] = LiveOutPair(VNI, MDT[MBB]); 8553b0f4066SDimitry Andric } 8563b0f4066SDimitry Andric } else { 8573b0f4066SDimitry Andric // This block needs a live-in value. 8583b0f4066SDimitry Andric LiveInBlocks.push_back(MDT[MBB]); 8593b0f4066SDimitry Andric // The last block covered may not be live-out. 8603b0f4066SDimitry Andric if (End < BlockEnd) 8613b0f4066SDimitry Andric LiveInBlocks.back().Kill = End; 8623b0f4066SDimitry Andric else { 8633b0f4066SDimitry Andric // Live-out, but we need updateSSA to tell us the value. 8643b0f4066SDimitry Andric LiveOutSeen.set(MBB->getNumber()); 8653b0f4066SDimitry Andric LiveOutCache[MBB] = LiveOutPair((VNInfo*)0, 8663b0f4066SDimitry Andric (MachineDomTreeNode*)0); 8673b0f4066SDimitry Andric } 8683b0f4066SDimitry Andric } 8693b0f4066SDimitry Andric BlockStart = BlockEnd; 8703b0f4066SDimitry Andric ++MBB; 8713b0f4066SDimitry Andric } 8723b0f4066SDimitry Andric Start = End; 8733b0f4066SDimitry Andric } while (Start != ParentI->end); 8743b0f4066SDimitry Andric DEBUG(dbgs() << '\n'); 8753b0f4066SDimitry Andric } 8763b0f4066SDimitry Andric 8773b0f4066SDimitry Andric if (!LiveInBlocks.empty()) 8783b0f4066SDimitry Andric updateSSA(); 8793b0f4066SDimitry Andric 8803b0f4066SDimitry Andric return Skipped; 8813b0f4066SDimitry Andric } 8823b0f4066SDimitry Andric 8833b0f4066SDimitry Andric void SplitEditor::extendPHIKillRanges() { 8843b0f4066SDimitry Andric // Extend live ranges to be live-out for successor PHI values. 8853b0f4066SDimitry Andric for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(), 8863b0f4066SDimitry Andric E = Edit->getParent().vni_end(); I != E; ++I) { 8873b0f4066SDimitry Andric const VNInfo *PHIVNI = *I; 8883b0f4066SDimitry Andric if (PHIVNI->isUnused() || !PHIVNI->isPHIDef()) 8893b0f4066SDimitry Andric continue; 8903b0f4066SDimitry Andric unsigned RegIdx = RegAssign.lookup(PHIVNI->def); 8913b0f4066SDimitry Andric MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def); 8923b0f4066SDimitry Andric for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), 8933b0f4066SDimitry Andric PE = MBB->pred_end(); PI != PE; ++PI) { 8943b0f4066SDimitry Andric SlotIndex End = LIS.getMBBEndIdx(*PI).getPrevSlot(); 8953b0f4066SDimitry Andric // The predecessor may not have a live-out value. That is OK, like an 8963b0f4066SDimitry Andric // undef PHI operand. 8973b0f4066SDimitry Andric if (Edit->getParent().liveAt(End)) { 8983b0f4066SDimitry Andric assert(RegAssign.lookup(End) == RegIdx && 8993b0f4066SDimitry Andric "Different register assignment in phi predecessor"); 9003b0f4066SDimitry Andric extendRange(RegIdx, End); 9013b0f4066SDimitry Andric } 9023b0f4066SDimitry Andric } 9033b0f4066SDimitry Andric } 9043b0f4066SDimitry Andric } 9053b0f4066SDimitry Andric 9063b0f4066SDimitry Andric /// rewriteAssigned - Rewrite all uses of Edit->getReg(). 9073b0f4066SDimitry Andric void SplitEditor::rewriteAssigned(bool ExtendRanges) { 9083b0f4066SDimitry Andric for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()), 9092754fe60SDimitry Andric RE = MRI.reg_end(); RI != RE;) { 910e580952dSDimitry Andric MachineOperand &MO = RI.getOperand(); 911e580952dSDimitry Andric MachineInstr *MI = MO.getParent(); 912e580952dSDimitry Andric ++RI; 9132754fe60SDimitry Andric // LiveDebugVariables should have handled all DBG_VALUE instructions. 914e580952dSDimitry Andric if (MI->isDebugValue()) { 915e580952dSDimitry Andric DEBUG(dbgs() << "Zapping " << *MI); 916e580952dSDimitry Andric MO.setReg(0); 917e580952dSDimitry Andric continue; 918e580952dSDimitry Andric } 9192754fe60SDimitry Andric 9202754fe60SDimitry Andric // <undef> operands don't really read the register, so just assign them to 9212754fe60SDimitry Andric // the complement. 9222754fe60SDimitry Andric if (MO.isUse() && MO.isUndef()) { 9233b0f4066SDimitry Andric MO.setReg(Edit->get(0)->reg); 9242754fe60SDimitry Andric continue; 9252754fe60SDimitry Andric } 9262754fe60SDimitry Andric 9272754fe60SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(MI); 9283b0f4066SDimitry Andric if (MO.isDef()) 9293b0f4066SDimitry Andric Idx = MO.isEarlyClobber() ? Idx.getUseIndex() : Idx.getDefIndex(); 9302754fe60SDimitry Andric 9312754fe60SDimitry Andric // Rewrite to the mapped register at Idx. 9322754fe60SDimitry Andric unsigned RegIdx = RegAssign.lookup(Idx); 9333b0f4066SDimitry Andric MO.setReg(Edit->get(RegIdx)->reg); 9342754fe60SDimitry Andric DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t' 9352754fe60SDimitry Andric << Idx << ':' << RegIdx << '\t' << *MI); 9362754fe60SDimitry Andric 9373b0f4066SDimitry Andric // Extend liveness to Idx if the instruction reads reg. 9383b0f4066SDimitry Andric if (!ExtendRanges) 9392754fe60SDimitry Andric continue; 9403b0f4066SDimitry Andric 9413b0f4066SDimitry Andric // Skip instructions that don't read Reg. 9423b0f4066SDimitry Andric if (MO.isDef()) { 9433b0f4066SDimitry Andric if (!MO.getSubReg() && !MO.isEarlyClobber()) 9443b0f4066SDimitry Andric continue; 9453b0f4066SDimitry Andric // We may wan't to extend a live range for a partial redef, or for a use 9463b0f4066SDimitry Andric // tied to an early clobber. 9473b0f4066SDimitry Andric Idx = Idx.getPrevSlot(); 9483b0f4066SDimitry Andric if (!Edit->getParent().liveAt(Idx)) 9493b0f4066SDimitry Andric continue; 9503b0f4066SDimitry Andric } else 9513b0f4066SDimitry Andric Idx = Idx.getUseIndex(); 9523b0f4066SDimitry Andric 9533b0f4066SDimitry Andric extendRange(RegIdx, Idx); 954e580952dSDimitry Andric } 955e580952dSDimitry Andric } 956e580952dSDimitry Andric 9573b0f4066SDimitry Andric void SplitEditor::deleteRematVictims() { 9583b0f4066SDimitry Andric SmallVector<MachineInstr*, 8> Dead; 9593b0f4066SDimitry Andric for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){ 9603b0f4066SDimitry Andric LiveInterval *LI = *I; 9613b0f4066SDimitry Andric for (LiveInterval::const_iterator LII = LI->begin(), LIE = LI->end(); 9623b0f4066SDimitry Andric LII != LIE; ++LII) { 9633b0f4066SDimitry Andric // Dead defs end at the store slot. 9643b0f4066SDimitry Andric if (LII->end != LII->valno->def.getNextSlot()) 9653b0f4066SDimitry Andric continue; 9663b0f4066SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(LII->valno->def); 9673b0f4066SDimitry Andric assert(MI && "Missing instruction for dead def"); 9683b0f4066SDimitry Andric MI->addRegisterDead(LI->reg, &TRI); 9693b0f4066SDimitry Andric 9703b0f4066SDimitry Andric if (!MI->allDefsAreDead()) 9713b0f4066SDimitry Andric continue; 9723b0f4066SDimitry Andric 9733b0f4066SDimitry Andric DEBUG(dbgs() << "All defs dead: " << *MI); 9743b0f4066SDimitry Andric Dead.push_back(MI); 9753b0f4066SDimitry Andric } 9763b0f4066SDimitry Andric } 9773b0f4066SDimitry Andric 9783b0f4066SDimitry Andric if (Dead.empty()) 9793b0f4066SDimitry Andric return; 9803b0f4066SDimitry Andric 9813b0f4066SDimitry Andric Edit->eliminateDeadDefs(Dead, LIS, VRM, TII); 9823b0f4066SDimitry Andric } 9833b0f4066SDimitry Andric 9843b0f4066SDimitry Andric void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) { 9853b0f4066SDimitry Andric ++NumFinished; 9862754fe60SDimitry Andric 9872754fe60SDimitry Andric // At this point, the live intervals in Edit contain VNInfos corresponding to 9882754fe60SDimitry Andric // the inserted copies. 9892754fe60SDimitry Andric 9902754fe60SDimitry Andric // Add the original defs from the parent interval. 9913b0f4066SDimitry Andric for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(), 9923b0f4066SDimitry Andric E = Edit->getParent().vni_end(); I != E; ++I) { 9932754fe60SDimitry Andric const VNInfo *ParentVNI = *I; 9942754fe60SDimitry Andric if (ParentVNI->isUnused()) 9952754fe60SDimitry Andric continue; 9963b0f4066SDimitry Andric unsigned RegIdx = RegAssign.lookup(ParentVNI->def); 9973b0f4066SDimitry Andric VNInfo *VNI = defValue(RegIdx, ParentVNI, ParentVNI->def); 9983b0f4066SDimitry Andric VNI->setIsPHIDef(ParentVNI->isPHIDef()); 9993b0f4066SDimitry Andric VNI->setCopy(ParentVNI->getCopy()); 10003b0f4066SDimitry Andric 10013b0f4066SDimitry Andric // Mark rematted values as complex everywhere to force liveness computation. 10023b0f4066SDimitry Andric // The new live ranges may be truncated. 10033b0f4066SDimitry Andric if (Edit->didRematerialize(ParentVNI)) 10043b0f4066SDimitry Andric for (unsigned i = 0, e = Edit->size(); i != e; ++i) 10053b0f4066SDimitry Andric markComplexMapped(i, ParentVNI); 10062754fe60SDimitry Andric } 10072754fe60SDimitry Andric 10082754fe60SDimitry Andric #ifndef NDEBUG 10092754fe60SDimitry Andric // Every new interval must have a def by now, otherwise the split is bogus. 10103b0f4066SDimitry Andric for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I) 10112754fe60SDimitry Andric assert((*I)->hasAtLeastOneValue() && "Split interval has no value"); 10122754fe60SDimitry Andric #endif 10132754fe60SDimitry Andric 10143b0f4066SDimitry Andric // Transfer the simply mapped values, check if any are skipped. 10153b0f4066SDimitry Andric bool Skipped = transferValues(); 10163b0f4066SDimitry Andric if (Skipped) 10173b0f4066SDimitry Andric extendPHIKillRanges(); 10182754fe60SDimitry Andric else 10193b0f4066SDimitry Andric ++NumSimple; 10202754fe60SDimitry Andric 10213b0f4066SDimitry Andric // Rewrite virtual registers, possibly extending ranges. 10223b0f4066SDimitry Andric rewriteAssigned(Skipped); 10232754fe60SDimitry Andric 10243b0f4066SDimitry Andric // Delete defs that were rematted everywhere. 10253b0f4066SDimitry Andric if (Skipped) 10263b0f4066SDimitry Andric deleteRematVictims(); 10272754fe60SDimitry Andric 10282754fe60SDimitry Andric // Get rid of unused values and set phi-kill flags. 10293b0f4066SDimitry Andric for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I) 10302754fe60SDimitry Andric (*I)->RenumberValues(LIS); 10312754fe60SDimitry Andric 10323b0f4066SDimitry Andric // Provide a reverse mapping from original indices to Edit ranges. 10333b0f4066SDimitry Andric if (LRMap) { 10343b0f4066SDimitry Andric LRMap->clear(); 10353b0f4066SDimitry Andric for (unsigned i = 0, e = Edit->size(); i != e; ++i) 10363b0f4066SDimitry Andric LRMap->push_back(i); 10373b0f4066SDimitry Andric } 10383b0f4066SDimitry Andric 10392754fe60SDimitry Andric // Now check if any registers were separated into multiple components. 10402754fe60SDimitry Andric ConnectedVNInfoEqClasses ConEQ(LIS); 10413b0f4066SDimitry Andric for (unsigned i = 0, e = Edit->size(); i != e; ++i) { 10422754fe60SDimitry Andric // Don't use iterators, they are invalidated by create() below. 10433b0f4066SDimitry Andric LiveInterval *li = Edit->get(i); 10442754fe60SDimitry Andric unsigned NumComp = ConEQ.Classify(li); 10452754fe60SDimitry Andric if (NumComp <= 1) 10462754fe60SDimitry Andric continue; 10472754fe60SDimitry Andric DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n'); 10482754fe60SDimitry Andric SmallVector<LiveInterval*, 8> dups; 10492754fe60SDimitry Andric dups.push_back(li); 10503b0f4066SDimitry Andric for (unsigned j = 1; j != NumComp; ++j) 10513b0f4066SDimitry Andric dups.push_back(&Edit->create(LIS, VRM)); 10523b0f4066SDimitry Andric ConEQ.Distribute(&dups[0], MRI); 10533b0f4066SDimitry Andric // The new intervals all map back to i. 10543b0f4066SDimitry Andric if (LRMap) 10553b0f4066SDimitry Andric LRMap->resize(Edit->size(), i); 10562754fe60SDimitry Andric } 10572754fe60SDimitry Andric 1058e580952dSDimitry Andric // Calculate spill weight and allocation hints for new intervals. 10593b0f4066SDimitry Andric Edit->calculateRegClassAndHint(VRM.getMachineFunction(), LIS, SA.Loops); 10603b0f4066SDimitry Andric 10613b0f4066SDimitry Andric assert(!LRMap || LRMap->size() == Edit->size()); 1062e580952dSDimitry Andric } 1063e580952dSDimitry Andric 1064e580952dSDimitry Andric 1065e580952dSDimitry Andric //===----------------------------------------------------------------------===// 1066e580952dSDimitry Andric // Single Block Splitting 1067e580952dSDimitry Andric //===----------------------------------------------------------------------===// 1068e580952dSDimitry Andric 10692754fe60SDimitry Andric /// getMultiUseBlocks - if CurLI has more than one use in a basic block, it 10702754fe60SDimitry Andric /// may be an advantage to split CurLI for the duration of the block. 10712754fe60SDimitry Andric bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) { 10722754fe60SDimitry Andric // If CurLI is local to one block, there is no point to splitting it. 10733b0f4066SDimitry Andric if (UseBlocks.size() <= 1) 10742754fe60SDimitry Andric return false; 10752754fe60SDimitry Andric // Add blocks with multiple uses. 10763b0f4066SDimitry Andric for (unsigned i = 0, e = UseBlocks.size(); i != e; ++i) { 10773b0f4066SDimitry Andric const BlockInfo &BI = UseBlocks[i]; 10783b0f4066SDimitry Andric if (BI.FirstUse == BI.LastUse) 10792754fe60SDimitry Andric continue; 10802754fe60SDimitry Andric Blocks.insert(BI.MBB); 10812754fe60SDimitry Andric } 10822754fe60SDimitry Andric return !Blocks.empty(); 1083e580952dSDimitry Andric } 1084e580952dSDimitry Andric 10853b0f4066SDimitry Andric void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) { 10863b0f4066SDimitry Andric openIntv(); 10873b0f4066SDimitry Andric SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber()); 10883b0f4066SDimitry Andric SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstUse, 10893b0f4066SDimitry Andric LastSplitPoint)); 10903b0f4066SDimitry Andric if (!BI.LiveOut || BI.LastUse < LastSplitPoint) { 10913b0f4066SDimitry Andric useIntv(SegStart, leaveIntvAfter(BI.LastUse)); 10923b0f4066SDimitry Andric } else { 10933b0f4066SDimitry Andric // The last use is after the last valid split point. 10943b0f4066SDimitry Andric SlotIndex SegStop = leaveIntvBefore(LastSplitPoint); 10953b0f4066SDimitry Andric useIntv(SegStart, SegStop); 10963b0f4066SDimitry Andric overlapIntv(SegStop, BI.LastUse); 10973b0f4066SDimitry Andric } 10983b0f4066SDimitry Andric } 10993b0f4066SDimitry Andric 11002754fe60SDimitry Andric /// splitSingleBlocks - Split CurLI into a separate live interval inside each 11012754fe60SDimitry Andric /// basic block in Blocks. 11022754fe60SDimitry Andric void SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) { 11032754fe60SDimitry Andric DEBUG(dbgs() << " splitSingleBlocks for " << Blocks.size() << " blocks.\n"); 11043b0f4066SDimitry Andric ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA.getUseBlocks(); 11053b0f4066SDimitry Andric for (unsigned i = 0; i != UseBlocks.size(); ++i) { 11063b0f4066SDimitry Andric const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; 11073b0f4066SDimitry Andric if (Blocks.count(BI.MBB)) 11083b0f4066SDimitry Andric splitSingleBlock(BI); 11092754fe60SDimitry Andric } 11102754fe60SDimitry Andric finish(); 1111e580952dSDimitry Andric } 1112