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" 19e580952dSDimitry Andric #include "llvm/CodeGen/CalcSpillWeights.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/CommandLine.h" 25e580952dSDimitry Andric #include "llvm/Support/Debug.h" 26e580952dSDimitry Andric #include "llvm/Support/raw_ostream.h" 27e580952dSDimitry Andric #include "llvm/Target/TargetInstrInfo.h" 28e580952dSDimitry Andric #include "llvm/Target/TargetMachine.h" 29e580952dSDimitry Andric 30e580952dSDimitry Andric using namespace llvm; 31e580952dSDimitry Andric 32e580952dSDimitry Andric static cl::opt<bool> 33e580952dSDimitry Andric AllowSplit("spiller-splits-edges", 34e580952dSDimitry Andric cl::desc("Allow critical edge splitting during spilling")); 35e580952dSDimitry Andric 36e580952dSDimitry Andric //===----------------------------------------------------------------------===// 37e580952dSDimitry Andric // Split Analysis 38e580952dSDimitry Andric //===----------------------------------------------------------------------===// 39e580952dSDimitry Andric 402754fe60SDimitry Andric SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, 41e580952dSDimitry Andric const LiveIntervals &lis, 42e580952dSDimitry Andric const MachineLoopInfo &mli) 432754fe60SDimitry Andric : MF(vrm.getMachineFunction()), 442754fe60SDimitry Andric VRM(vrm), 452754fe60SDimitry Andric LIS(lis), 462754fe60SDimitry Andric Loops(mli), 472754fe60SDimitry Andric TII(*MF.getTarget().getInstrInfo()), 482754fe60SDimitry Andric CurLI(0) {} 49e580952dSDimitry Andric 50e580952dSDimitry Andric void SplitAnalysis::clear() { 512754fe60SDimitry Andric UseSlots.clear(); 522754fe60SDimitry Andric UsingInstrs.clear(); 532754fe60SDimitry Andric UsingBlocks.clear(); 542754fe60SDimitry Andric LiveBlocks.clear(); 552754fe60SDimitry Andric CurLI = 0; 56e580952dSDimitry Andric } 57e580952dSDimitry Andric 58e580952dSDimitry Andric bool SplitAnalysis::canAnalyzeBranch(const MachineBasicBlock *MBB) { 59e580952dSDimitry Andric MachineBasicBlock *T, *F; 60e580952dSDimitry Andric SmallVector<MachineOperand, 4> Cond; 612754fe60SDimitry Andric return !TII.AnalyzeBranch(const_cast<MachineBasicBlock&>(*MBB), T, F, Cond); 62e580952dSDimitry Andric } 63e580952dSDimitry Andric 642754fe60SDimitry Andric /// analyzeUses - Count instructions, basic blocks, and loops using CurLI. 65e580952dSDimitry Andric void SplitAnalysis::analyzeUses() { 662754fe60SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 672754fe60SDimitry Andric for (MachineRegisterInfo::reg_iterator I = MRI.reg_begin(CurLI->reg), 682754fe60SDimitry Andric E = MRI.reg_end(); I != E; ++I) { 692754fe60SDimitry Andric MachineOperand &MO = I.getOperand(); 702754fe60SDimitry Andric if (MO.isUse() && MO.isUndef()) 71e580952dSDimitry Andric continue; 722754fe60SDimitry Andric MachineInstr *MI = MO.getParent(); 732754fe60SDimitry Andric if (MI->isDebugValue() || !UsingInstrs.insert(MI)) 742754fe60SDimitry Andric continue; 752754fe60SDimitry Andric UseSlots.push_back(LIS.getInstructionIndex(MI).getDefIndex()); 76e580952dSDimitry Andric MachineBasicBlock *MBB = MI->getParent(); 772754fe60SDimitry Andric UsingBlocks[MBB]++; 78e580952dSDimitry Andric } 792754fe60SDimitry Andric array_pod_sort(UseSlots.begin(), UseSlots.end()); 802754fe60SDimitry Andric calcLiveBlockInfo(); 81e580952dSDimitry Andric DEBUG(dbgs() << " counted " 822754fe60SDimitry Andric << UsingInstrs.size() << " instrs, " 832754fe60SDimitry Andric << UsingBlocks.size() << " blocks.\n"); 84e580952dSDimitry Andric } 85e580952dSDimitry Andric 862754fe60SDimitry Andric /// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks 872754fe60SDimitry Andric /// where CurLI is live. 882754fe60SDimitry Andric void SplitAnalysis::calcLiveBlockInfo() { 892754fe60SDimitry Andric if (CurLI->empty()) 90e580952dSDimitry Andric return; 91e580952dSDimitry Andric 922754fe60SDimitry Andric LiveInterval::const_iterator LVI = CurLI->begin(); 932754fe60SDimitry Andric LiveInterval::const_iterator LVE = CurLI->end(); 94e580952dSDimitry Andric 952754fe60SDimitry Andric SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE; 962754fe60SDimitry Andric UseI = UseSlots.begin(); 972754fe60SDimitry Andric UseE = UseSlots.end(); 982754fe60SDimitry Andric 992754fe60SDimitry Andric // Loop over basic blocks where CurLI is live. 1002754fe60SDimitry Andric MachineFunction::iterator MFI = LIS.getMBBFromIndex(LVI->start); 1012754fe60SDimitry Andric for (;;) { 1022754fe60SDimitry Andric BlockInfo BI; 1032754fe60SDimitry Andric BI.MBB = MFI; 1042754fe60SDimitry Andric SlotIndex Start, Stop; 1052754fe60SDimitry Andric tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB); 1062754fe60SDimitry Andric 1072754fe60SDimitry Andric // The last split point is the latest possible insertion point that dominates 1082754fe60SDimitry Andric // all successor blocks. If interference reaches LastSplitPoint, it is not 1092754fe60SDimitry Andric // possible to insert a split or reload that makes CurLI live in the 1102754fe60SDimitry Andric // outgoing bundle. 1112754fe60SDimitry Andric MachineBasicBlock::iterator LSP = LIS.getLastSplitPoint(*CurLI, BI.MBB); 1122754fe60SDimitry Andric if (LSP == BI.MBB->end()) 1132754fe60SDimitry Andric BI.LastSplitPoint = Stop; 1142754fe60SDimitry Andric else 1152754fe60SDimitry Andric BI.LastSplitPoint = LIS.getInstructionIndex(LSP); 1162754fe60SDimitry Andric 1172754fe60SDimitry Andric // LVI is the first live segment overlapping MBB. 1182754fe60SDimitry Andric BI.LiveIn = LVI->start <= Start; 1192754fe60SDimitry Andric if (!BI.LiveIn) 1202754fe60SDimitry Andric BI.Def = LVI->start; 1212754fe60SDimitry Andric 1222754fe60SDimitry Andric // Find the first and last uses in the block. 1232754fe60SDimitry Andric BI.Uses = hasUses(MFI); 1242754fe60SDimitry Andric if (BI.Uses && UseI != UseE) { 1252754fe60SDimitry Andric BI.FirstUse = *UseI; 1262754fe60SDimitry Andric assert(BI.FirstUse >= Start); 1272754fe60SDimitry Andric do ++UseI; 1282754fe60SDimitry Andric while (UseI != UseE && *UseI < Stop); 1292754fe60SDimitry Andric BI.LastUse = UseI[-1]; 1302754fe60SDimitry Andric assert(BI.LastUse < Stop); 131e580952dSDimitry Andric } 132e580952dSDimitry Andric 1332754fe60SDimitry Andric // Look for gaps in the live range. 1342754fe60SDimitry Andric bool hasGap = false; 1352754fe60SDimitry Andric BI.LiveOut = true; 1362754fe60SDimitry Andric while (LVI->end < Stop) { 1372754fe60SDimitry Andric SlotIndex LastStop = LVI->end; 1382754fe60SDimitry Andric if (++LVI == LVE || LVI->start >= Stop) { 1392754fe60SDimitry Andric BI.Kill = LastStop; 1402754fe60SDimitry Andric BI.LiveOut = false; 141e580952dSDimitry Andric break; 142e580952dSDimitry Andric } 1432754fe60SDimitry Andric if (LastStop < LVI->start) { 1442754fe60SDimitry Andric hasGap = true; 1452754fe60SDimitry Andric BI.Kill = LastStop; 1462754fe60SDimitry Andric BI.Def = LVI->start; 147e580952dSDimitry Andric } 148e580952dSDimitry Andric } 149e580952dSDimitry Andric 1502754fe60SDimitry Andric // Don't set LiveThrough when the block has a gap. 1512754fe60SDimitry Andric BI.LiveThrough = !hasGap && BI.LiveIn && BI.LiveOut; 1522754fe60SDimitry Andric LiveBlocks.push_back(BI); 153e580952dSDimitry Andric 1542754fe60SDimitry Andric // LVI is now at LVE or LVI->end >= Stop. 1552754fe60SDimitry Andric if (LVI == LVE) 1562754fe60SDimitry Andric break; 1572754fe60SDimitry Andric 1582754fe60SDimitry Andric // Live segment ends exactly at Stop. Move to the next segment. 1592754fe60SDimitry Andric if (LVI->end == Stop && ++LVI == LVE) 1602754fe60SDimitry Andric break; 1612754fe60SDimitry Andric 1622754fe60SDimitry Andric // Pick the next basic block. 1632754fe60SDimitry Andric if (LVI->start < Stop) 1642754fe60SDimitry Andric ++MFI; 1652754fe60SDimitry Andric else 1662754fe60SDimitry Andric MFI = LIS.getMBBFromIndex(LVI->start); 1672754fe60SDimitry Andric } 168e580952dSDimitry Andric } 169e580952dSDimitry Andric 170dd6029ffSDimitry Andric bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const { 171dd6029ffSDimitry Andric unsigned OrigReg = VRM.getOriginal(CurLI->reg); 172dd6029ffSDimitry Andric const LiveInterval &Orig = LIS.getInterval(OrigReg); 173dd6029ffSDimitry Andric assert(!Orig.empty() && "Splitting empty interval?"); 174dd6029ffSDimitry Andric LiveInterval::const_iterator I = Orig.find(Idx); 175dd6029ffSDimitry Andric 176dd6029ffSDimitry Andric // Range containing Idx should begin at Idx. 177dd6029ffSDimitry Andric if (I != Orig.end() && I->start <= Idx) 178dd6029ffSDimitry Andric return I->start == Idx; 179dd6029ffSDimitry Andric 180dd6029ffSDimitry Andric // Range does not contain Idx, previous must end at Idx. 181dd6029ffSDimitry Andric return I != Orig.begin() && (--I)->end == Idx; 182dd6029ffSDimitry Andric } 183dd6029ffSDimitry Andric 1842754fe60SDimitry Andric void SplitAnalysis::print(const BlockPtrSet &B, raw_ostream &OS) const { 1852754fe60SDimitry Andric for (BlockPtrSet::const_iterator I = B.begin(), E = B.end(); I != E; ++I) { 1862754fe60SDimitry Andric unsigned count = UsingBlocks.lookup(*I); 1872754fe60SDimitry Andric OS << " BB#" << (*I)->getNumber(); 1882754fe60SDimitry Andric if (count) 1892754fe60SDimitry Andric OS << '(' << count << ')'; 190e580952dSDimitry Andric } 191e580952dSDimitry Andric } 192e580952dSDimitry Andric 193e580952dSDimitry Andric void SplitAnalysis::analyze(const LiveInterval *li) { 194e580952dSDimitry Andric clear(); 1952754fe60SDimitry Andric CurLI = li; 196e580952dSDimitry Andric analyzeUses(); 197e580952dSDimitry Andric } 198e580952dSDimitry Andric 199e580952dSDimitry Andric 200e580952dSDimitry Andric //===----------------------------------------------------------------------===// 201e580952dSDimitry Andric // LiveIntervalMap 202e580952dSDimitry Andric //===----------------------------------------------------------------------===// 203e580952dSDimitry Andric 2042754fe60SDimitry Andric // Work around the fact that the std::pair constructors are broken for pointer 2052754fe60SDimitry Andric // pairs in some implementations. makeVV(x, 0) works. 2062754fe60SDimitry Andric static inline std::pair<const VNInfo*, VNInfo*> 2072754fe60SDimitry Andric makeVV(const VNInfo *a, VNInfo *b) { 2082754fe60SDimitry Andric return std::make_pair(a, b); 209e580952dSDimitry Andric } 210e580952dSDimitry Andric 2112754fe60SDimitry Andric void LiveIntervalMap::reset(LiveInterval *li) { 2122754fe60SDimitry Andric LI = li; 2132754fe60SDimitry Andric Values.clear(); 2142754fe60SDimitry Andric LiveOutCache.clear(); 2152754fe60SDimitry Andric } 2162754fe60SDimitry Andric 2172754fe60SDimitry Andric bool LiveIntervalMap::isComplexMapped(const VNInfo *ParentVNI) const { 2182754fe60SDimitry Andric ValueMap::const_iterator i = Values.find(ParentVNI); 2192754fe60SDimitry Andric return i != Values.end() && i->second == 0; 2202754fe60SDimitry Andric } 2212754fe60SDimitry Andric 2222754fe60SDimitry Andric // defValue - Introduce a LI def for ParentVNI that could be later than 2232754fe60SDimitry Andric // ParentVNI->def. 2242754fe60SDimitry Andric VNInfo *LiveIntervalMap::defValue(const VNInfo *ParentVNI, SlotIndex Idx) { 2252754fe60SDimitry Andric assert(LI && "call reset first"); 226e580952dSDimitry Andric assert(ParentVNI && "Mapping NULL value"); 227e580952dSDimitry Andric assert(Idx.isValid() && "Invalid SlotIndex"); 2282754fe60SDimitry Andric assert(ParentLI.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI"); 2292754fe60SDimitry Andric 2302754fe60SDimitry Andric // Create a new value. 2312754fe60SDimitry Andric VNInfo *VNI = LI->getNextValue(Idx, 0, LIS.getVNInfoAllocator()); 2322754fe60SDimitry Andric 2332754fe60SDimitry Andric // Preserve the PHIDef bit. 2342754fe60SDimitry Andric if (ParentVNI->isPHIDef() && Idx == ParentVNI->def) 2352754fe60SDimitry Andric VNI->setIsPHIDef(true); 236e580952dSDimitry Andric 237e580952dSDimitry Andric // Use insert for lookup, so we can add missing values with a second lookup. 238e580952dSDimitry Andric std::pair<ValueMap::iterator,bool> InsP = 2392754fe60SDimitry Andric Values.insert(makeVV(ParentVNI, Idx == ParentVNI->def ? VNI : 0)); 2402754fe60SDimitry Andric 2412754fe60SDimitry Andric // This is now a complex def. Mark with a NULL in valueMap. 2422754fe60SDimitry Andric if (!InsP.second) 2432754fe60SDimitry Andric InsP.first->second = 0; 2442754fe60SDimitry Andric 2452754fe60SDimitry Andric return VNI; 2462754fe60SDimitry Andric } 2472754fe60SDimitry Andric 2482754fe60SDimitry Andric 2492754fe60SDimitry Andric // mapValue - Find the mapped value for ParentVNI at Idx. 2502754fe60SDimitry Andric // Potentially create phi-def values. 2512754fe60SDimitry Andric VNInfo *LiveIntervalMap::mapValue(const VNInfo *ParentVNI, SlotIndex Idx, 2522754fe60SDimitry Andric bool *simple) { 2532754fe60SDimitry Andric assert(LI && "call reset first"); 2542754fe60SDimitry Andric assert(ParentVNI && "Mapping NULL value"); 2552754fe60SDimitry Andric assert(Idx.isValid() && "Invalid SlotIndex"); 2562754fe60SDimitry Andric assert(ParentLI.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI"); 2572754fe60SDimitry Andric 2582754fe60SDimitry Andric // Use insert for lookup, so we can add missing values with a second lookup. 2592754fe60SDimitry Andric std::pair<ValueMap::iterator,bool> InsP = 2602754fe60SDimitry Andric Values.insert(makeVV(ParentVNI, 0)); 261e580952dSDimitry Andric 262e580952dSDimitry Andric // This was an unknown value. Create a simple mapping. 2632754fe60SDimitry Andric if (InsP.second) { 2642754fe60SDimitry Andric if (simple) *simple = true; 2652754fe60SDimitry Andric return InsP.first->second = LI->createValueCopy(ParentVNI, 2662754fe60SDimitry Andric LIS.getVNInfoAllocator()); 2672754fe60SDimitry Andric } 2682754fe60SDimitry Andric 269e580952dSDimitry Andric // This was a simple mapped value. 2702754fe60SDimitry Andric if (InsP.first->second) { 2712754fe60SDimitry Andric if (simple) *simple = true; 272e580952dSDimitry Andric return InsP.first->second; 2732754fe60SDimitry Andric } 274e580952dSDimitry Andric 275e580952dSDimitry Andric // This is a complex mapped value. There may be multiple defs, and we may need 276e580952dSDimitry Andric // to create phi-defs. 2772754fe60SDimitry Andric if (simple) *simple = false; 2782754fe60SDimitry Andric MachineBasicBlock *IdxMBB = LIS.getMBBFromIndex(Idx); 279e580952dSDimitry Andric assert(IdxMBB && "No MBB at Idx"); 280e580952dSDimitry Andric 281e580952dSDimitry Andric // Is there a def in the same MBB we can extend? 282e580952dSDimitry Andric if (VNInfo *VNI = extendTo(IdxMBB, Idx)) 283e580952dSDimitry Andric return VNI; 284e580952dSDimitry Andric 285e580952dSDimitry Andric // Now for the fun part. We know that ParentVNI potentially has multiple defs, 286e580952dSDimitry Andric // and we may need to create even more phi-defs to preserve VNInfo SSA form. 2872754fe60SDimitry Andric // Perform a search for all predecessor blocks where we know the dominating 2882754fe60SDimitry Andric // VNInfo. Insert phi-def VNInfos along the path back to IdxMBB. 2892754fe60SDimitry Andric DEBUG(dbgs() << "\n Reaching defs for BB#" << IdxMBB->getNumber() 2902754fe60SDimitry Andric << " at " << Idx << " in " << *LI << '\n'); 291e580952dSDimitry Andric 2922754fe60SDimitry Andric // Blocks where LI should be live-in. 2932754fe60SDimitry Andric SmallVector<MachineDomTreeNode*, 16> LiveIn; 2942754fe60SDimitry Andric LiveIn.push_back(MDT[IdxMBB]); 295e580952dSDimitry Andric 2962754fe60SDimitry Andric // Using LiveOutCache as a visited set, perform a BFS for all reaching defs. 2972754fe60SDimitry Andric for (unsigned i = 0; i != LiveIn.size(); ++i) { 2982754fe60SDimitry Andric MachineBasicBlock *MBB = LiveIn[i]->getBlock(); 2992754fe60SDimitry Andric for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), 3002754fe60SDimitry Andric PE = MBB->pred_end(); PI != PE; ++PI) { 3012754fe60SDimitry Andric MachineBasicBlock *Pred = *PI; 3022754fe60SDimitry Andric // Is this a known live-out block? 3032754fe60SDimitry Andric std::pair<LiveOutMap::iterator,bool> LOIP = 3042754fe60SDimitry Andric LiveOutCache.insert(std::make_pair(Pred, LiveOutPair())); 3052754fe60SDimitry Andric // Yes, we have been here before. 3062754fe60SDimitry Andric if (!LOIP.second) { 3072754fe60SDimitry Andric DEBUG(if (VNInfo *VNI = LOIP.first->second.first) 3082754fe60SDimitry Andric dbgs() << " known valno #" << VNI->id 3092754fe60SDimitry Andric << " at BB#" << Pred->getNumber() << '\n'); 310e580952dSDimitry Andric continue; 311e580952dSDimitry Andric } 312e580952dSDimitry Andric 3132754fe60SDimitry Andric // Does Pred provide a live-out value? 3142754fe60SDimitry Andric SlotIndex Last = LIS.getMBBEndIdx(Pred).getPrevSlot(); 3152754fe60SDimitry Andric if (VNInfo *VNI = extendTo(Pred, Last)) { 3162754fe60SDimitry Andric MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(VNI->def); 3172754fe60SDimitry Andric DEBUG(dbgs() << " found valno #" << VNI->id 3182754fe60SDimitry Andric << " from BB#" << DefMBB->getNumber() 3192754fe60SDimitry Andric << " at BB#" << Pred->getNumber() << '\n'); 3202754fe60SDimitry Andric LiveOutPair &LOP = LOIP.first->second; 3212754fe60SDimitry Andric LOP.first = VNI; 3222754fe60SDimitry Andric LOP.second = MDT[DefMBB]; 323e580952dSDimitry Andric continue; 324e580952dSDimitry Andric } 3252754fe60SDimitry Andric // No, we need a live-in value for Pred as well 3262754fe60SDimitry Andric if (Pred != IdxMBB) 3272754fe60SDimitry Andric LiveIn.push_back(MDT[Pred]); 328e580952dSDimitry Andric } 329e580952dSDimitry Andric } 330e580952dSDimitry Andric 3312754fe60SDimitry Andric // We may need to add phi-def values to preserve the SSA form. 3322754fe60SDimitry Andric // This is essentially the same iterative algorithm that SSAUpdater uses, 3332754fe60SDimitry Andric // except we already have a dominator tree, so we don't have to recompute it. 334e580952dSDimitry Andric VNInfo *IdxVNI = 0; 3352754fe60SDimitry Andric unsigned Changes; 3362754fe60SDimitry Andric do { 3372754fe60SDimitry Andric Changes = 0; 3382754fe60SDimitry Andric DEBUG(dbgs() << " Iterating over " << LiveIn.size() << " blocks.\n"); 3392754fe60SDimitry Andric // Propagate live-out values down the dominator tree, inserting phi-defs when 3402754fe60SDimitry Andric // necessary. Since LiveIn was created by a BFS, going backwards makes it more 3412754fe60SDimitry Andric // likely for us to visit immediate dominators before their children. 3422754fe60SDimitry Andric for (unsigned i = LiveIn.size(); i; --i) { 3432754fe60SDimitry Andric MachineDomTreeNode *Node = LiveIn[i-1]; 3442754fe60SDimitry Andric MachineBasicBlock *MBB = Node->getBlock(); 3452754fe60SDimitry Andric MachineDomTreeNode *IDom = Node->getIDom(); 3462754fe60SDimitry Andric LiveOutPair IDomValue; 3472754fe60SDimitry Andric // We need a live-in value to a block with no immediate dominator? 3482754fe60SDimitry Andric // This is probably an unreachable block that has survived somehow. 3492754fe60SDimitry Andric bool needPHI = !IDom; 3502754fe60SDimitry Andric 3512754fe60SDimitry Andric // Get the IDom live-out value. 3522754fe60SDimitry Andric if (!needPHI) { 3532754fe60SDimitry Andric LiveOutMap::iterator I = LiveOutCache.find(IDom->getBlock()); 3542754fe60SDimitry Andric if (I != LiveOutCache.end()) 3552754fe60SDimitry Andric IDomValue = I->second; 3562754fe60SDimitry Andric else 3572754fe60SDimitry Andric // If IDom is outside our set of live-out blocks, there must be new 3582754fe60SDimitry Andric // defs, and we need a phi-def here. 3592754fe60SDimitry Andric needPHI = true; 360e580952dSDimitry Andric } 361e580952dSDimitry Andric 3622754fe60SDimitry Andric // IDom dominates all of our predecessors, but it may not be the immediate 3632754fe60SDimitry Andric // dominator. Check if any of them have live-out values that are properly 3642754fe60SDimitry Andric // dominated by IDom. If so, we need a phi-def here. 3652754fe60SDimitry Andric if (!needPHI) { 3662754fe60SDimitry Andric for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), 3672754fe60SDimitry Andric PE = MBB->pred_end(); PI != PE; ++PI) { 3682754fe60SDimitry Andric LiveOutPair Value = LiveOutCache[*PI]; 3692754fe60SDimitry Andric if (!Value.first || Value.first == IDomValue.first) 3702754fe60SDimitry Andric continue; 3712754fe60SDimitry Andric // This predecessor is carrying something other than IDomValue. 3722754fe60SDimitry Andric // It could be because IDomValue hasn't propagated yet, or it could be 3732754fe60SDimitry Andric // because MBB is in the dominance frontier of that value. 3742754fe60SDimitry Andric if (MDT.dominates(IDom, Value.second)) { 3752754fe60SDimitry Andric needPHI = true; 3762754fe60SDimitry Andric break; 3772754fe60SDimitry Andric } 3782754fe60SDimitry Andric } 3792754fe60SDimitry Andric } 3802754fe60SDimitry Andric 3812754fe60SDimitry Andric // Create a phi-def if required. 3822754fe60SDimitry Andric if (needPHI) { 3832754fe60SDimitry Andric ++Changes; 3842754fe60SDimitry Andric SlotIndex Start = LIS.getMBBStartIdx(MBB); 3852754fe60SDimitry Andric VNInfo *VNI = LI->getNextValue(Start, 0, LIS.getVNInfoAllocator()); 3862754fe60SDimitry Andric VNI->setIsPHIDef(true); 3872754fe60SDimitry Andric DEBUG(dbgs() << " - BB#" << MBB->getNumber() 3882754fe60SDimitry Andric << " phi-def #" << VNI->id << " at " << Start << '\n'); 3892754fe60SDimitry Andric // We no longer need LI to be live-in. 3902754fe60SDimitry Andric LiveIn.erase(LiveIn.begin()+(i-1)); 3912754fe60SDimitry Andric // Blocks in LiveIn are either IdxMBB, or have a value live-through. 3922754fe60SDimitry Andric if (MBB == IdxMBB) 3932754fe60SDimitry Andric IdxVNI = VNI; 3942754fe60SDimitry Andric // Check if we need to update live-out info. 3952754fe60SDimitry Andric LiveOutMap::iterator I = LiveOutCache.find(MBB); 3962754fe60SDimitry Andric if (I == LiveOutCache.end() || I->second.second == Node) { 3972754fe60SDimitry Andric // We already have a live-out defined in MBB, so this must be IdxMBB. 3982754fe60SDimitry Andric assert(MBB == IdxMBB && "Adding phi-def to known live-out"); 3992754fe60SDimitry Andric LI->addRange(LiveRange(Start, Idx.getNextSlot(), VNI)); 4002754fe60SDimitry Andric } else { 4012754fe60SDimitry Andric // This phi-def is also live-out, so color the whole block. 4022754fe60SDimitry Andric LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI)); 4032754fe60SDimitry Andric I->second = LiveOutPair(VNI, Node); 4042754fe60SDimitry Andric } 4052754fe60SDimitry Andric } else if (IDomValue.first) { 4062754fe60SDimitry Andric // No phi-def here. Remember incoming value for IdxMBB. 4072754fe60SDimitry Andric if (MBB == IdxMBB) 4082754fe60SDimitry Andric IdxVNI = IDomValue.first; 4092754fe60SDimitry Andric // Propagate IDomValue if needed: 4102754fe60SDimitry Andric // MBB is live-out and doesn't define its own value. 4112754fe60SDimitry Andric LiveOutMap::iterator I = LiveOutCache.find(MBB); 4122754fe60SDimitry Andric if (I != LiveOutCache.end() && I->second.second != Node && 4132754fe60SDimitry Andric I->second.first != IDomValue.first) { 4142754fe60SDimitry Andric ++Changes; 4152754fe60SDimitry Andric I->second = IDomValue; 4162754fe60SDimitry Andric DEBUG(dbgs() << " - BB#" << MBB->getNumber() 4172754fe60SDimitry Andric << " idom valno #" << IDomValue.first->id 4182754fe60SDimitry Andric << " from BB#" << IDom->getBlock()->getNumber() << '\n'); 4192754fe60SDimitry Andric } 4202754fe60SDimitry Andric } 4212754fe60SDimitry Andric } 4222754fe60SDimitry Andric DEBUG(dbgs() << " - made " << Changes << " changes.\n"); 4232754fe60SDimitry Andric } while (Changes); 4242754fe60SDimitry Andric 425e580952dSDimitry Andric assert(IdxVNI && "Didn't find value for Idx"); 4262754fe60SDimitry Andric 4272754fe60SDimitry Andric #ifndef NDEBUG 4282754fe60SDimitry Andric // Check the LiveOutCache invariants. 4292754fe60SDimitry Andric for (LiveOutMap::iterator I = LiveOutCache.begin(), E = LiveOutCache.end(); 4302754fe60SDimitry Andric I != E; ++I) { 4312754fe60SDimitry Andric assert(I->first && "Null MBB entry in cache"); 4322754fe60SDimitry Andric assert(I->second.first && "Null VNInfo in cache"); 4332754fe60SDimitry Andric assert(I->second.second && "Null DomTreeNode in cache"); 4342754fe60SDimitry Andric if (I->second.second->getBlock() == I->first) 4352754fe60SDimitry Andric continue; 4362754fe60SDimitry Andric for (MachineBasicBlock::pred_iterator PI = I->first->pred_begin(), 4372754fe60SDimitry Andric PE = I->first->pred_end(); PI != PE; ++PI) 4382754fe60SDimitry Andric assert(LiveOutCache.lookup(*PI) == I->second && "Bad invariant"); 4392754fe60SDimitry Andric } 4402754fe60SDimitry Andric #endif 4412754fe60SDimitry Andric 4422754fe60SDimitry Andric // Since we went through the trouble of a full BFS visiting all reaching defs, 4432754fe60SDimitry Andric // the values in LiveIn are now accurate. No more phi-defs are needed 4442754fe60SDimitry Andric // for these blocks, so we can color the live ranges. 4452754fe60SDimitry Andric // This makes the next mapValue call much faster. 4462754fe60SDimitry Andric for (unsigned i = 0, e = LiveIn.size(); i != e; ++i) { 4472754fe60SDimitry Andric MachineBasicBlock *MBB = LiveIn[i]->getBlock(); 4482754fe60SDimitry Andric SlotIndex Start = LIS.getMBBStartIdx(MBB); 4492754fe60SDimitry Andric VNInfo *VNI = LiveOutCache.lookup(MBB).first; 4502754fe60SDimitry Andric 4512754fe60SDimitry Andric // Anything in LiveIn other than IdxMBB is live-through. 4522754fe60SDimitry Andric // In IdxMBB, we should stop at Idx unless the same value is live-out. 4532754fe60SDimitry Andric if (MBB == IdxMBB && IdxVNI != VNI) 4542754fe60SDimitry Andric LI->addRange(LiveRange(Start, Idx.getNextSlot(), IdxVNI)); 4552754fe60SDimitry Andric else 4562754fe60SDimitry Andric LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI)); 4572754fe60SDimitry Andric } 4582754fe60SDimitry Andric 459e580952dSDimitry Andric return IdxVNI; 460e580952dSDimitry Andric } 461e580952dSDimitry Andric 4622754fe60SDimitry Andric #ifndef NDEBUG 4632754fe60SDimitry Andric void LiveIntervalMap::dumpCache() { 4642754fe60SDimitry Andric for (LiveOutMap::iterator I = LiveOutCache.begin(), E = LiveOutCache.end(); 4652754fe60SDimitry Andric I != E; ++I) { 4662754fe60SDimitry Andric assert(I->first && "Null MBB entry in cache"); 4672754fe60SDimitry Andric assert(I->second.first && "Null VNInfo in cache"); 4682754fe60SDimitry Andric assert(I->second.second && "Null DomTreeNode in cache"); 4692754fe60SDimitry Andric dbgs() << " cache: BB#" << I->first->getNumber() 4702754fe60SDimitry Andric << " has valno #" << I->second.first->id << " from BB#" 4712754fe60SDimitry Andric << I->second.second->getBlock()->getNumber() << ", preds"; 4722754fe60SDimitry Andric for (MachineBasicBlock::pred_iterator PI = I->first->pred_begin(), 4732754fe60SDimitry Andric PE = I->first->pred_end(); PI != PE; ++PI) 4742754fe60SDimitry Andric dbgs() << " BB#" << (*PI)->getNumber(); 4752754fe60SDimitry Andric dbgs() << '\n'; 4762754fe60SDimitry Andric } 4772754fe60SDimitry Andric dbgs() << " cache: " << LiveOutCache.size() << " entries.\n"; 4782754fe60SDimitry Andric } 4792754fe60SDimitry Andric #endif 4802754fe60SDimitry Andric 4812754fe60SDimitry Andric // extendTo - Find the last LI value defined in MBB at or before Idx. The 4822754fe60SDimitry Andric // ParentLI is assumed to be live at Idx. Extend the live range to Idx. 483e580952dSDimitry Andric // Return the found VNInfo, or NULL. 4842754fe60SDimitry Andric VNInfo *LiveIntervalMap::extendTo(const MachineBasicBlock *MBB, SlotIndex Idx) { 4852754fe60SDimitry Andric assert(LI && "call reset first"); 4862754fe60SDimitry Andric LiveInterval::iterator I = std::upper_bound(LI->begin(), LI->end(), Idx); 4872754fe60SDimitry Andric if (I == LI->begin()) 488e580952dSDimitry Andric return 0; 489e580952dSDimitry Andric --I; 4902754fe60SDimitry Andric if (I->end <= LIS.getMBBStartIdx(MBB)) 491e580952dSDimitry Andric return 0; 4922754fe60SDimitry Andric if (I->end <= Idx) 4932754fe60SDimitry Andric I->end = Idx.getNextSlot(); 494e580952dSDimitry Andric return I->valno; 495e580952dSDimitry Andric } 496e580952dSDimitry Andric 4972754fe60SDimitry Andric // addSimpleRange - Add a simple range from ParentLI to LI. 498e580952dSDimitry Andric // ParentVNI must be live in the [Start;End) interval. 499e580952dSDimitry Andric void LiveIntervalMap::addSimpleRange(SlotIndex Start, SlotIndex End, 500e580952dSDimitry Andric const VNInfo *ParentVNI) { 5012754fe60SDimitry Andric assert(LI && "call reset first"); 5022754fe60SDimitry Andric bool simple; 5032754fe60SDimitry Andric VNInfo *VNI = mapValue(ParentVNI, Start, &simple); 5042754fe60SDimitry Andric // A simple mapping is easy. 5052754fe60SDimitry Andric if (simple) { 5062754fe60SDimitry Andric LI->addRange(LiveRange(Start, End, VNI)); 507e580952dSDimitry Andric return; 508e580952dSDimitry Andric } 509e580952dSDimitry Andric 510e580952dSDimitry Andric // ParentVNI is a complex value. We must map per MBB. 5112754fe60SDimitry Andric MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start); 5122754fe60SDimitry Andric MachineFunction::iterator MBBE = LIS.getMBBFromIndex(End.getPrevSlot()); 513e580952dSDimitry Andric 514e580952dSDimitry Andric if (MBB == MBBE) { 5152754fe60SDimitry Andric LI->addRange(LiveRange(Start, End, VNI)); 516e580952dSDimitry Andric return; 517e580952dSDimitry Andric } 518e580952dSDimitry Andric 519e580952dSDimitry Andric // First block. 5202754fe60SDimitry Andric LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI)); 521e580952dSDimitry Andric 522e580952dSDimitry Andric // Run sequence of full blocks. 523e580952dSDimitry Andric for (++MBB; MBB != MBBE; ++MBB) { 5242754fe60SDimitry Andric Start = LIS.getMBBStartIdx(MBB); 5252754fe60SDimitry Andric LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), 526e580952dSDimitry Andric mapValue(ParentVNI, Start))); 527e580952dSDimitry Andric } 528e580952dSDimitry Andric 529e580952dSDimitry Andric // Final block. 5302754fe60SDimitry Andric Start = LIS.getMBBStartIdx(MBB); 531e580952dSDimitry Andric if (Start != End) 5322754fe60SDimitry Andric LI->addRange(LiveRange(Start, End, mapValue(ParentVNI, Start))); 533e580952dSDimitry Andric } 534e580952dSDimitry Andric 5352754fe60SDimitry Andric /// addRange - Add live ranges to LI where [Start;End) intersects ParentLI. 536e580952dSDimitry Andric /// All needed values whose def is not inside [Start;End) must be defined 537e580952dSDimitry Andric /// beforehand so mapValue will work. 538e580952dSDimitry Andric void LiveIntervalMap::addRange(SlotIndex Start, SlotIndex End) { 5392754fe60SDimitry Andric assert(LI && "call reset first"); 5402754fe60SDimitry Andric LiveInterval::const_iterator B = ParentLI.begin(), E = ParentLI.end(); 541e580952dSDimitry Andric LiveInterval::const_iterator I = std::lower_bound(B, E, Start); 542e580952dSDimitry Andric 543e580952dSDimitry Andric // Check if --I begins before Start and overlaps. 544e580952dSDimitry Andric if (I != B) { 545e580952dSDimitry Andric --I; 546e580952dSDimitry Andric if (I->end > Start) 547e580952dSDimitry Andric addSimpleRange(Start, std::min(End, I->end), I->valno); 548e580952dSDimitry Andric ++I; 549e580952dSDimitry Andric } 550e580952dSDimitry Andric 551e580952dSDimitry Andric // The remaining ranges begin after Start. 552e580952dSDimitry Andric for (;I != E && I->start < End; ++I) 553e580952dSDimitry Andric addSimpleRange(I->start, std::min(End, I->end), I->valno); 554e580952dSDimitry Andric } 555e580952dSDimitry Andric 5562754fe60SDimitry Andric 557e580952dSDimitry Andric //===----------------------------------------------------------------------===// 558e580952dSDimitry Andric // Split Editor 559e580952dSDimitry Andric //===----------------------------------------------------------------------===// 560e580952dSDimitry Andric 561e580952dSDimitry Andric /// Create a new SplitEditor for editing the LiveInterval analyzed by SA. 5622754fe60SDimitry Andric SplitEditor::SplitEditor(SplitAnalysis &sa, 5632754fe60SDimitry Andric LiveIntervals &lis, 5642754fe60SDimitry Andric VirtRegMap &vrm, 5652754fe60SDimitry Andric MachineDominatorTree &mdt, 5662754fe60SDimitry Andric LiveRangeEdit &edit) 5672754fe60SDimitry Andric : SA(sa), LIS(lis), VRM(vrm), 5682754fe60SDimitry Andric MRI(vrm.getMachineFunction().getRegInfo()), 5692754fe60SDimitry Andric MDT(mdt), 5702754fe60SDimitry Andric TII(*vrm.getMachineFunction().getTarget().getInstrInfo()), 5712754fe60SDimitry Andric TRI(*vrm.getMachineFunction().getTarget().getRegisterInfo()), 5722754fe60SDimitry Andric Edit(edit), 5732754fe60SDimitry Andric OpenIdx(0), 5742754fe60SDimitry Andric RegAssign(Allocator) 575e580952dSDimitry Andric { 5762754fe60SDimitry Andric // We don't need an AliasAnalysis since we will only be performing 5772754fe60SDimitry Andric // cheap-as-a-copy remats anyway. 5782754fe60SDimitry Andric Edit.anyRematerializable(LIS, TII, 0); 579e580952dSDimitry Andric } 580e580952dSDimitry Andric 5812754fe60SDimitry Andric void SplitEditor::dump() const { 5822754fe60SDimitry Andric if (RegAssign.empty()) { 5832754fe60SDimitry Andric dbgs() << " empty\n"; 5842754fe60SDimitry Andric return; 585e580952dSDimitry Andric } 586e580952dSDimitry Andric 5872754fe60SDimitry Andric for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I) 5882754fe60SDimitry Andric dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value(); 5892754fe60SDimitry Andric dbgs() << '\n'; 590e580952dSDimitry Andric } 591e580952dSDimitry Andric 5922754fe60SDimitry Andric VNInfo *SplitEditor::defFromParent(unsigned RegIdx, 5932754fe60SDimitry Andric VNInfo *ParentVNI, 5942754fe60SDimitry Andric SlotIndex UseIdx, 595e580952dSDimitry Andric MachineBasicBlock &MBB, 596e580952dSDimitry Andric MachineBasicBlock::iterator I) { 5972754fe60SDimitry Andric MachineInstr *CopyMI = 0; 5982754fe60SDimitry Andric SlotIndex Def; 5992754fe60SDimitry Andric LiveInterval *LI = Edit.get(RegIdx); 6002754fe60SDimitry Andric 6012754fe60SDimitry Andric // Attempt cheap-as-a-copy rematerialization. 6022754fe60SDimitry Andric LiveRangeEdit::Remat RM(ParentVNI); 6032754fe60SDimitry Andric if (Edit.canRematerializeAt(RM, UseIdx, true, LIS)) { 6042754fe60SDimitry Andric Def = Edit.rematerializeAt(MBB, I, LI->reg, RM, LIS, TII, TRI); 6052754fe60SDimitry Andric } else { 6062754fe60SDimitry Andric // Can't remat, just insert a copy from parent. 6072754fe60SDimitry Andric CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg) 6082754fe60SDimitry Andric .addReg(Edit.getReg()); 6092754fe60SDimitry Andric Def = LIS.InsertMachineInstrInMaps(CopyMI).getDefIndex(); 6102754fe60SDimitry Andric } 6112754fe60SDimitry Andric 6122754fe60SDimitry Andric // Define the value in Reg. 6132754fe60SDimitry Andric VNInfo *VNI = LIMappers[RegIdx].defValue(ParentVNI, Def); 6142754fe60SDimitry Andric VNI->setCopy(CopyMI); 6152754fe60SDimitry Andric 6162754fe60SDimitry Andric // Add minimal liveness for the new value. 6172754fe60SDimitry Andric Edit.get(RegIdx)->addRange(LiveRange(Def, Def.getNextSlot(), VNI)); 6182754fe60SDimitry Andric return VNI; 619e580952dSDimitry Andric } 620e580952dSDimitry Andric 621e580952dSDimitry Andric /// Create a new virtual register and live interval. 622e580952dSDimitry Andric void SplitEditor::openIntv() { 6232754fe60SDimitry Andric assert(!OpenIdx && "Previous LI not closed before openIntv"); 6242754fe60SDimitry Andric 6252754fe60SDimitry Andric // Create the complement as index 0. 6262754fe60SDimitry Andric if (Edit.empty()) { 6272754fe60SDimitry Andric Edit.create(MRI, LIS, VRM); 6282754fe60SDimitry Andric LIMappers.push_back(LiveIntervalMap(LIS, MDT, Edit.getParent())); 6292754fe60SDimitry Andric LIMappers.back().reset(Edit.get(0)); 630e580952dSDimitry Andric } 631e580952dSDimitry Andric 6322754fe60SDimitry Andric // Create the open interval. 6332754fe60SDimitry Andric OpenIdx = Edit.size(); 6342754fe60SDimitry Andric Edit.create(MRI, LIS, VRM); 6352754fe60SDimitry Andric LIMappers.push_back(LiveIntervalMap(LIS, MDT, Edit.getParent())); 6362754fe60SDimitry Andric LIMappers[OpenIdx].reset(Edit.get(OpenIdx)); 6372754fe60SDimitry Andric } 638e580952dSDimitry Andric 6392754fe60SDimitry Andric SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) { 6402754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before enterIntvBefore"); 6412754fe60SDimitry Andric DEBUG(dbgs() << " enterIntvBefore " << Idx); 6422754fe60SDimitry Andric Idx = Idx.getBaseIndex(); 6432754fe60SDimitry Andric VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx); 6442754fe60SDimitry Andric if (!ParentVNI) { 6452754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 6462754fe60SDimitry Andric return Idx; 6472754fe60SDimitry Andric } 6482754fe60SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 6492754fe60SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Idx); 650e580952dSDimitry Andric assert(MI && "enterIntvBefore called with invalid index"); 651e580952dSDimitry Andric 6522754fe60SDimitry Andric VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI); 6532754fe60SDimitry Andric return VNI->def; 654e580952dSDimitry Andric } 655e580952dSDimitry Andric 6562754fe60SDimitry Andric SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) { 6572754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before enterIntvAtEnd"); 6582754fe60SDimitry Andric SlotIndex End = LIS.getMBBEndIdx(&MBB); 6592754fe60SDimitry Andric SlotIndex Last = End.getPrevSlot(); 6602754fe60SDimitry Andric DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last); 6612754fe60SDimitry Andric VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Last); 6622754fe60SDimitry Andric if (!ParentVNI) { 6632754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 6642754fe60SDimitry Andric return End; 6652754fe60SDimitry Andric } 6662754fe60SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id); 6672754fe60SDimitry Andric VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB, 6682754fe60SDimitry Andric LIS.getLastSplitPoint(Edit.getParent(), &MBB)); 6692754fe60SDimitry Andric RegAssign.insert(VNI->def, End, OpenIdx); 6702754fe60SDimitry Andric DEBUG(dump()); 6712754fe60SDimitry Andric return VNI->def; 672e580952dSDimitry Andric } 673e580952dSDimitry Andric 6742754fe60SDimitry Andric /// useIntv - indicate that all instructions in MBB should use OpenLI. 675e580952dSDimitry Andric void SplitEditor::useIntv(const MachineBasicBlock &MBB) { 6762754fe60SDimitry Andric useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB)); 677e580952dSDimitry Andric } 678e580952dSDimitry Andric 679e580952dSDimitry Andric void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) { 6802754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before useIntv"); 6812754fe60SDimitry Andric DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):"); 6822754fe60SDimitry Andric RegAssign.insert(Start, End, OpenIdx); 6832754fe60SDimitry Andric DEBUG(dump()); 684e580952dSDimitry Andric } 685e580952dSDimitry Andric 6862754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) { 6872754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before leaveIntvAfter"); 6882754fe60SDimitry Andric DEBUG(dbgs() << " leaveIntvAfter " << Idx); 6892754fe60SDimitry Andric 6902754fe60SDimitry Andric // The interval must be live beyond the instruction at Idx. 6912754fe60SDimitry Andric Idx = Idx.getBoundaryIndex(); 6922754fe60SDimitry Andric VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx); 6932754fe60SDimitry Andric if (!ParentVNI) { 6942754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 6952754fe60SDimitry Andric return Idx.getNextSlot(); 6962754fe60SDimitry Andric } 6972754fe60SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 6982754fe60SDimitry Andric 6992754fe60SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Idx); 7002754fe60SDimitry Andric assert(MI && "No instruction at index"); 7012754fe60SDimitry Andric VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), 7022754fe60SDimitry Andric llvm::next(MachineBasicBlock::iterator(MI))); 7032754fe60SDimitry Andric return VNI->def; 704e580952dSDimitry Andric } 705e580952dSDimitry Andric 7062754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) { 7072754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before leaveIntvBefore"); 7082754fe60SDimitry Andric DEBUG(dbgs() << " leaveIntvBefore " << Idx); 709e580952dSDimitry Andric 7102754fe60SDimitry Andric // The interval must be live into the instruction at Idx. 7112754fe60SDimitry Andric Idx = Idx.getBoundaryIndex(); 7122754fe60SDimitry Andric VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx); 7132754fe60SDimitry Andric if (!ParentVNI) { 7142754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 7152754fe60SDimitry Andric return Idx.getNextSlot(); 7162754fe60SDimitry Andric } 7172754fe60SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 7182754fe60SDimitry Andric 7192754fe60SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Idx); 7202754fe60SDimitry Andric assert(MI && "No instruction at index"); 7212754fe60SDimitry Andric VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI); 7222754fe60SDimitry Andric return VNI->def; 723e580952dSDimitry Andric } 724e580952dSDimitry Andric 7252754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) { 7262754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before leaveIntvAtTop"); 7272754fe60SDimitry Andric SlotIndex Start = LIS.getMBBStartIdx(&MBB); 7282754fe60SDimitry Andric DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start); 7292754fe60SDimitry Andric 7302754fe60SDimitry Andric VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Start); 7312754fe60SDimitry Andric if (!ParentVNI) { 7322754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 7332754fe60SDimitry Andric return Start; 734e580952dSDimitry Andric } 735e580952dSDimitry Andric 7362754fe60SDimitry Andric VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB, 7372754fe60SDimitry Andric MBB.SkipPHIsAndLabels(MBB.begin())); 7382754fe60SDimitry Andric RegAssign.insert(Start, VNI->def, OpenIdx); 7392754fe60SDimitry Andric DEBUG(dump()); 7402754fe60SDimitry Andric return VNI->def; 741e580952dSDimitry Andric } 742e580952dSDimitry Andric 7432754fe60SDimitry Andric void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) { 7442754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before overlapIntv"); 7452754fe60SDimitry Andric assert(Edit.getParent().getVNInfoAt(Start) == 7462754fe60SDimitry Andric Edit.getParent().getVNInfoAt(End.getPrevSlot()) && 7472754fe60SDimitry Andric "Parent changes value in extended range"); 7482754fe60SDimitry Andric assert(Edit.get(0)->getVNInfoAt(Start) && "Start must come from leaveIntv*"); 7492754fe60SDimitry Andric assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) && 7502754fe60SDimitry Andric "Range cannot span basic blocks"); 751e580952dSDimitry Andric 7522754fe60SDimitry Andric // Treat this as useIntv() for now. The complement interval will be extended 7532754fe60SDimitry Andric // as needed by mapValue(). 7542754fe60SDimitry Andric DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):"); 7552754fe60SDimitry Andric RegAssign.insert(Start, End, OpenIdx); 7562754fe60SDimitry Andric DEBUG(dump()); 757e580952dSDimitry Andric } 758e580952dSDimitry Andric 759e580952dSDimitry Andric /// closeIntv - Indicate that we are done editing the currently open 760e580952dSDimitry Andric /// LiveInterval, and ranges can be trimmed. 761e580952dSDimitry Andric void SplitEditor::closeIntv() { 7622754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before closeIntv"); 7632754fe60SDimitry Andric OpenIdx = 0; 764e580952dSDimitry Andric } 765e580952dSDimitry Andric 7662754fe60SDimitry Andric /// rewriteAssigned - Rewrite all uses of Edit.getReg(). 7672754fe60SDimitry Andric void SplitEditor::rewriteAssigned() { 7682754fe60SDimitry Andric for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit.getReg()), 7692754fe60SDimitry Andric RE = MRI.reg_end(); RI != RE;) { 770e580952dSDimitry Andric MachineOperand &MO = RI.getOperand(); 771e580952dSDimitry Andric MachineInstr *MI = MO.getParent(); 772e580952dSDimitry Andric ++RI; 7732754fe60SDimitry Andric // LiveDebugVariables should have handled all DBG_VALUE instructions. 774e580952dSDimitry Andric if (MI->isDebugValue()) { 775e580952dSDimitry Andric DEBUG(dbgs() << "Zapping " << *MI); 776e580952dSDimitry Andric MO.setReg(0); 777e580952dSDimitry Andric continue; 778e580952dSDimitry Andric } 7792754fe60SDimitry Andric 7802754fe60SDimitry Andric // <undef> operands don't really read the register, so just assign them to 7812754fe60SDimitry Andric // the complement. 7822754fe60SDimitry Andric if (MO.isUse() && MO.isUndef()) { 7832754fe60SDimitry Andric MO.setReg(Edit.get(0)->reg); 7842754fe60SDimitry Andric continue; 7852754fe60SDimitry Andric } 7862754fe60SDimitry Andric 7872754fe60SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(MI); 788e580952dSDimitry Andric Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex(); 7892754fe60SDimitry Andric 7902754fe60SDimitry Andric // Rewrite to the mapped register at Idx. 7912754fe60SDimitry Andric unsigned RegIdx = RegAssign.lookup(Idx); 7922754fe60SDimitry Andric MO.setReg(Edit.get(RegIdx)->reg); 7932754fe60SDimitry Andric DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t' 7942754fe60SDimitry Andric << Idx << ':' << RegIdx << '\t' << *MI); 7952754fe60SDimitry Andric 7962754fe60SDimitry Andric // Extend liveness to Idx. 7972754fe60SDimitry Andric const VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx); 7982754fe60SDimitry Andric LIMappers[RegIdx].mapValue(ParentVNI, Idx); 799e580952dSDimitry Andric } 800e580952dSDimitry Andric } 801e580952dSDimitry Andric 8022754fe60SDimitry Andric /// rewriteSplit - Rewrite uses of Intvs[0] according to the ConEQ mapping. 8032754fe60SDimitry Andric void SplitEditor::rewriteComponents(const SmallVectorImpl<LiveInterval*> &Intvs, 8042754fe60SDimitry Andric const ConnectedVNInfoEqClasses &ConEq) { 8052754fe60SDimitry Andric for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Intvs[0]->reg), 8062754fe60SDimitry Andric RE = MRI.reg_end(); RI != RE;) { 8072754fe60SDimitry Andric MachineOperand &MO = RI.getOperand(); 8082754fe60SDimitry Andric MachineInstr *MI = MO.getParent(); 8092754fe60SDimitry Andric ++RI; 8102754fe60SDimitry Andric if (MO.isUse() && MO.isUndef()) 8112754fe60SDimitry Andric continue; 8122754fe60SDimitry Andric // DBG_VALUE instructions should have been eliminated earlier. 8132754fe60SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(MI); 8142754fe60SDimitry Andric Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex(); 8152754fe60SDimitry Andric DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t' 8162754fe60SDimitry Andric << Idx << ':'); 8172754fe60SDimitry Andric const VNInfo *VNI = Intvs[0]->getVNInfoAt(Idx); 8182754fe60SDimitry Andric assert(VNI && "Interval not live at use."); 8192754fe60SDimitry Andric MO.setReg(Intvs[ConEq.getEqClass(VNI)]->reg); 8202754fe60SDimitry Andric DEBUG(dbgs() << VNI->id << '\t' << *MI); 821e580952dSDimitry Andric } 822e580952dSDimitry Andric } 823e580952dSDimitry Andric 8242754fe60SDimitry Andric void SplitEditor::finish() { 8252754fe60SDimitry Andric assert(OpenIdx == 0 && "Previous LI not closed before rewrite"); 8262754fe60SDimitry Andric 8272754fe60SDimitry Andric // At this point, the live intervals in Edit contain VNInfos corresponding to 8282754fe60SDimitry Andric // the inserted copies. 8292754fe60SDimitry Andric 8302754fe60SDimitry Andric // Add the original defs from the parent interval. 8312754fe60SDimitry Andric for (LiveInterval::const_vni_iterator I = Edit.getParent().vni_begin(), 8322754fe60SDimitry Andric E = Edit.getParent().vni_end(); I != E; ++I) { 8332754fe60SDimitry Andric const VNInfo *ParentVNI = *I; 8342754fe60SDimitry Andric if (ParentVNI->isUnused()) 8352754fe60SDimitry Andric continue; 8362754fe60SDimitry Andric LiveIntervalMap &LIM = LIMappers[RegAssign.lookup(ParentVNI->def)]; 8372754fe60SDimitry Andric VNInfo *VNI = LIM.defValue(ParentVNI, ParentVNI->def); 8382754fe60SDimitry Andric LIM.getLI()->addRange(LiveRange(ParentVNI->def, 8392754fe60SDimitry Andric ParentVNI->def.getNextSlot(), VNI)); 8402754fe60SDimitry Andric // Mark all values as complex to force liveness computation. 8412754fe60SDimitry Andric // This should really only be necessary for remat victims, but we are lazy. 8422754fe60SDimitry Andric LIM.markComplexMapped(ParentVNI); 8432754fe60SDimitry Andric } 8442754fe60SDimitry Andric 8452754fe60SDimitry Andric #ifndef NDEBUG 8462754fe60SDimitry Andric // Every new interval must have a def by now, otherwise the split is bogus. 8472754fe60SDimitry Andric for (LiveRangeEdit::iterator I = Edit.begin(), E = Edit.end(); I != E; ++I) 8482754fe60SDimitry Andric assert((*I)->hasAtLeastOneValue() && "Split interval has no value"); 8492754fe60SDimitry Andric #endif 8502754fe60SDimitry Andric 8512754fe60SDimitry Andric // FIXME: Don't recompute the liveness of all values, infer it from the 8522754fe60SDimitry Andric // overlaps between the parent live interval and RegAssign. 8532754fe60SDimitry Andric // The mapValue algorithm is only necessary when: 8542754fe60SDimitry Andric // - The parent value maps to multiple defs, and new phis are needed, or 8552754fe60SDimitry Andric // - The value has been rematerialized before some uses, and we want to 8562754fe60SDimitry Andric // minimize the live range so it only reaches the remaining uses. 8572754fe60SDimitry Andric // All other values have simple liveness that can be computed from RegAssign 8582754fe60SDimitry Andric // and the parent live interval. 8592754fe60SDimitry Andric 8602754fe60SDimitry Andric // Extend live ranges to be live-out for successor PHI values. 8612754fe60SDimitry Andric for (LiveInterval::const_vni_iterator I = Edit.getParent().vni_begin(), 8622754fe60SDimitry Andric E = Edit.getParent().vni_end(); I != E; ++I) { 8632754fe60SDimitry Andric const VNInfo *PHIVNI = *I; 8642754fe60SDimitry Andric if (PHIVNI->isUnused() || !PHIVNI->isPHIDef()) 8652754fe60SDimitry Andric continue; 8662754fe60SDimitry Andric unsigned RegIdx = RegAssign.lookup(PHIVNI->def); 8672754fe60SDimitry Andric LiveIntervalMap &LIM = LIMappers[RegIdx]; 8682754fe60SDimitry Andric MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def); 8692754fe60SDimitry Andric DEBUG(dbgs() << " map phi in BB#" << MBB->getNumber() << '@' << PHIVNI->def 8702754fe60SDimitry Andric << " -> " << RegIdx << '\n'); 8712754fe60SDimitry Andric for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), 8722754fe60SDimitry Andric PE = MBB->pred_end(); PI != PE; ++PI) { 8732754fe60SDimitry Andric SlotIndex End = LIS.getMBBEndIdx(*PI).getPrevSlot(); 8742754fe60SDimitry Andric DEBUG(dbgs() << " pred BB#" << (*PI)->getNumber() << '@' << End); 8752754fe60SDimitry Andric // The predecessor may not have a live-out value. That is OK, like an 8762754fe60SDimitry Andric // undef PHI operand. 8772754fe60SDimitry Andric if (VNInfo *VNI = Edit.getParent().getVNInfoAt(End)) { 8782754fe60SDimitry Andric DEBUG(dbgs() << " has parent valno #" << VNI->id << " live out\n"); 8792754fe60SDimitry Andric assert(RegAssign.lookup(End) == RegIdx && 8802754fe60SDimitry Andric "Different register assignment in phi predecessor"); 8812754fe60SDimitry Andric LIM.mapValue(VNI, End); 8822754fe60SDimitry Andric } 8832754fe60SDimitry Andric else 8842754fe60SDimitry Andric DEBUG(dbgs() << " is not live-out\n"); 8852754fe60SDimitry Andric } 8862754fe60SDimitry Andric DEBUG(dbgs() << " " << *LIM.getLI() << '\n'); 8872754fe60SDimitry Andric } 8882754fe60SDimitry Andric 8892754fe60SDimitry Andric // Rewrite instructions. 8902754fe60SDimitry Andric rewriteAssigned(); 8912754fe60SDimitry Andric 8922754fe60SDimitry Andric // FIXME: Delete defs that were rematted everywhere. 8932754fe60SDimitry Andric 8942754fe60SDimitry Andric // Get rid of unused values and set phi-kill flags. 8952754fe60SDimitry Andric for (LiveRangeEdit::iterator I = Edit.begin(), E = Edit.end(); I != E; ++I) 8962754fe60SDimitry Andric (*I)->RenumberValues(LIS); 8972754fe60SDimitry Andric 8982754fe60SDimitry Andric // Now check if any registers were separated into multiple components. 8992754fe60SDimitry Andric ConnectedVNInfoEqClasses ConEQ(LIS); 9002754fe60SDimitry Andric for (unsigned i = 0, e = Edit.size(); i != e; ++i) { 9012754fe60SDimitry Andric // Don't use iterators, they are invalidated by create() below. 9022754fe60SDimitry Andric LiveInterval *li = Edit.get(i); 9032754fe60SDimitry Andric unsigned NumComp = ConEQ.Classify(li); 9042754fe60SDimitry Andric if (NumComp <= 1) 9052754fe60SDimitry Andric continue; 9062754fe60SDimitry Andric DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n'); 9072754fe60SDimitry Andric SmallVector<LiveInterval*, 8> dups; 9082754fe60SDimitry Andric dups.push_back(li); 9092754fe60SDimitry Andric for (unsigned i = 1; i != NumComp; ++i) 9102754fe60SDimitry Andric dups.push_back(&Edit.create(MRI, LIS, VRM)); 9112754fe60SDimitry Andric rewriteComponents(dups, ConEQ); 9122754fe60SDimitry Andric ConEQ.Distribute(&dups[0]); 9132754fe60SDimitry Andric } 9142754fe60SDimitry Andric 915e580952dSDimitry Andric // Calculate spill weight and allocation hints for new intervals. 9162754fe60SDimitry Andric VirtRegAuxInfo vrai(VRM.getMachineFunction(), LIS, SA.Loops); 9172754fe60SDimitry Andric for (LiveRangeEdit::iterator I = Edit.begin(), E = Edit.end(); I != E; ++I){ 9182754fe60SDimitry Andric LiveInterval &li = **I; 919e580952dSDimitry Andric vrai.CalculateRegClass(li.reg); 920e580952dSDimitry Andric vrai.CalculateWeightAndHint(li); 9212754fe60SDimitry Andric DEBUG(dbgs() << " new interval " << MRI.getRegClass(li.reg)->getName() 922e580952dSDimitry Andric << ":" << li << '\n'); 923e580952dSDimitry Andric } 924e580952dSDimitry Andric } 925e580952dSDimitry Andric 926e580952dSDimitry Andric 927e580952dSDimitry Andric //===----------------------------------------------------------------------===// 928e580952dSDimitry Andric // Single Block Splitting 929e580952dSDimitry Andric //===----------------------------------------------------------------------===// 930e580952dSDimitry Andric 9312754fe60SDimitry Andric /// getMultiUseBlocks - if CurLI has more than one use in a basic block, it 9322754fe60SDimitry Andric /// may be an advantage to split CurLI for the duration of the block. 9332754fe60SDimitry Andric bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) { 9342754fe60SDimitry Andric // If CurLI is local to one block, there is no point to splitting it. 9352754fe60SDimitry Andric if (LiveBlocks.size() <= 1) 9362754fe60SDimitry Andric return false; 9372754fe60SDimitry Andric // Add blocks with multiple uses. 9382754fe60SDimitry Andric for (unsigned i = 0, e = LiveBlocks.size(); i != e; ++i) { 9392754fe60SDimitry Andric const BlockInfo &BI = LiveBlocks[i]; 9402754fe60SDimitry Andric if (!BI.Uses) 941e580952dSDimitry Andric continue; 9422754fe60SDimitry Andric unsigned Instrs = UsingBlocks.lookup(BI.MBB); 9432754fe60SDimitry Andric if (Instrs <= 1) 9442754fe60SDimitry Andric continue; 9452754fe60SDimitry Andric if (Instrs == 2 && BI.LiveIn && BI.LiveOut && !BI.LiveThrough) 9462754fe60SDimitry Andric continue; 9472754fe60SDimitry Andric Blocks.insert(BI.MBB); 9482754fe60SDimitry Andric } 9492754fe60SDimitry Andric return !Blocks.empty(); 950e580952dSDimitry Andric } 951e580952dSDimitry Andric 9522754fe60SDimitry Andric /// splitSingleBlocks - Split CurLI into a separate live interval inside each 9532754fe60SDimitry Andric /// basic block in Blocks. 9542754fe60SDimitry Andric void SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) { 9552754fe60SDimitry Andric DEBUG(dbgs() << " splitSingleBlocks for " << Blocks.size() << " blocks.\n"); 9562754fe60SDimitry Andric 9572754fe60SDimitry Andric for (unsigned i = 0, e = SA.LiveBlocks.size(); i != e; ++i) { 9582754fe60SDimitry Andric const SplitAnalysis::BlockInfo &BI = SA.LiveBlocks[i]; 9592754fe60SDimitry Andric if (!BI.Uses || !Blocks.count(BI.MBB)) 9602754fe60SDimitry Andric continue; 961e580952dSDimitry Andric 962e580952dSDimitry Andric openIntv(); 9632754fe60SDimitry Andric SlotIndex SegStart = enterIntvBefore(BI.FirstUse); 964dd6029ffSDimitry Andric if (!BI.LiveOut || BI.LastUse < BI.LastSplitPoint) { 9652754fe60SDimitry Andric useIntv(SegStart, leaveIntvAfter(BI.LastUse)); 9662754fe60SDimitry Andric } else { 967dd6029ffSDimitry Andric // The last use is after the last valid split point. 9682754fe60SDimitry Andric SlotIndex SegStop = leaveIntvBefore(BI.LastSplitPoint); 9692754fe60SDimitry Andric useIntv(SegStart, SegStop); 9702754fe60SDimitry Andric overlapIntv(SegStop, BI.LastUse); 9712754fe60SDimitry Andric } 972e580952dSDimitry Andric closeIntv(); 973e580952dSDimitry Andric } 9742754fe60SDimitry Andric finish(); 975e580952dSDimitry Andric } 976e580952dSDimitry Andric 977e580952dSDimitry Andric 978e580952dSDimitry Andric //===----------------------------------------------------------------------===// 979e580952dSDimitry Andric // Sub Block Splitting 980e580952dSDimitry Andric //===----------------------------------------------------------------------===// 981e580952dSDimitry Andric 9822754fe60SDimitry Andric /// getBlockForInsideSplit - If CurLI is contained inside a single basic block, 983e580952dSDimitry Andric /// and it wou pay to subdivide the interval inside that block, return it. 984e580952dSDimitry Andric /// Otherwise return NULL. The returned block can be passed to 985e580952dSDimitry Andric /// SplitEditor::splitInsideBlock. 986e580952dSDimitry Andric const MachineBasicBlock *SplitAnalysis::getBlockForInsideSplit() { 987e580952dSDimitry Andric // The interval must be exclusive to one block. 9882754fe60SDimitry Andric if (UsingBlocks.size() != 1) 989e580952dSDimitry Andric return 0; 990e580952dSDimitry Andric // Don't to this for less than 4 instructions. We want to be sure that 991e580952dSDimitry Andric // splitting actually reduces the instruction count per interval. 9922754fe60SDimitry Andric if (UsingInstrs.size() < 4) 993e580952dSDimitry Andric return 0; 9942754fe60SDimitry Andric return UsingBlocks.begin()->first; 995e580952dSDimitry Andric } 996e580952dSDimitry Andric 9972754fe60SDimitry Andric /// splitInsideBlock - Split CurLI into multiple intervals inside MBB. 9982754fe60SDimitry Andric void SplitEditor::splitInsideBlock(const MachineBasicBlock *MBB) { 999e580952dSDimitry Andric SmallVector<SlotIndex, 32> Uses; 10002754fe60SDimitry Andric Uses.reserve(SA.UsingInstrs.size()); 10012754fe60SDimitry Andric for (SplitAnalysis::InstrPtrSet::const_iterator I = SA.UsingInstrs.begin(), 10022754fe60SDimitry Andric E = SA.UsingInstrs.end(); I != E; ++I) 1003e580952dSDimitry Andric if ((*I)->getParent() == MBB) 10042754fe60SDimitry Andric Uses.push_back(LIS.getInstructionIndex(*I)); 1005e580952dSDimitry Andric DEBUG(dbgs() << " splitInsideBlock BB#" << MBB->getNumber() << " for " 1006e580952dSDimitry Andric << Uses.size() << " instructions.\n"); 1007e580952dSDimitry Andric assert(Uses.size() >= 3 && "Need at least 3 instructions"); 1008e580952dSDimitry Andric array_pod_sort(Uses.begin(), Uses.end()); 1009e580952dSDimitry Andric 1010e580952dSDimitry Andric // Simple algorithm: Find the largest gap between uses as determined by slot 1011e580952dSDimitry Andric // indices. Create new intervals for instructions before the gap and after the 1012e580952dSDimitry Andric // gap. 1013e580952dSDimitry Andric unsigned bestPos = 0; 1014e580952dSDimitry Andric int bestGap = 0; 1015e580952dSDimitry Andric DEBUG(dbgs() << " dist (" << Uses[0]); 1016e580952dSDimitry Andric for (unsigned i = 1, e = Uses.size(); i != e; ++i) { 1017e580952dSDimitry Andric int g = Uses[i-1].distance(Uses[i]); 1018e580952dSDimitry Andric DEBUG(dbgs() << ") -" << g << "- (" << Uses[i]); 1019e580952dSDimitry Andric if (g > bestGap) 1020e580952dSDimitry Andric bestPos = i, bestGap = g; 1021e580952dSDimitry Andric } 1022e580952dSDimitry Andric DEBUG(dbgs() << "), best: -" << bestGap << "-\n"); 1023e580952dSDimitry Andric 1024e580952dSDimitry Andric // bestPos points to the first use after the best gap. 1025e580952dSDimitry Andric assert(bestPos > 0 && "Invalid gap"); 1026e580952dSDimitry Andric 1027e580952dSDimitry Andric // FIXME: Don't create intervals for low densities. 1028e580952dSDimitry Andric 1029e580952dSDimitry Andric // First interval before the gap. Don't create single-instr intervals. 1030e580952dSDimitry Andric if (bestPos > 1) { 1031e580952dSDimitry Andric openIntv(); 10322754fe60SDimitry Andric useIntv(enterIntvBefore(Uses.front()), leaveIntvAfter(Uses[bestPos-1])); 1033e580952dSDimitry Andric closeIntv(); 1034e580952dSDimitry Andric } 1035e580952dSDimitry Andric 1036e580952dSDimitry Andric // Second interval after the gap. 1037e580952dSDimitry Andric if (bestPos < Uses.size()-1) { 1038e580952dSDimitry Andric openIntv(); 10392754fe60SDimitry Andric useIntv(enterIntvBefore(Uses[bestPos]), leaveIntvAfter(Uses.back())); 1040e580952dSDimitry Andric closeIntv(); 1041e580952dSDimitry Andric } 1042e580952dSDimitry Andric 10432754fe60SDimitry Andric finish(); 1044e580952dSDimitry Andric } 1045