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"
173b0f4066SDimitry Andric #include "llvm/ADT/Statistic.h"
18e580952dSDimitry Andric #include "llvm/CodeGen/LiveIntervalAnalysis.h"
19dff0c46cSDimitry Andric #include "llvm/CodeGen/LiveRangeEdit.h"
202754fe60SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
21e580952dSDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
226122f3e6SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h"
23e580952dSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
24139f7f9bSDimitry Andric #include "llvm/CodeGen/VirtRegMap.h"
25e580952dSDimitry Andric #include "llvm/Support/Debug.h"
26e580952dSDimitry Andric #include "llvm/Support/raw_ostream.h"
27e580952dSDimitry Andric #include "llvm/Target/TargetInstrInfo.h"
28e580952dSDimitry Andric #include "llvm/Target/TargetMachine.h"
29e580952dSDimitry Andric 
30e580952dSDimitry Andric using namespace llvm;
31e580952dSDimitry Andric 
323b0f4066SDimitry Andric STATISTIC(NumFinished, "Number of splits finished");
333b0f4066SDimitry Andric STATISTIC(NumSimple,   "Number of splits that were simple");
34bd5abe19SDimitry Andric STATISTIC(NumCopies,   "Number of copies inserted for splitting");
35bd5abe19SDimitry Andric STATISTIC(NumRemats,   "Number of rematerialized defs for splitting");
36bd5abe19SDimitry Andric STATISTIC(NumRepairs,  "Number of invalid live ranges repaired");
37e580952dSDimitry Andric 
38e580952dSDimitry Andric //===----------------------------------------------------------------------===//
39e580952dSDimitry Andric //                                 Split Analysis
40e580952dSDimitry Andric //===----------------------------------------------------------------------===//
41e580952dSDimitry Andric 
422754fe60SDimitry Andric SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm,
43e580952dSDimitry Andric                              const LiveIntervals &lis,
44e580952dSDimitry Andric                              const MachineLoopInfo &mli)
452754fe60SDimitry Andric   : MF(vrm.getMachineFunction()),
462754fe60SDimitry Andric     VRM(vrm),
472754fe60SDimitry Andric     LIS(lis),
482754fe60SDimitry Andric     Loops(mli),
492754fe60SDimitry Andric     TII(*MF.getTarget().getInstrInfo()),
503b0f4066SDimitry Andric     CurLI(0),
513b0f4066SDimitry Andric     LastSplitPoint(MF.getNumBlockIDs()) {}
52e580952dSDimitry Andric 
53e580952dSDimitry Andric void SplitAnalysis::clear() {
542754fe60SDimitry Andric   UseSlots.clear();
553b0f4066SDimitry Andric   UseBlocks.clear();
563b0f4066SDimitry Andric   ThroughBlocks.clear();
572754fe60SDimitry Andric   CurLI = 0;
58bd5abe19SDimitry Andric   DidRepairRange = false;
59e580952dSDimitry Andric }
60e580952dSDimitry Andric 
613b0f4066SDimitry Andric SlotIndex SplitAnalysis::computeLastSplitPoint(unsigned Num) {
623b0f4066SDimitry Andric   const MachineBasicBlock *MBB = MF.getBlockNumbered(Num);
633b0f4066SDimitry Andric   const MachineBasicBlock *LPad = MBB->getLandingPadSuccessor();
643b0f4066SDimitry Andric   std::pair<SlotIndex, SlotIndex> &LSP = LastSplitPoint[Num];
65dff0c46cSDimitry Andric   SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB);
663b0f4066SDimitry Andric 
673b0f4066SDimitry Andric   // Compute split points on the first call. The pair is independent of the
683b0f4066SDimitry Andric   // current live interval.
693b0f4066SDimitry Andric   if (!LSP.first.isValid()) {
703b0f4066SDimitry Andric     MachineBasicBlock::const_iterator FirstTerm = MBB->getFirstTerminator();
713b0f4066SDimitry Andric     if (FirstTerm == MBB->end())
72dff0c46cSDimitry Andric       LSP.first = MBBEnd;
733b0f4066SDimitry Andric     else
743b0f4066SDimitry Andric       LSP.first = LIS.getInstructionIndex(FirstTerm);
753b0f4066SDimitry Andric 
763b0f4066SDimitry Andric     // If there is a landing pad successor, also find the call instruction.
773b0f4066SDimitry Andric     if (!LPad)
783b0f4066SDimitry Andric       return LSP.first;
793b0f4066SDimitry Andric     // There may not be a call instruction (?) in which case we ignore LPad.
803b0f4066SDimitry Andric     LSP.second = LSP.first;
8117a519f9SDimitry Andric     for (MachineBasicBlock::const_iterator I = MBB->end(), E = MBB->begin();
8217a519f9SDimitry Andric          I != E;) {
8317a519f9SDimitry Andric       --I;
84dff0c46cSDimitry Andric       if (I->isCall()) {
853b0f4066SDimitry Andric         LSP.second = LIS.getInstructionIndex(I);
863b0f4066SDimitry Andric         break;
873b0f4066SDimitry Andric       }
883b0f4066SDimitry Andric     }
8917a519f9SDimitry Andric   }
903b0f4066SDimitry Andric 
913b0f4066SDimitry Andric   // If CurLI is live into a landing pad successor, move the last split point
923b0f4066SDimitry Andric   // back to the call that may throw.
93dff0c46cSDimitry Andric   if (!LPad || !LSP.second || !LIS.isLiveInToMBB(*CurLI, LPad))
943b0f4066SDimitry Andric     return LSP.first;
95dff0c46cSDimitry Andric 
96dff0c46cSDimitry Andric   // Find the value leaving MBB.
97dff0c46cSDimitry Andric   const VNInfo *VNI = CurLI->getVNInfoBefore(MBBEnd);
98dff0c46cSDimitry Andric   if (!VNI)
99dff0c46cSDimitry Andric     return LSP.first;
100dff0c46cSDimitry Andric 
101dff0c46cSDimitry Andric   // If the value leaving MBB was defined after the call in MBB, it can't
102dff0c46cSDimitry Andric   // really be live-in to the landing pad.  This can happen if the landing pad
103dff0c46cSDimitry Andric   // has a PHI, and this register is undef on the exceptional edge.
104dff0c46cSDimitry Andric   // <rdar://problem/10664933>
105dff0c46cSDimitry Andric   if (!SlotIndex::isEarlierInstr(VNI->def, LSP.second) && VNI->def < MBBEnd)
106dff0c46cSDimitry Andric     return LSP.first;
107dff0c46cSDimitry Andric 
108dff0c46cSDimitry Andric   // Value is properly live-in to the landing pad.
109dff0c46cSDimitry Andric   // Only allow splits before the call.
110dff0c46cSDimitry Andric   return LSP.second;
111dff0c46cSDimitry Andric }
112dff0c46cSDimitry Andric 
113dff0c46cSDimitry Andric MachineBasicBlock::iterator
114dff0c46cSDimitry Andric SplitAnalysis::getLastSplitPointIter(MachineBasicBlock *MBB) {
115dff0c46cSDimitry Andric   SlotIndex LSP = getLastSplitPoint(MBB->getNumber());
116dff0c46cSDimitry Andric   if (LSP == LIS.getMBBEndIdx(MBB))
117dff0c46cSDimitry Andric     return MBB->end();
118dff0c46cSDimitry Andric   return LIS.getInstructionFromIndex(LSP);
119e580952dSDimitry Andric }
120e580952dSDimitry Andric 
1212754fe60SDimitry Andric /// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
122e580952dSDimitry Andric void SplitAnalysis::analyzeUses() {
1233b0f4066SDimitry Andric   assert(UseSlots.empty() && "Call clear first");
1243b0f4066SDimitry Andric 
1253b0f4066SDimitry Andric   // First get all the defs from the interval values. This provides the correct
1263b0f4066SDimitry Andric   // slots for early clobbers.
1273b0f4066SDimitry Andric   for (LiveInterval::const_vni_iterator I = CurLI->vni_begin(),
1283b0f4066SDimitry Andric        E = CurLI->vni_end(); I != E; ++I)
1293b0f4066SDimitry Andric     if (!(*I)->isPHIDef() && !(*I)->isUnused())
1303b0f4066SDimitry Andric       UseSlots.push_back((*I)->def);
1313b0f4066SDimitry Andric 
1323b0f4066SDimitry Andric   // Get use slots form the use-def chain.
1332754fe60SDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
1343b0f4066SDimitry Andric   for (MachineRegisterInfo::use_nodbg_iterator
1353b0f4066SDimitry Andric        I = MRI.use_nodbg_begin(CurLI->reg), E = MRI.use_nodbg_end(); I != E;
1363b0f4066SDimitry Andric        ++I)
1373b0f4066SDimitry Andric     if (!I.getOperand().isUndef())
138dff0c46cSDimitry Andric       UseSlots.push_back(LIS.getInstructionIndex(&*I).getRegSlot());
1393b0f4066SDimitry Andric 
1402754fe60SDimitry Andric   array_pod_sort(UseSlots.begin(), UseSlots.end());
1413b0f4066SDimitry Andric 
1423b0f4066SDimitry Andric   // Remove duplicates, keeping the smaller slot for each instruction.
1433b0f4066SDimitry Andric   // That is what we want for early clobbers.
1443b0f4066SDimitry Andric   UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
1453b0f4066SDimitry Andric                              SlotIndex::isSameInstr),
1463b0f4066SDimitry Andric                  UseSlots.end());
1473b0f4066SDimitry Andric 
1483b0f4066SDimitry Andric   // Compute per-live block info.
1493b0f4066SDimitry Andric   if (!calcLiveBlockInfo()) {
1503b0f4066SDimitry Andric     // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
15117a519f9SDimitry Andric     // I am looking at you, RegisterCoalescer!
152bd5abe19SDimitry Andric     DidRepairRange = true;
153bd5abe19SDimitry Andric     ++NumRepairs;
1543b0f4066SDimitry Andric     DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
1553b0f4066SDimitry Andric     const_cast<LiveIntervals&>(LIS)
1563b0f4066SDimitry Andric       .shrinkToUses(const_cast<LiveInterval*>(CurLI));
1573b0f4066SDimitry Andric     UseBlocks.clear();
1583b0f4066SDimitry Andric     ThroughBlocks.clear();
1593b0f4066SDimitry Andric     bool fixed = calcLiveBlockInfo();
1603b0f4066SDimitry Andric     (void)fixed;
1613b0f4066SDimitry Andric     assert(fixed && "Couldn't fix broken live interval");
1623b0f4066SDimitry Andric   }
1633b0f4066SDimitry Andric 
1643b0f4066SDimitry Andric   DEBUG(dbgs() << "Analyze counted "
1653b0f4066SDimitry Andric                << UseSlots.size() << " instrs in "
1663b0f4066SDimitry Andric                << UseBlocks.size() << " blocks, through "
1673b0f4066SDimitry Andric                << NumThroughBlocks << " blocks.\n");
168e580952dSDimitry Andric }
169e580952dSDimitry Andric 
1702754fe60SDimitry Andric /// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
1712754fe60SDimitry Andric /// where CurLI is live.
1723b0f4066SDimitry Andric bool SplitAnalysis::calcLiveBlockInfo() {
1733b0f4066SDimitry Andric   ThroughBlocks.resize(MF.getNumBlockIDs());
174bd5abe19SDimitry Andric   NumThroughBlocks = NumGapBlocks = 0;
1752754fe60SDimitry Andric   if (CurLI->empty())
1763b0f4066SDimitry Andric     return true;
177e580952dSDimitry Andric 
1782754fe60SDimitry Andric   LiveInterval::const_iterator LVI = CurLI->begin();
1792754fe60SDimitry Andric   LiveInterval::const_iterator LVE = CurLI->end();
180e580952dSDimitry Andric 
1812754fe60SDimitry Andric   SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
1822754fe60SDimitry Andric   UseI = UseSlots.begin();
1832754fe60SDimitry Andric   UseE = UseSlots.end();
1842754fe60SDimitry Andric 
1852754fe60SDimitry Andric   // Loop over basic blocks where CurLI is live.
1862754fe60SDimitry Andric   MachineFunction::iterator MFI = LIS.getMBBFromIndex(LVI->start);
1872754fe60SDimitry Andric   for (;;) {
1882754fe60SDimitry Andric     BlockInfo BI;
1892754fe60SDimitry Andric     BI.MBB = MFI;
1902754fe60SDimitry Andric     SlotIndex Start, Stop;
1912754fe60SDimitry Andric     tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
1922754fe60SDimitry Andric 
193bd5abe19SDimitry Andric     // If the block contains no uses, the range must be live through. At one
19417a519f9SDimitry Andric     // point, RegisterCoalescer could create dangling ranges that ended
195bd5abe19SDimitry Andric     // mid-block.
196bd5abe19SDimitry Andric     if (UseI == UseE || *UseI >= Stop) {
197bd5abe19SDimitry Andric       ++NumThroughBlocks;
198bd5abe19SDimitry Andric       ThroughBlocks.set(BI.MBB->getNumber());
199bd5abe19SDimitry Andric       // The range shouldn't end mid-block if there are no uses. This shouldn't
200bd5abe19SDimitry Andric       // happen.
201bd5abe19SDimitry Andric       if (LVI->end < Stop)
202bd5abe19SDimitry Andric         return false;
203bd5abe19SDimitry Andric     } else {
204bd5abe19SDimitry Andric       // This block has uses. Find the first and last uses in the block.
2056122f3e6SDimitry Andric       BI.FirstInstr = *UseI;
2066122f3e6SDimitry Andric       assert(BI.FirstInstr >= Start);
2072754fe60SDimitry Andric       do ++UseI;
2082754fe60SDimitry Andric       while (UseI != UseE && *UseI < Stop);
2096122f3e6SDimitry Andric       BI.LastInstr = UseI[-1];
2106122f3e6SDimitry Andric       assert(BI.LastInstr < Stop);
211bd5abe19SDimitry Andric 
212bd5abe19SDimitry Andric       // LVI is the first live segment overlapping MBB.
213bd5abe19SDimitry Andric       BI.LiveIn = LVI->start <= Start;
214e580952dSDimitry Andric 
2156122f3e6SDimitry Andric       // When not live in, the first use should be a def.
2166122f3e6SDimitry Andric       if (!BI.LiveIn) {
217f785676fSDimitry Andric         assert(LVI->start == LVI->valno->def && "Dangling Segment start");
2186122f3e6SDimitry Andric         assert(LVI->start == BI.FirstInstr && "First instr should be a def");
2196122f3e6SDimitry Andric         BI.FirstDef = BI.FirstInstr;
2206122f3e6SDimitry Andric       }
2216122f3e6SDimitry Andric 
2222754fe60SDimitry Andric       // Look for gaps in the live range.
2232754fe60SDimitry Andric       BI.LiveOut = true;
2242754fe60SDimitry Andric       while (LVI->end < Stop) {
2252754fe60SDimitry Andric         SlotIndex LastStop = LVI->end;
2262754fe60SDimitry Andric         if (++LVI == LVE || LVI->start >= Stop) {
2272754fe60SDimitry Andric           BI.LiveOut = false;
2286122f3e6SDimitry Andric           BI.LastInstr = LastStop;
229e580952dSDimitry Andric           break;
230e580952dSDimitry Andric         }
2316122f3e6SDimitry Andric 
2322754fe60SDimitry Andric         if (LastStop < LVI->start) {
233bd5abe19SDimitry Andric           // There is a gap in the live range. Create duplicate entries for the
234bd5abe19SDimitry Andric           // live-in snippet and the live-out snippet.
235bd5abe19SDimitry Andric           ++NumGapBlocks;
236bd5abe19SDimitry Andric 
237bd5abe19SDimitry Andric           // Push the Live-in part.
238bd5abe19SDimitry Andric           BI.LiveOut = false;
239bd5abe19SDimitry Andric           UseBlocks.push_back(BI);
2406122f3e6SDimitry Andric           UseBlocks.back().LastInstr = LastStop;
241bd5abe19SDimitry Andric 
242bd5abe19SDimitry Andric           // Set up BI for the live-out part.
243bd5abe19SDimitry Andric           BI.LiveIn = false;
244bd5abe19SDimitry Andric           BI.LiveOut = true;
2456122f3e6SDimitry Andric           BI.FirstInstr = BI.FirstDef = LVI->start;
246e580952dSDimitry Andric         }
247e580952dSDimitry Andric 
248f785676fSDimitry Andric         // A Segment that starts in the middle of the block must be a def.
249f785676fSDimitry Andric         assert(LVI->start == LVI->valno->def && "Dangling Segment start");
2506122f3e6SDimitry Andric         if (!BI.FirstDef)
2516122f3e6SDimitry Andric           BI.FirstDef = LVI->start;
2526122f3e6SDimitry Andric       }
2536122f3e6SDimitry Andric 
2543b0f4066SDimitry Andric       UseBlocks.push_back(BI);
255e580952dSDimitry Andric 
2562754fe60SDimitry Andric       // LVI is now at LVE or LVI->end >= Stop.
2572754fe60SDimitry Andric       if (LVI == LVE)
2582754fe60SDimitry Andric         break;
259bd5abe19SDimitry Andric     }
2602754fe60SDimitry Andric 
2612754fe60SDimitry Andric     // Live segment ends exactly at Stop. Move to the next segment.
2622754fe60SDimitry Andric     if (LVI->end == Stop && ++LVI == LVE)
2632754fe60SDimitry Andric       break;
2642754fe60SDimitry Andric 
2652754fe60SDimitry Andric     // Pick the next basic block.
2662754fe60SDimitry Andric     if (LVI->start < Stop)
2672754fe60SDimitry Andric       ++MFI;
2682754fe60SDimitry Andric     else
2692754fe60SDimitry Andric       MFI = LIS.getMBBFromIndex(LVI->start);
2702754fe60SDimitry Andric   }
271bd5abe19SDimitry Andric 
272bd5abe19SDimitry Andric   assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
2733b0f4066SDimitry Andric   return true;
2743b0f4066SDimitry Andric }
2753b0f4066SDimitry Andric 
2763b0f4066SDimitry Andric unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
2773b0f4066SDimitry Andric   if (cli->empty())
2783b0f4066SDimitry Andric     return 0;
2793b0f4066SDimitry Andric   LiveInterval *li = const_cast<LiveInterval*>(cli);
2803b0f4066SDimitry Andric   LiveInterval::iterator LVI = li->begin();
2813b0f4066SDimitry Andric   LiveInterval::iterator LVE = li->end();
2823b0f4066SDimitry Andric   unsigned Count = 0;
2833b0f4066SDimitry Andric 
2843b0f4066SDimitry Andric   // Loop over basic blocks where li is live.
2853b0f4066SDimitry Andric   MachineFunction::const_iterator MFI = LIS.getMBBFromIndex(LVI->start);
2863b0f4066SDimitry Andric   SlotIndex Stop = LIS.getMBBEndIdx(MFI);
2873b0f4066SDimitry Andric   for (;;) {
2883b0f4066SDimitry Andric     ++Count;
2893b0f4066SDimitry Andric     LVI = li->advanceTo(LVI, Stop);
2903b0f4066SDimitry Andric     if (LVI == LVE)
2913b0f4066SDimitry Andric       return Count;
2923b0f4066SDimitry Andric     do {
2933b0f4066SDimitry Andric       ++MFI;
2943b0f4066SDimitry Andric       Stop = LIS.getMBBEndIdx(MFI);
2953b0f4066SDimitry Andric     } while (Stop <= LVI->start);
2963b0f4066SDimitry Andric   }
297e580952dSDimitry Andric }
298e580952dSDimitry Andric 
299dd6029ffSDimitry Andric bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
300dd6029ffSDimitry Andric   unsigned OrigReg = VRM.getOriginal(CurLI->reg);
301dd6029ffSDimitry Andric   const LiveInterval &Orig = LIS.getInterval(OrigReg);
302dd6029ffSDimitry Andric   assert(!Orig.empty() && "Splitting empty interval?");
303dd6029ffSDimitry Andric   LiveInterval::const_iterator I = Orig.find(Idx);
304dd6029ffSDimitry Andric 
305dd6029ffSDimitry Andric   // Range containing Idx should begin at Idx.
306dd6029ffSDimitry Andric   if (I != Orig.end() && I->start <= Idx)
307dd6029ffSDimitry Andric     return I->start == Idx;
308dd6029ffSDimitry Andric 
309dd6029ffSDimitry Andric   // Range does not contain Idx, previous must end at Idx.
310dd6029ffSDimitry Andric   return I != Orig.begin() && (--I)->end == Idx;
311dd6029ffSDimitry Andric }
312dd6029ffSDimitry Andric 
313e580952dSDimitry Andric void SplitAnalysis::analyze(const LiveInterval *li) {
314e580952dSDimitry Andric   clear();
3152754fe60SDimitry Andric   CurLI = li;
316e580952dSDimitry Andric   analyzeUses();
317e580952dSDimitry Andric }
318e580952dSDimitry Andric 
319e580952dSDimitry Andric 
320e580952dSDimitry Andric //===----------------------------------------------------------------------===//
3213b0f4066SDimitry Andric //                               Split Editor
322e580952dSDimitry Andric //===----------------------------------------------------------------------===//
323e580952dSDimitry Andric 
3243b0f4066SDimitry Andric /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
3253b0f4066SDimitry Andric SplitEditor::SplitEditor(SplitAnalysis &sa,
3263b0f4066SDimitry Andric                          LiveIntervals &lis,
3273b0f4066SDimitry Andric                          VirtRegMap &vrm,
328f785676fSDimitry Andric                          MachineDominatorTree &mdt,
329f785676fSDimitry Andric                          MachineBlockFrequencyInfo &mbfi)
3303b0f4066SDimitry Andric   : SA(sa), LIS(lis), VRM(vrm),
3313b0f4066SDimitry Andric     MRI(vrm.getMachineFunction().getRegInfo()),
3323b0f4066SDimitry Andric     MDT(mdt),
3333b0f4066SDimitry Andric     TII(*vrm.getMachineFunction().getTarget().getInstrInfo()),
3343b0f4066SDimitry Andric     TRI(*vrm.getMachineFunction().getTarget().getRegisterInfo()),
335f785676fSDimitry Andric     MBFI(mbfi),
3363b0f4066SDimitry Andric     Edit(0),
3373b0f4066SDimitry Andric     OpenIdx(0),
3386122f3e6SDimitry Andric     SpillMode(SM_Partition),
3393b0f4066SDimitry Andric     RegAssign(Allocator)
3403b0f4066SDimitry Andric {}
341e580952dSDimitry Andric 
3426122f3e6SDimitry Andric void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
3436122f3e6SDimitry Andric   Edit = &LRE;
3446122f3e6SDimitry Andric   SpillMode = SM;
3453b0f4066SDimitry Andric   OpenIdx = 0;
3463b0f4066SDimitry Andric   RegAssign.clear();
3472754fe60SDimitry Andric   Values.clear();
3483b0f4066SDimitry Andric 
3496122f3e6SDimitry Andric   // Reset the LiveRangeCalc instances needed for this spill mode.
3507ae0e2c9SDimitry Andric   LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
3517ae0e2c9SDimitry Andric                   &LIS.getVNInfoAllocator());
3526122f3e6SDimitry Andric   if (SpillMode)
3537ae0e2c9SDimitry Andric     LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
3547ae0e2c9SDimitry Andric                     &LIS.getVNInfoAllocator());
3553b0f4066SDimitry Andric 
3563b0f4066SDimitry Andric   // We don't need an AliasAnalysis since we will only be performing
3573b0f4066SDimitry Andric   // cheap-as-a-copy remats anyway.
358dff0c46cSDimitry Andric   Edit->anyRematerializable(0);
3592754fe60SDimitry Andric }
3602754fe60SDimitry Andric 
3613861d79fSDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3623b0f4066SDimitry Andric void SplitEditor::dump() const {
3633b0f4066SDimitry Andric   if (RegAssign.empty()) {
3643b0f4066SDimitry Andric     dbgs() << " empty\n";
3653b0f4066SDimitry Andric     return;
3662754fe60SDimitry Andric   }
3672754fe60SDimitry Andric 
3683b0f4066SDimitry Andric   for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
3693b0f4066SDimitry Andric     dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
3703b0f4066SDimitry Andric   dbgs() << '\n';
3713b0f4066SDimitry Andric }
3723861d79fSDimitry Andric #endif
3733b0f4066SDimitry Andric 
3743b0f4066SDimitry Andric VNInfo *SplitEditor::defValue(unsigned RegIdx,
3753b0f4066SDimitry Andric                               const VNInfo *ParentVNI,
3763b0f4066SDimitry Andric                               SlotIndex Idx) {
377e580952dSDimitry Andric   assert(ParentVNI && "Mapping  NULL value");
378e580952dSDimitry Andric   assert(Idx.isValid() && "Invalid SlotIndex");
3793b0f4066SDimitry Andric   assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
380f785676fSDimitry Andric   LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
3812754fe60SDimitry Andric 
3822754fe60SDimitry Andric   // Create a new value.
383dff0c46cSDimitry Andric   VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
3842754fe60SDimitry Andric 
385e580952dSDimitry Andric   // Use insert for lookup, so we can add missing values with a second lookup.
386e580952dSDimitry Andric   std::pair<ValueMap::iterator, bool> InsP =
3876122f3e6SDimitry Andric     Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id),
3886122f3e6SDimitry Andric                                  ValueForcePair(VNI, false)));
3892754fe60SDimitry Andric 
3903b0f4066SDimitry Andric   // This was the first time (RegIdx, ParentVNI) was mapped.
3913b0f4066SDimitry Andric   // Keep it as a simple def without any liveness.
3923b0f4066SDimitry Andric   if (InsP.second)
3933b0f4066SDimitry Andric     return VNI;
3943b0f4066SDimitry Andric 
3953b0f4066SDimitry Andric   // If the previous value was a simple mapping, add liveness for it now.
3966122f3e6SDimitry Andric   if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
3973b0f4066SDimitry Andric     SlotIndex Def = OldVNI->def;
398f785676fSDimitry Andric     LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), OldVNI));
3996122f3e6SDimitry Andric     // No longer a simple mapping.  Switch to a complex, non-forced mapping.
4006122f3e6SDimitry Andric     InsP.first->second = ValueForcePair();
4013b0f4066SDimitry Andric   }
4023b0f4066SDimitry Andric 
4033b0f4066SDimitry Andric   // This is a complex mapping, add liveness for VNI
4043b0f4066SDimitry Andric   SlotIndex Def = VNI->def;
405f785676fSDimitry Andric   LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
4062754fe60SDimitry Andric 
4072754fe60SDimitry Andric   return VNI;
4082754fe60SDimitry Andric }
4092754fe60SDimitry Andric 
4106122f3e6SDimitry Andric void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) {
4112754fe60SDimitry Andric   assert(ParentVNI && "Mapping  NULL value");
4126122f3e6SDimitry Andric   ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)];
4136122f3e6SDimitry Andric   VNInfo *VNI = VFP.getPointer();
4143b0f4066SDimitry Andric 
4156122f3e6SDimitry Andric   // ParentVNI was either unmapped or already complex mapped. Either way, just
4166122f3e6SDimitry Andric   // set the force bit.
4176122f3e6SDimitry Andric   if (!VNI) {
4186122f3e6SDimitry Andric     VFP.setInt(true);
4193b0f4066SDimitry Andric     return;
4206122f3e6SDimitry Andric   }
4213b0f4066SDimitry Andric 
4223b0f4066SDimitry Andric   // This was previously a single mapping. Make sure the old def is represented
4233b0f4066SDimitry Andric   // by a trivial live range.
4243b0f4066SDimitry Andric   SlotIndex Def = VNI->def;
425f785676fSDimitry Andric   LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
426f785676fSDimitry Andric   LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
4276122f3e6SDimitry Andric   // Mark as complex mapped, forced.
4286122f3e6SDimitry Andric   VFP = ValueForcePair(0, true);
429e580952dSDimitry Andric }
430e580952dSDimitry Andric 
4312754fe60SDimitry Andric VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
4322754fe60SDimitry Andric                                    VNInfo *ParentVNI,
4332754fe60SDimitry Andric                                    SlotIndex UseIdx,
434e580952dSDimitry Andric                                    MachineBasicBlock &MBB,
435e580952dSDimitry Andric                                    MachineBasicBlock::iterator I) {
4362754fe60SDimitry Andric   MachineInstr *CopyMI = 0;
4372754fe60SDimitry Andric   SlotIndex Def;
438f785676fSDimitry Andric   LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
4393b0f4066SDimitry Andric 
4403b0f4066SDimitry Andric   // We may be trying to avoid interference that ends at a deleted instruction,
4413b0f4066SDimitry Andric   // so always begin RegIdx 0 early and all others late.
4423b0f4066SDimitry Andric   bool Late = RegIdx != 0;
4432754fe60SDimitry Andric 
4442754fe60SDimitry Andric   // Attempt cheap-as-a-copy rematerialization.
4452754fe60SDimitry Andric   LiveRangeEdit::Remat RM(ParentVNI);
446dff0c46cSDimitry Andric   if (Edit->canRematerializeAt(RM, UseIdx, true)) {
447dff0c46cSDimitry Andric     Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, TRI, Late);
448bd5abe19SDimitry Andric     ++NumRemats;
4492754fe60SDimitry Andric   } else {
4502754fe60SDimitry Andric     // Can't remat, just insert a copy from parent.
4512754fe60SDimitry Andric     CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
4523b0f4066SDimitry Andric                .addReg(Edit->getReg());
4533b0f4066SDimitry Andric     Def = LIS.getSlotIndexes()->insertMachineInstrInMaps(CopyMI, Late)
454dff0c46cSDimitry Andric             .getRegSlot();
455bd5abe19SDimitry Andric     ++NumCopies;
4562754fe60SDimitry Andric   }
4572754fe60SDimitry Andric 
4582754fe60SDimitry Andric   // Define the value in Reg.
459dff0c46cSDimitry Andric   return defValue(RegIdx, ParentVNI, Def);
460e580952dSDimitry Andric }
461e580952dSDimitry Andric 
462e580952dSDimitry Andric /// Create a new virtual register and live interval.
4633b0f4066SDimitry Andric unsigned SplitEditor::openIntv() {
4642754fe60SDimitry Andric   // Create the complement as index 0.
4653b0f4066SDimitry Andric   if (Edit->empty())
466f785676fSDimitry Andric     Edit->createEmptyInterval();
467e580952dSDimitry Andric 
4682754fe60SDimitry Andric   // Create the open interval.
4693b0f4066SDimitry Andric   OpenIdx = Edit->size();
470f785676fSDimitry Andric   Edit->createEmptyInterval();
4713b0f4066SDimitry Andric   return OpenIdx;
4723b0f4066SDimitry Andric }
4733b0f4066SDimitry Andric 
4743b0f4066SDimitry Andric void SplitEditor::selectIntv(unsigned Idx) {
4753b0f4066SDimitry Andric   assert(Idx != 0 && "Cannot select the complement interval");
4763b0f4066SDimitry Andric   assert(Idx < Edit->size() && "Can only select previously opened interval");
47717a519f9SDimitry Andric   DEBUG(dbgs() << "    selectIntv " << OpenIdx << " -> " << Idx << '\n');
4783b0f4066SDimitry Andric   OpenIdx = Idx;
4792754fe60SDimitry Andric }
480e580952dSDimitry Andric 
4812754fe60SDimitry Andric SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
4822754fe60SDimitry Andric   assert(OpenIdx && "openIntv not called before enterIntvBefore");
4832754fe60SDimitry Andric   DEBUG(dbgs() << "    enterIntvBefore " << Idx);
4842754fe60SDimitry Andric   Idx = Idx.getBaseIndex();
4853b0f4066SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
4862754fe60SDimitry Andric   if (!ParentVNI) {
4872754fe60SDimitry Andric     DEBUG(dbgs() << ": not live\n");
4882754fe60SDimitry Andric     return Idx;
4892754fe60SDimitry Andric   }
4902754fe60SDimitry Andric   DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
4912754fe60SDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
492e580952dSDimitry Andric   assert(MI && "enterIntvBefore called with invalid index");
493e580952dSDimitry Andric 
4942754fe60SDimitry Andric   VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
4952754fe60SDimitry Andric   return VNI->def;
496e580952dSDimitry Andric }
497e580952dSDimitry Andric 
49817a519f9SDimitry Andric SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
49917a519f9SDimitry Andric   assert(OpenIdx && "openIntv not called before enterIntvAfter");
50017a519f9SDimitry Andric   DEBUG(dbgs() << "    enterIntvAfter " << Idx);
50117a519f9SDimitry Andric   Idx = Idx.getBoundaryIndex();
50217a519f9SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
50317a519f9SDimitry Andric   if (!ParentVNI) {
50417a519f9SDimitry Andric     DEBUG(dbgs() << ": not live\n");
50517a519f9SDimitry Andric     return Idx;
50617a519f9SDimitry Andric   }
50717a519f9SDimitry Andric   DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
50817a519f9SDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
50917a519f9SDimitry Andric   assert(MI && "enterIntvAfter called with invalid index");
51017a519f9SDimitry Andric 
51117a519f9SDimitry Andric   VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
51217a519f9SDimitry Andric                               llvm::next(MachineBasicBlock::iterator(MI)));
51317a519f9SDimitry Andric   return VNI->def;
51417a519f9SDimitry Andric }
51517a519f9SDimitry Andric 
5162754fe60SDimitry Andric SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
5172754fe60SDimitry Andric   assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
5182754fe60SDimitry Andric   SlotIndex End = LIS.getMBBEndIdx(&MBB);
5192754fe60SDimitry Andric   SlotIndex Last = End.getPrevSlot();
5202754fe60SDimitry Andric   DEBUG(dbgs() << "    enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
5213b0f4066SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
5222754fe60SDimitry Andric   if (!ParentVNI) {
5232754fe60SDimitry Andric     DEBUG(dbgs() << ": not live\n");
5242754fe60SDimitry Andric     return End;
5252754fe60SDimitry Andric   }
5262754fe60SDimitry Andric   DEBUG(dbgs() << ": valno " << ParentVNI->id);
5272754fe60SDimitry Andric   VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
528dff0c46cSDimitry Andric                               SA.getLastSplitPointIter(&MBB));
5292754fe60SDimitry Andric   RegAssign.insert(VNI->def, End, OpenIdx);
5302754fe60SDimitry Andric   DEBUG(dump());
5312754fe60SDimitry Andric   return VNI->def;
532e580952dSDimitry Andric }
533e580952dSDimitry Andric 
5342754fe60SDimitry Andric /// useIntv - indicate that all instructions in MBB should use OpenLI.
535e580952dSDimitry Andric void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
5362754fe60SDimitry Andric   useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
537e580952dSDimitry Andric }
538e580952dSDimitry Andric 
539e580952dSDimitry Andric void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
5402754fe60SDimitry Andric   assert(OpenIdx && "openIntv not called before useIntv");
5412754fe60SDimitry Andric   DEBUG(dbgs() << "    useIntv [" << Start << ';' << End << "):");
5422754fe60SDimitry Andric   RegAssign.insert(Start, End, OpenIdx);
5432754fe60SDimitry Andric   DEBUG(dump());
544e580952dSDimitry Andric }
545e580952dSDimitry Andric 
5462754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
5472754fe60SDimitry Andric   assert(OpenIdx && "openIntv not called before leaveIntvAfter");
5482754fe60SDimitry Andric   DEBUG(dbgs() << "    leaveIntvAfter " << Idx);
5492754fe60SDimitry Andric 
5502754fe60SDimitry Andric   // The interval must be live beyond the instruction at Idx.
5516122f3e6SDimitry Andric   SlotIndex Boundary = Idx.getBoundaryIndex();
5526122f3e6SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
5532754fe60SDimitry Andric   if (!ParentVNI) {
5542754fe60SDimitry Andric     DEBUG(dbgs() << ": not live\n");
5556122f3e6SDimitry Andric     return Boundary.getNextSlot();
5562754fe60SDimitry Andric   }
5572754fe60SDimitry Andric   DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
5586122f3e6SDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
5592754fe60SDimitry Andric   assert(MI && "No instruction at index");
5606122f3e6SDimitry Andric 
5616122f3e6SDimitry Andric   // In spill mode, make live ranges as short as possible by inserting the copy
5626122f3e6SDimitry Andric   // before MI.  This is only possible if that instruction doesn't redefine the
5636122f3e6SDimitry Andric   // value.  The inserted COPY is not a kill, and we don't need to recompute
5646122f3e6SDimitry Andric   // the source live range.  The spiller also won't try to hoist this copy.
5656122f3e6SDimitry Andric   if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
5666122f3e6SDimitry Andric       MI->readsVirtualRegister(Edit->getReg())) {
5676122f3e6SDimitry Andric     forceRecompute(0, ParentVNI);
5686122f3e6SDimitry Andric     defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
5696122f3e6SDimitry Andric     return Idx;
5706122f3e6SDimitry Andric   }
5716122f3e6SDimitry Andric 
5726122f3e6SDimitry Andric   VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
5732754fe60SDimitry Andric                               llvm::next(MachineBasicBlock::iterator(MI)));
5742754fe60SDimitry Andric   return VNI->def;
575e580952dSDimitry Andric }
576e580952dSDimitry Andric 
5772754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
5782754fe60SDimitry Andric   assert(OpenIdx && "openIntv not called before leaveIntvBefore");
5792754fe60SDimitry Andric   DEBUG(dbgs() << "    leaveIntvBefore " << Idx);
580e580952dSDimitry Andric 
5812754fe60SDimitry Andric   // The interval must be live into the instruction at Idx.
5826122f3e6SDimitry Andric   Idx = Idx.getBaseIndex();
5833b0f4066SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
5842754fe60SDimitry Andric   if (!ParentVNI) {
5852754fe60SDimitry Andric     DEBUG(dbgs() << ": not live\n");
5862754fe60SDimitry Andric     return Idx.getNextSlot();
5872754fe60SDimitry Andric   }
5882754fe60SDimitry Andric   DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
5892754fe60SDimitry Andric 
5902754fe60SDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
5912754fe60SDimitry Andric   assert(MI && "No instruction at index");
5922754fe60SDimitry Andric   VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
5932754fe60SDimitry Andric   return VNI->def;
594e580952dSDimitry Andric }
595e580952dSDimitry Andric 
5962754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
5972754fe60SDimitry Andric   assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
5982754fe60SDimitry Andric   SlotIndex Start = LIS.getMBBStartIdx(&MBB);
5992754fe60SDimitry Andric   DEBUG(dbgs() << "    leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
6002754fe60SDimitry Andric 
6013b0f4066SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
6022754fe60SDimitry Andric   if (!ParentVNI) {
6032754fe60SDimitry Andric     DEBUG(dbgs() << ": not live\n");
6042754fe60SDimitry Andric     return Start;
605e580952dSDimitry Andric   }
606e580952dSDimitry Andric 
6072754fe60SDimitry Andric   VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
6082754fe60SDimitry Andric                               MBB.SkipPHIsAndLabels(MBB.begin()));
6092754fe60SDimitry Andric   RegAssign.insert(Start, VNI->def, OpenIdx);
6102754fe60SDimitry Andric   DEBUG(dump());
6112754fe60SDimitry Andric   return VNI->def;
612e580952dSDimitry Andric }
613e580952dSDimitry Andric 
6142754fe60SDimitry Andric void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
6152754fe60SDimitry Andric   assert(OpenIdx && "openIntv not called before overlapIntv");
6163b0f4066SDimitry Andric   const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
617dff0c46cSDimitry Andric   assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
6182754fe60SDimitry Andric          "Parent changes value in extended range");
6192754fe60SDimitry Andric   assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
6202754fe60SDimitry Andric          "Range cannot span basic blocks");
621e580952dSDimitry Andric 
6226122f3e6SDimitry Andric   // The complement interval will be extended as needed by LRCalc.extend().
6233b0f4066SDimitry Andric   if (ParentVNI)
6246122f3e6SDimitry Andric     forceRecompute(0, ParentVNI);
6252754fe60SDimitry Andric   DEBUG(dbgs() << "    overlapIntv [" << Start << ';' << End << "):");
6262754fe60SDimitry Andric   RegAssign.insert(Start, End, OpenIdx);
6272754fe60SDimitry Andric   DEBUG(dump());
628e580952dSDimitry Andric }
629e580952dSDimitry Andric 
6306122f3e6SDimitry Andric //===----------------------------------------------------------------------===//
6316122f3e6SDimitry Andric //                                  Spill modes
6326122f3e6SDimitry Andric //===----------------------------------------------------------------------===//
6336122f3e6SDimitry Andric 
6346122f3e6SDimitry Andric void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
635f785676fSDimitry Andric   LiveInterval *LI = &LIS.getInterval(Edit->get(0));
6366122f3e6SDimitry Andric   DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
6376122f3e6SDimitry Andric   RegAssignMap::iterator AssignI;
6386122f3e6SDimitry Andric   AssignI.setMap(RegAssign);
6396122f3e6SDimitry Andric 
6406122f3e6SDimitry Andric   for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
6416122f3e6SDimitry Andric     VNInfo *VNI = Copies[i];
6426122f3e6SDimitry Andric     SlotIndex Def = VNI->def;
6436122f3e6SDimitry Andric     MachineInstr *MI = LIS.getInstructionFromIndex(Def);
6446122f3e6SDimitry Andric     assert(MI && "No instruction for back-copy");
6456122f3e6SDimitry Andric 
6466122f3e6SDimitry Andric     MachineBasicBlock *MBB = MI->getParent();
6476122f3e6SDimitry Andric     MachineBasicBlock::iterator MBBI(MI);
6486122f3e6SDimitry Andric     bool AtBegin;
6496122f3e6SDimitry Andric     do AtBegin = MBBI == MBB->begin();
6506122f3e6SDimitry Andric     while (!AtBegin && (--MBBI)->isDebugValue());
6516122f3e6SDimitry Andric 
6526122f3e6SDimitry Andric     DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
6536122f3e6SDimitry Andric     LI->removeValNo(VNI);
6546122f3e6SDimitry Andric     LIS.RemoveMachineInstrFromMaps(MI);
6556122f3e6SDimitry Andric     MI->eraseFromParent();
6566122f3e6SDimitry Andric 
6576122f3e6SDimitry Andric     // Adjust RegAssign if a register assignment is killed at VNI->def.  We
6586122f3e6SDimitry Andric     // want to avoid calculating the live range of the source register if
6596122f3e6SDimitry Andric     // possible.
6607ae0e2c9SDimitry Andric     AssignI.find(Def.getPrevSlot());
6616122f3e6SDimitry Andric     if (!AssignI.valid() || AssignI.start() >= Def)
6626122f3e6SDimitry Andric       continue;
6636122f3e6SDimitry Andric     // If MI doesn't kill the assigned register, just leave it.
6646122f3e6SDimitry Andric     if (AssignI.stop() != Def)
6656122f3e6SDimitry Andric       continue;
6666122f3e6SDimitry Andric     unsigned RegIdx = AssignI.value();
6676122f3e6SDimitry Andric     if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) {
6686122f3e6SDimitry Andric       DEBUG(dbgs() << "  cannot find simple kill of RegIdx " << RegIdx << '\n');
6696122f3e6SDimitry Andric       forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def));
6706122f3e6SDimitry Andric     } else {
671dff0c46cSDimitry Andric       SlotIndex Kill = LIS.getInstructionIndex(MBBI).getRegSlot();
6726122f3e6SDimitry Andric       DEBUG(dbgs() << "  move kill to " << Kill << '\t' << *MBBI);
6736122f3e6SDimitry Andric       AssignI.setStop(Kill);
6746122f3e6SDimitry Andric     }
6756122f3e6SDimitry Andric   }
6766122f3e6SDimitry Andric }
6776122f3e6SDimitry Andric 
6786122f3e6SDimitry Andric MachineBasicBlock*
6796122f3e6SDimitry Andric SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
6806122f3e6SDimitry Andric                                   MachineBasicBlock *DefMBB) {
6816122f3e6SDimitry Andric   if (MBB == DefMBB)
6826122f3e6SDimitry Andric     return MBB;
6836122f3e6SDimitry Andric   assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
6846122f3e6SDimitry Andric 
6856122f3e6SDimitry Andric   const MachineLoopInfo &Loops = SA.Loops;
6866122f3e6SDimitry Andric   const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
6876122f3e6SDimitry Andric   MachineDomTreeNode *DefDomNode = MDT[DefMBB];
6886122f3e6SDimitry Andric 
6896122f3e6SDimitry Andric   // Best candidate so far.
6906122f3e6SDimitry Andric   MachineBasicBlock *BestMBB = MBB;
6916122f3e6SDimitry Andric   unsigned BestDepth = UINT_MAX;
6926122f3e6SDimitry Andric 
6936122f3e6SDimitry Andric   for (;;) {
6946122f3e6SDimitry Andric     const MachineLoop *Loop = Loops.getLoopFor(MBB);
6956122f3e6SDimitry Andric 
6966122f3e6SDimitry Andric     // MBB isn't in a loop, it doesn't get any better.  All dominators have a
6976122f3e6SDimitry Andric     // higher frequency by definition.
6986122f3e6SDimitry Andric     if (!Loop) {
6996122f3e6SDimitry Andric       DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
7006122f3e6SDimitry Andric                    << MBB->getNumber() << " at depth 0\n");
7016122f3e6SDimitry Andric       return MBB;
7026122f3e6SDimitry Andric     }
7036122f3e6SDimitry Andric 
7046122f3e6SDimitry Andric     // We'll never be able to exit the DefLoop.
7056122f3e6SDimitry Andric     if (Loop == DefLoop) {
7066122f3e6SDimitry Andric       DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
7076122f3e6SDimitry Andric                    << MBB->getNumber() << " in the same loop\n");
7086122f3e6SDimitry Andric       return MBB;
7096122f3e6SDimitry Andric     }
7106122f3e6SDimitry Andric 
7116122f3e6SDimitry Andric     // Least busy dominator seen so far.
7126122f3e6SDimitry Andric     unsigned Depth = Loop->getLoopDepth();
7136122f3e6SDimitry Andric     if (Depth < BestDepth) {
7146122f3e6SDimitry Andric       BestMBB = MBB;
7156122f3e6SDimitry Andric       BestDepth = Depth;
7166122f3e6SDimitry Andric       DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
7176122f3e6SDimitry Andric                    << MBB->getNumber() << " at depth " << Depth << '\n');
7186122f3e6SDimitry Andric     }
7196122f3e6SDimitry Andric 
7206122f3e6SDimitry Andric     // Leave loop by going to the immediate dominator of the loop header.
7216122f3e6SDimitry Andric     // This is a bigger stride than simply walking up the dominator tree.
7226122f3e6SDimitry Andric     MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
7236122f3e6SDimitry Andric 
7246122f3e6SDimitry Andric     // Too far up the dominator tree?
7256122f3e6SDimitry Andric     if (!IDom || !MDT.dominates(DefDomNode, IDom))
7266122f3e6SDimitry Andric       return BestMBB;
7276122f3e6SDimitry Andric 
7286122f3e6SDimitry Andric     MBB = IDom->getBlock();
7296122f3e6SDimitry Andric   }
7306122f3e6SDimitry Andric }
7316122f3e6SDimitry Andric 
7326122f3e6SDimitry Andric void SplitEditor::hoistCopiesForSize() {
7336122f3e6SDimitry Andric   // Get the complement interval, always RegIdx 0.
734f785676fSDimitry Andric   LiveInterval *LI = &LIS.getInterval(Edit->get(0));
7356122f3e6SDimitry Andric   LiveInterval *Parent = &Edit->getParent();
7366122f3e6SDimitry Andric 
7376122f3e6SDimitry Andric   // Track the nearest common dominator for all back-copies for each ParentVNI,
7386122f3e6SDimitry Andric   // indexed by ParentVNI->id.
7396122f3e6SDimitry Andric   typedef std::pair<MachineBasicBlock*, SlotIndex> DomPair;
7406122f3e6SDimitry Andric   SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
7416122f3e6SDimitry Andric 
7426122f3e6SDimitry Andric   // Find the nearest common dominator for parent values with multiple
7436122f3e6SDimitry Andric   // back-copies.  If a single back-copy dominates, put it in DomPair.second.
7446122f3e6SDimitry Andric   for (LiveInterval::vni_iterator VI = LI->vni_begin(), VE = LI->vni_end();
7456122f3e6SDimitry Andric        VI != VE; ++VI) {
7466122f3e6SDimitry Andric     VNInfo *VNI = *VI;
7477ae0e2c9SDimitry Andric     if (VNI->isUnused())
7487ae0e2c9SDimitry Andric       continue;
7496122f3e6SDimitry Andric     VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
7506122f3e6SDimitry Andric     assert(ParentVNI && "Parent not live at complement def");
7516122f3e6SDimitry Andric 
7526122f3e6SDimitry Andric     // Don't hoist remats.  The complement is probably going to disappear
7536122f3e6SDimitry Andric     // completely anyway.
7546122f3e6SDimitry Andric     if (Edit->didRematerialize(ParentVNI))
7556122f3e6SDimitry Andric       continue;
7566122f3e6SDimitry Andric 
7576122f3e6SDimitry Andric     MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
7586122f3e6SDimitry Andric     DomPair &Dom = NearestDom[ParentVNI->id];
7596122f3e6SDimitry Andric 
7606122f3e6SDimitry Andric     // Keep directly defined parent values.  This is either a PHI or an
7616122f3e6SDimitry Andric     // instruction in the complement range.  All other copies of ParentVNI
7626122f3e6SDimitry Andric     // should be eliminated.
7636122f3e6SDimitry Andric     if (VNI->def == ParentVNI->def) {
7646122f3e6SDimitry Andric       DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
7656122f3e6SDimitry Andric       Dom = DomPair(ValMBB, VNI->def);
7666122f3e6SDimitry Andric       continue;
7676122f3e6SDimitry Andric     }
7686122f3e6SDimitry Andric     // Skip the singly mapped values.  There is nothing to gain from hoisting a
7696122f3e6SDimitry Andric     // single back-copy.
7706122f3e6SDimitry Andric     if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
7716122f3e6SDimitry Andric       DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
7726122f3e6SDimitry Andric       continue;
7736122f3e6SDimitry Andric     }
7746122f3e6SDimitry Andric 
7756122f3e6SDimitry Andric     if (!Dom.first) {
7766122f3e6SDimitry Andric       // First time we see ParentVNI.  VNI dominates itself.
7776122f3e6SDimitry Andric       Dom = DomPair(ValMBB, VNI->def);
7786122f3e6SDimitry Andric     } else if (Dom.first == ValMBB) {
7796122f3e6SDimitry Andric       // Two defs in the same block.  Pick the earlier def.
7806122f3e6SDimitry Andric       if (!Dom.second.isValid() || VNI->def < Dom.second)
7816122f3e6SDimitry Andric         Dom.second = VNI->def;
7826122f3e6SDimitry Andric     } else {
7836122f3e6SDimitry Andric       // Different basic blocks. Check if one dominates.
7846122f3e6SDimitry Andric       MachineBasicBlock *Near =
7856122f3e6SDimitry Andric         MDT.findNearestCommonDominator(Dom.first, ValMBB);
7866122f3e6SDimitry Andric       if (Near == ValMBB)
7876122f3e6SDimitry Andric         // Def ValMBB dominates.
7886122f3e6SDimitry Andric         Dom = DomPair(ValMBB, VNI->def);
7896122f3e6SDimitry Andric       else if (Near != Dom.first)
7906122f3e6SDimitry Andric         // None dominate. Hoist to common dominator, need new def.
7916122f3e6SDimitry Andric         Dom = DomPair(Near, SlotIndex());
7926122f3e6SDimitry Andric     }
7936122f3e6SDimitry Andric 
7946122f3e6SDimitry Andric     DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def
7956122f3e6SDimitry Andric                  << " for parent " << ParentVNI->id << '@' << ParentVNI->def
7966122f3e6SDimitry Andric                  << " hoist to BB#" << Dom.first->getNumber() << ' '
7976122f3e6SDimitry Andric                  << Dom.second << '\n');
7986122f3e6SDimitry Andric   }
7996122f3e6SDimitry Andric 
8006122f3e6SDimitry Andric   // Insert the hoisted copies.
8016122f3e6SDimitry Andric   for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
8026122f3e6SDimitry Andric     DomPair &Dom = NearestDom[i];
8036122f3e6SDimitry Andric     if (!Dom.first || Dom.second.isValid())
8046122f3e6SDimitry Andric       continue;
8056122f3e6SDimitry Andric     // This value needs a hoisted copy inserted at the end of Dom.first.
8066122f3e6SDimitry Andric     VNInfo *ParentVNI = Parent->getValNumInfo(i);
8076122f3e6SDimitry Andric     MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
8086122f3e6SDimitry Andric     // Get a less loopy dominator than Dom.first.
8096122f3e6SDimitry Andric     Dom.first = findShallowDominator(Dom.first, DefMBB);
8106122f3e6SDimitry Andric     SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot();
8116122f3e6SDimitry Andric     Dom.second =
8126122f3e6SDimitry Andric       defFromParent(0, ParentVNI, Last, *Dom.first,
813dff0c46cSDimitry Andric                     SA.getLastSplitPointIter(Dom.first))->def;
8146122f3e6SDimitry Andric   }
8156122f3e6SDimitry Andric 
8166122f3e6SDimitry Andric   // Remove redundant back-copies that are now known to be dominated by another
8176122f3e6SDimitry Andric   // def with the same value.
8186122f3e6SDimitry Andric   SmallVector<VNInfo*, 8> BackCopies;
8196122f3e6SDimitry Andric   for (LiveInterval::vni_iterator VI = LI->vni_begin(), VE = LI->vni_end();
8206122f3e6SDimitry Andric        VI != VE; ++VI) {
8216122f3e6SDimitry Andric     VNInfo *VNI = *VI;
8227ae0e2c9SDimitry Andric     if (VNI->isUnused())
8237ae0e2c9SDimitry Andric       continue;
8246122f3e6SDimitry Andric     VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
8256122f3e6SDimitry Andric     const DomPair &Dom = NearestDom[ParentVNI->id];
8266122f3e6SDimitry Andric     if (!Dom.first || Dom.second == VNI->def)
8276122f3e6SDimitry Andric       continue;
8286122f3e6SDimitry Andric     BackCopies.push_back(VNI);
8296122f3e6SDimitry Andric     forceRecompute(0, ParentVNI);
8306122f3e6SDimitry Andric   }
8316122f3e6SDimitry Andric   removeBackCopies(BackCopies);
8326122f3e6SDimitry Andric }
8336122f3e6SDimitry Andric 
8346122f3e6SDimitry Andric 
8353b0f4066SDimitry Andric /// transferValues - Transfer all possible values to the new live ranges.
8366122f3e6SDimitry Andric /// Values that were rematerialized are left alone, they need LRCalc.extend().
8373b0f4066SDimitry Andric bool SplitEditor::transferValues() {
8383b0f4066SDimitry Andric   bool Skipped = false;
8393b0f4066SDimitry Andric   RegAssignMap::const_iterator AssignI = RegAssign.begin();
8403b0f4066SDimitry Andric   for (LiveInterval::const_iterator ParentI = Edit->getParent().begin(),
8413b0f4066SDimitry Andric          ParentE = Edit->getParent().end(); ParentI != ParentE; ++ParentI) {
8423b0f4066SDimitry Andric     DEBUG(dbgs() << "  blit " << *ParentI << ':');
8433b0f4066SDimitry Andric     VNInfo *ParentVNI = ParentI->valno;
8443b0f4066SDimitry Andric     // RegAssign has holes where RegIdx 0 should be used.
8453b0f4066SDimitry Andric     SlotIndex Start = ParentI->start;
8463b0f4066SDimitry Andric     AssignI.advanceTo(Start);
8473b0f4066SDimitry Andric     do {
8483b0f4066SDimitry Andric       unsigned RegIdx;
8493b0f4066SDimitry Andric       SlotIndex End = ParentI->end;
8503b0f4066SDimitry Andric       if (!AssignI.valid()) {
8513b0f4066SDimitry Andric         RegIdx = 0;
8523b0f4066SDimitry Andric       } else if (AssignI.start() <= Start) {
8533b0f4066SDimitry Andric         RegIdx = AssignI.value();
8543b0f4066SDimitry Andric         if (AssignI.stop() < End) {
8553b0f4066SDimitry Andric           End = AssignI.stop();
8563b0f4066SDimitry Andric           ++AssignI;
8573b0f4066SDimitry Andric         }
8583b0f4066SDimitry Andric       } else {
8593b0f4066SDimitry Andric         RegIdx = 0;
8603b0f4066SDimitry Andric         End = std::min(End, AssignI.start());
861e580952dSDimitry Andric       }
862e580952dSDimitry Andric 
8633b0f4066SDimitry Andric       // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
8643b0f4066SDimitry Andric       DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx);
865f785676fSDimitry Andric       LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
8663b0f4066SDimitry Andric 
8673b0f4066SDimitry Andric       // Check for a simply defined value that can be blitted directly.
8686122f3e6SDimitry Andric       ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
8696122f3e6SDimitry Andric       if (VNInfo *VNI = VFP.getPointer()) {
8703b0f4066SDimitry Andric         DEBUG(dbgs() << ':' << VNI->id);
871f785676fSDimitry Andric         LR.addSegment(LiveInterval::Segment(Start, End, VNI));
8723b0f4066SDimitry Andric         Start = End;
8733b0f4066SDimitry Andric         continue;
8743b0f4066SDimitry Andric       }
8753b0f4066SDimitry Andric 
8766122f3e6SDimitry Andric       // Skip values with forced recomputation.
8776122f3e6SDimitry Andric       if (VFP.getInt()) {
8786122f3e6SDimitry Andric         DEBUG(dbgs() << "(recalc)");
8793b0f4066SDimitry Andric         Skipped = true;
8803b0f4066SDimitry Andric         Start = End;
8813b0f4066SDimitry Andric         continue;
8823b0f4066SDimitry Andric       }
8833b0f4066SDimitry Andric 
8846122f3e6SDimitry Andric       LiveRangeCalc &LRC = getLRCalc(RegIdx);
8853b0f4066SDimitry Andric 
8863b0f4066SDimitry Andric       // This value has multiple defs in RegIdx, but it wasn't rematerialized,
8873b0f4066SDimitry Andric       // so the live range is accurate. Add live-in blocks in [Start;End) to the
8883b0f4066SDimitry Andric       // LiveInBlocks.
8893b0f4066SDimitry Andric       MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
8903b0f4066SDimitry Andric       SlotIndex BlockStart, BlockEnd;
8913b0f4066SDimitry Andric       tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(MBB);
8923b0f4066SDimitry Andric 
8933b0f4066SDimitry Andric       // The first block may be live-in, or it may have its own def.
8943b0f4066SDimitry Andric       if (Start != BlockStart) {
895f785676fSDimitry Andric         VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
8963b0f4066SDimitry Andric         assert(VNI && "Missing def for complex mapped value");
8973b0f4066SDimitry Andric         DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber());
8983b0f4066SDimitry Andric         // MBB has its own def. Is it also live-out?
8996122f3e6SDimitry Andric         if (BlockEnd <= End)
9006122f3e6SDimitry Andric           LRC.setLiveOutValue(MBB, VNI);
9016122f3e6SDimitry Andric 
9023b0f4066SDimitry Andric         // Skip to the next block for live-in.
9033b0f4066SDimitry Andric         ++MBB;
9043b0f4066SDimitry Andric         BlockStart = BlockEnd;
9053b0f4066SDimitry Andric       }
9063b0f4066SDimitry Andric 
9073b0f4066SDimitry Andric       // Handle the live-in blocks covered by [Start;End).
9083b0f4066SDimitry Andric       assert(Start <= BlockStart && "Expected live-in block");
9093b0f4066SDimitry Andric       while (BlockStart < End) {
9103b0f4066SDimitry Andric         DEBUG(dbgs() << ">BB#" << MBB->getNumber());
9113b0f4066SDimitry Andric         BlockEnd = LIS.getMBBEndIdx(MBB);
9123b0f4066SDimitry Andric         if (BlockStart == ParentVNI->def) {
9133b0f4066SDimitry Andric           // This block has the def of a parent PHI, so it isn't live-in.
9143b0f4066SDimitry Andric           assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
915f785676fSDimitry Andric           VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
9163b0f4066SDimitry Andric           assert(VNI && "Missing def for complex mapped parent PHI");
9176122f3e6SDimitry Andric           if (End >= BlockEnd)
9186122f3e6SDimitry Andric             LRC.setLiveOutValue(MBB, VNI); // Live-out as well.
9193b0f4066SDimitry Andric         } else {
9206122f3e6SDimitry Andric           // This block needs a live-in value.  The last block covered may not
9216122f3e6SDimitry Andric           // be live-out.
9223b0f4066SDimitry Andric           if (End < BlockEnd)
923f785676fSDimitry Andric             LRC.addLiveInBlock(LR, MDT[MBB], End);
9243b0f4066SDimitry Andric           else {
9256122f3e6SDimitry Andric             // Live-through, and we don't know the value.
926f785676fSDimitry Andric             LRC.addLiveInBlock(LR, MDT[MBB]);
9276122f3e6SDimitry Andric             LRC.setLiveOutValue(MBB, 0);
9283b0f4066SDimitry Andric           }
9293b0f4066SDimitry Andric         }
9303b0f4066SDimitry Andric         BlockStart = BlockEnd;
9313b0f4066SDimitry Andric         ++MBB;
9323b0f4066SDimitry Andric       }
9333b0f4066SDimitry Andric       Start = End;
9343b0f4066SDimitry Andric     } while (Start != ParentI->end);
9353b0f4066SDimitry Andric     DEBUG(dbgs() << '\n');
9363b0f4066SDimitry Andric   }
9373b0f4066SDimitry Andric 
9387ae0e2c9SDimitry Andric   LRCalc[0].calculateValues();
9396122f3e6SDimitry Andric   if (SpillMode)
9407ae0e2c9SDimitry Andric     LRCalc[1].calculateValues();
9413b0f4066SDimitry Andric 
9423b0f4066SDimitry Andric   return Skipped;
9433b0f4066SDimitry Andric }
9443b0f4066SDimitry Andric 
9453b0f4066SDimitry Andric void SplitEditor::extendPHIKillRanges() {
9463b0f4066SDimitry Andric     // Extend live ranges to be live-out for successor PHI values.
9473b0f4066SDimitry Andric   for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
9483b0f4066SDimitry Andric        E = Edit->getParent().vni_end(); I != E; ++I) {
9493b0f4066SDimitry Andric     const VNInfo *PHIVNI = *I;
9503b0f4066SDimitry Andric     if (PHIVNI->isUnused() || !PHIVNI->isPHIDef())
9513b0f4066SDimitry Andric       continue;
9523b0f4066SDimitry Andric     unsigned RegIdx = RegAssign.lookup(PHIVNI->def);
953f785676fSDimitry Andric     LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
9546122f3e6SDimitry Andric     LiveRangeCalc &LRC = getLRCalc(RegIdx);
9553b0f4066SDimitry Andric     MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
9563b0f4066SDimitry Andric     for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
9573b0f4066SDimitry Andric          PE = MBB->pred_end(); PI != PE; ++PI) {
9586122f3e6SDimitry Andric       SlotIndex End = LIS.getMBBEndIdx(*PI);
9596122f3e6SDimitry Andric       SlotIndex LastUse = End.getPrevSlot();
9603b0f4066SDimitry Andric       // The predecessor may not have a live-out value. That is OK, like an
9613b0f4066SDimitry Andric       // undef PHI operand.
9626122f3e6SDimitry Andric       if (Edit->getParent().liveAt(LastUse)) {
9636122f3e6SDimitry Andric         assert(RegAssign.lookup(LastUse) == RegIdx &&
9643b0f4066SDimitry Andric                "Different register assignment in phi predecessor");
965f785676fSDimitry Andric         LRC.extend(LR, End);
9663b0f4066SDimitry Andric       }
9673b0f4066SDimitry Andric     }
9683b0f4066SDimitry Andric   }
9693b0f4066SDimitry Andric }
9703b0f4066SDimitry Andric 
9713b0f4066SDimitry Andric /// rewriteAssigned - Rewrite all uses of Edit->getReg().
9723b0f4066SDimitry Andric void SplitEditor::rewriteAssigned(bool ExtendRanges) {
9733b0f4066SDimitry Andric   for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
9742754fe60SDimitry Andric        RE = MRI.reg_end(); RI != RE;) {
975e580952dSDimitry Andric     MachineOperand &MO = RI.getOperand();
976e580952dSDimitry Andric     MachineInstr *MI = MO.getParent();
977e580952dSDimitry Andric     ++RI;
9782754fe60SDimitry Andric     // LiveDebugVariables should have handled all DBG_VALUE instructions.
979e580952dSDimitry Andric     if (MI->isDebugValue()) {
980e580952dSDimitry Andric       DEBUG(dbgs() << "Zapping " << *MI);
981e580952dSDimitry Andric       MO.setReg(0);
982e580952dSDimitry Andric       continue;
983e580952dSDimitry Andric     }
9842754fe60SDimitry Andric 
9856122f3e6SDimitry Andric     // <undef> operands don't really read the register, so it doesn't matter
9866122f3e6SDimitry Andric     // which register we choose.  When the use operand is tied to a def, we must
9876122f3e6SDimitry Andric     // use the same register as the def, so just do that always.
9882754fe60SDimitry Andric     SlotIndex Idx = LIS.getInstructionIndex(MI);
9896122f3e6SDimitry Andric     if (MO.isDef() || MO.isUndef())
990dff0c46cSDimitry Andric       Idx = Idx.getRegSlot(MO.isEarlyClobber());
9912754fe60SDimitry Andric 
9922754fe60SDimitry Andric     // Rewrite to the mapped register at Idx.
9932754fe60SDimitry Andric     unsigned RegIdx = RegAssign.lookup(Idx);
994f785676fSDimitry Andric     LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
9956122f3e6SDimitry Andric     MO.setReg(LI->reg);
9962754fe60SDimitry Andric     DEBUG(dbgs() << "  rewr BB#" << MI->getParent()->getNumber() << '\t'
9972754fe60SDimitry Andric                  << Idx << ':' << RegIdx << '\t' << *MI);
9982754fe60SDimitry Andric 
9993b0f4066SDimitry Andric     // Extend liveness to Idx if the instruction reads reg.
10006122f3e6SDimitry Andric     if (!ExtendRanges || MO.isUndef())
10012754fe60SDimitry Andric       continue;
10023b0f4066SDimitry Andric 
10033b0f4066SDimitry Andric     // Skip instructions that don't read Reg.
10043b0f4066SDimitry Andric     if (MO.isDef()) {
10053b0f4066SDimitry Andric       if (!MO.getSubReg() && !MO.isEarlyClobber())
10063b0f4066SDimitry Andric         continue;
10073b0f4066SDimitry Andric       // We may wan't to extend a live range for a partial redef, or for a use
10083b0f4066SDimitry Andric       // tied to an early clobber.
10093b0f4066SDimitry Andric       Idx = Idx.getPrevSlot();
10103b0f4066SDimitry Andric       if (!Edit->getParent().liveAt(Idx))
10113b0f4066SDimitry Andric         continue;
10123b0f4066SDimitry Andric     } else
1013dff0c46cSDimitry Andric       Idx = Idx.getRegSlot(true);
10143b0f4066SDimitry Andric 
1015f785676fSDimitry Andric     getLRCalc(RegIdx).extend(*LI, Idx.getNextSlot());
1016e580952dSDimitry Andric   }
1017e580952dSDimitry Andric }
1018e580952dSDimitry Andric 
10193b0f4066SDimitry Andric void SplitEditor::deleteRematVictims() {
10203b0f4066SDimitry Andric   SmallVector<MachineInstr*, 8> Dead;
10213b0f4066SDimitry Andric   for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
1022f785676fSDimitry Andric     LiveInterval *LI = &LIS.getInterval(*I);
10233b0f4066SDimitry Andric     for (LiveInterval::const_iterator LII = LI->begin(), LIE = LI->end();
10243b0f4066SDimitry Andric            LII != LIE; ++LII) {
1025dff0c46cSDimitry Andric       // Dead defs end at the dead slot.
1026dff0c46cSDimitry Andric       if (LII->end != LII->valno->def.getDeadSlot())
10273b0f4066SDimitry Andric         continue;
10283b0f4066SDimitry Andric       MachineInstr *MI = LIS.getInstructionFromIndex(LII->valno->def);
10293b0f4066SDimitry Andric       assert(MI && "Missing instruction for dead def");
10303b0f4066SDimitry Andric       MI->addRegisterDead(LI->reg, &TRI);
10313b0f4066SDimitry Andric 
10323b0f4066SDimitry Andric       if (!MI->allDefsAreDead())
10333b0f4066SDimitry Andric         continue;
10343b0f4066SDimitry Andric 
10353b0f4066SDimitry Andric       DEBUG(dbgs() << "All defs dead: " << *MI);
10363b0f4066SDimitry Andric       Dead.push_back(MI);
10373b0f4066SDimitry Andric     }
10383b0f4066SDimitry Andric   }
10393b0f4066SDimitry Andric 
10403b0f4066SDimitry Andric   if (Dead.empty())
10413b0f4066SDimitry Andric     return;
10423b0f4066SDimitry Andric 
1043dff0c46cSDimitry Andric   Edit->eliminateDeadDefs(Dead);
10443b0f4066SDimitry Andric }
10453b0f4066SDimitry Andric 
10463b0f4066SDimitry Andric void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
10473b0f4066SDimitry Andric   ++NumFinished;
10482754fe60SDimitry Andric 
10492754fe60SDimitry Andric   // At this point, the live intervals in Edit contain VNInfos corresponding to
10502754fe60SDimitry Andric   // the inserted copies.
10512754fe60SDimitry Andric 
10522754fe60SDimitry Andric   // Add the original defs from the parent interval.
10533b0f4066SDimitry Andric   for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
10543b0f4066SDimitry Andric          E = Edit->getParent().vni_end(); I != E; ++I) {
10552754fe60SDimitry Andric     const VNInfo *ParentVNI = *I;
10562754fe60SDimitry Andric     if (ParentVNI->isUnused())
10572754fe60SDimitry Andric       continue;
10583b0f4066SDimitry Andric     unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
10597ae0e2c9SDimitry Andric     defValue(RegIdx, ParentVNI, ParentVNI->def);
10603b0f4066SDimitry Andric 
10616122f3e6SDimitry Andric     // Force rematted values to be recomputed everywhere.
10623b0f4066SDimitry Andric     // The new live ranges may be truncated.
10633b0f4066SDimitry Andric     if (Edit->didRematerialize(ParentVNI))
10643b0f4066SDimitry Andric       for (unsigned i = 0, e = Edit->size(); i != e; ++i)
10656122f3e6SDimitry Andric         forceRecompute(i, ParentVNI);
10666122f3e6SDimitry Andric   }
10676122f3e6SDimitry Andric 
10686122f3e6SDimitry Andric   // Hoist back-copies to the complement interval when in spill mode.
10696122f3e6SDimitry Andric   switch (SpillMode) {
10706122f3e6SDimitry Andric   case SM_Partition:
10716122f3e6SDimitry Andric     // Leave all back-copies as is.
10726122f3e6SDimitry Andric     break;
10736122f3e6SDimitry Andric   case SM_Size:
10746122f3e6SDimitry Andric     hoistCopiesForSize();
10756122f3e6SDimitry Andric     break;
10766122f3e6SDimitry Andric   case SM_Speed:
10776122f3e6SDimitry Andric     llvm_unreachable("Spill mode 'speed' not implemented yet");
10782754fe60SDimitry Andric   }
10792754fe60SDimitry Andric 
10803b0f4066SDimitry Andric   // Transfer the simply mapped values, check if any are skipped.
10813b0f4066SDimitry Andric   bool Skipped = transferValues();
10823b0f4066SDimitry Andric   if (Skipped)
10833b0f4066SDimitry Andric     extendPHIKillRanges();
10842754fe60SDimitry Andric   else
10853b0f4066SDimitry Andric     ++NumSimple;
10862754fe60SDimitry Andric 
10873b0f4066SDimitry Andric   // Rewrite virtual registers, possibly extending ranges.
10883b0f4066SDimitry Andric   rewriteAssigned(Skipped);
10892754fe60SDimitry Andric 
10903b0f4066SDimitry Andric   // Delete defs that were rematted everywhere.
10913b0f4066SDimitry Andric   if (Skipped)
10923b0f4066SDimitry Andric     deleteRematVictims();
10932754fe60SDimitry Andric 
10942754fe60SDimitry Andric   // Get rid of unused values and set phi-kill flags.
1095f785676fSDimitry Andric   for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I) {
1096f785676fSDimitry Andric     LiveInterval &LI = LIS.getInterval(*I);
1097f785676fSDimitry Andric     LI.RenumberValues();
1098f785676fSDimitry Andric   }
10992754fe60SDimitry Andric 
11003b0f4066SDimitry Andric   // Provide a reverse mapping from original indices to Edit ranges.
11013b0f4066SDimitry Andric   if (LRMap) {
11023b0f4066SDimitry Andric     LRMap->clear();
11033b0f4066SDimitry Andric     for (unsigned i = 0, e = Edit->size(); i != e; ++i)
11043b0f4066SDimitry Andric       LRMap->push_back(i);
11053b0f4066SDimitry Andric   }
11063b0f4066SDimitry Andric 
11072754fe60SDimitry Andric   // Now check if any registers were separated into multiple components.
11082754fe60SDimitry Andric   ConnectedVNInfoEqClasses ConEQ(LIS);
11093b0f4066SDimitry Andric   for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
11102754fe60SDimitry Andric     // Don't use iterators, they are invalidated by create() below.
1111f785676fSDimitry Andric     LiveInterval *li = &LIS.getInterval(Edit->get(i));
11122754fe60SDimitry Andric     unsigned NumComp = ConEQ.Classify(li);
11132754fe60SDimitry Andric     if (NumComp <= 1)
11142754fe60SDimitry Andric       continue;
11152754fe60SDimitry Andric     DEBUG(dbgs() << "  " << NumComp << " components: " << *li << '\n');
11162754fe60SDimitry Andric     SmallVector<LiveInterval*, 8> dups;
11172754fe60SDimitry Andric     dups.push_back(li);
11183b0f4066SDimitry Andric     for (unsigned j = 1; j != NumComp; ++j)
1119f785676fSDimitry Andric       dups.push_back(&Edit->createEmptyInterval());
11203b0f4066SDimitry Andric     ConEQ.Distribute(&dups[0], MRI);
11213b0f4066SDimitry Andric     // The new intervals all map back to i.
11223b0f4066SDimitry Andric     if (LRMap)
11233b0f4066SDimitry Andric       LRMap->resize(Edit->size(), i);
11242754fe60SDimitry Andric   }
11252754fe60SDimitry Andric 
1126e580952dSDimitry Andric   // Calculate spill weight and allocation hints for new intervals.
1127f785676fSDimitry Andric   Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI);
11283b0f4066SDimitry Andric 
11293b0f4066SDimitry Andric   assert(!LRMap || LRMap->size() == Edit->size());
1130e580952dSDimitry Andric }
1131e580952dSDimitry Andric 
1132e580952dSDimitry Andric 
1133e580952dSDimitry Andric //===----------------------------------------------------------------------===//
1134e580952dSDimitry Andric //                            Single Block Splitting
1135e580952dSDimitry Andric //===----------------------------------------------------------------------===//
1136e580952dSDimitry Andric 
11376122f3e6SDimitry Andric bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
11386122f3e6SDimitry Andric                                            bool SingleInstrs) const {
11396122f3e6SDimitry Andric   // Always split for multiple instructions.
11406122f3e6SDimitry Andric   if (!BI.isOneInstr())
11416122f3e6SDimitry Andric     return true;
11426122f3e6SDimitry Andric   // Don't split for single instructions unless explicitly requested.
11436122f3e6SDimitry Andric   if (!SingleInstrs)
11442754fe60SDimitry Andric     return false;
11456122f3e6SDimitry Andric   // Splitting a live-through range always makes progress.
11466122f3e6SDimitry Andric   if (BI.LiveIn && BI.LiveOut)
11476122f3e6SDimitry Andric     return true;
11486122f3e6SDimitry Andric   // No point in isolating a copy. It has no register class constraints.
11496122f3e6SDimitry Andric   if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
11506122f3e6SDimitry Andric     return false;
11516122f3e6SDimitry Andric   // Finally, don't isolate an end point that was created by earlier splits.
11526122f3e6SDimitry Andric   return isOriginalEndpoint(BI.FirstInstr);
1153e580952dSDimitry Andric }
1154e580952dSDimitry Andric 
11553b0f4066SDimitry Andric void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
11563b0f4066SDimitry Andric   openIntv();
11573b0f4066SDimitry Andric   SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber());
11586122f3e6SDimitry Andric   SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
11593b0f4066SDimitry Andric     LastSplitPoint));
11606122f3e6SDimitry Andric   if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
11616122f3e6SDimitry Andric     useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
11623b0f4066SDimitry Andric   } else {
11633b0f4066SDimitry Andric       // The last use is after the last valid split point.
11643b0f4066SDimitry Andric     SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
11653b0f4066SDimitry Andric     useIntv(SegStart, SegStop);
11666122f3e6SDimitry Andric     overlapIntv(SegStop, BI.LastInstr);
11673b0f4066SDimitry Andric   }
11683b0f4066SDimitry Andric }
11693b0f4066SDimitry Andric 
117017a519f9SDimitry Andric 
117117a519f9SDimitry Andric //===----------------------------------------------------------------------===//
117217a519f9SDimitry Andric //                    Global Live Range Splitting Support
117317a519f9SDimitry Andric //===----------------------------------------------------------------------===//
117417a519f9SDimitry Andric 
117517a519f9SDimitry Andric // These methods support a method of global live range splitting that uses a
117617a519f9SDimitry Andric // global algorithm to decide intervals for CFG edges. They will insert split
117717a519f9SDimitry Andric // points and color intervals in basic blocks while avoiding interference.
117817a519f9SDimitry Andric //
117917a519f9SDimitry Andric // Note that splitSingleBlock is also useful for blocks where both CFG edges
118017a519f9SDimitry Andric // are on the stack.
118117a519f9SDimitry Andric 
118217a519f9SDimitry Andric void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
118317a519f9SDimitry Andric                                         unsigned IntvIn, SlotIndex LeaveBefore,
118417a519f9SDimitry Andric                                         unsigned IntvOut, SlotIndex EnterAfter){
118517a519f9SDimitry Andric   SlotIndex Start, Stop;
118617a519f9SDimitry Andric   tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
118717a519f9SDimitry Andric 
118817a519f9SDimitry Andric   DEBUG(dbgs() << "BB#" << MBBNum << " [" << Start << ';' << Stop
118917a519f9SDimitry Andric                << ") intf " << LeaveBefore << '-' << EnterAfter
119017a519f9SDimitry Andric                << ", live-through " << IntvIn << " -> " << IntvOut);
119117a519f9SDimitry Andric 
119217a519f9SDimitry Andric   assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
119317a519f9SDimitry Andric 
11946122f3e6SDimitry Andric   assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
11956122f3e6SDimitry Andric   assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
11966122f3e6SDimitry Andric   assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
11976122f3e6SDimitry Andric 
11986122f3e6SDimitry Andric   MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
11996122f3e6SDimitry Andric 
120017a519f9SDimitry Andric   if (!IntvOut) {
120117a519f9SDimitry Andric     DEBUG(dbgs() << ", spill on entry.\n");
120217a519f9SDimitry Andric     //
120317a519f9SDimitry Andric     //        <<<<<<<<<    Possible LeaveBefore interference.
120417a519f9SDimitry Andric     //    |-----------|    Live through.
120517a519f9SDimitry Andric     //    -____________    Spill on entry.
120617a519f9SDimitry Andric     //
120717a519f9SDimitry Andric     selectIntv(IntvIn);
120817a519f9SDimitry Andric     SlotIndex Idx = leaveIntvAtTop(*MBB);
120917a519f9SDimitry Andric     assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
121017a519f9SDimitry Andric     (void)Idx;
121117a519f9SDimitry Andric     return;
121217a519f9SDimitry Andric   }
121317a519f9SDimitry Andric 
121417a519f9SDimitry Andric   if (!IntvIn) {
121517a519f9SDimitry Andric     DEBUG(dbgs() << ", reload on exit.\n");
121617a519f9SDimitry Andric     //
121717a519f9SDimitry Andric     //    >>>>>>>          Possible EnterAfter interference.
121817a519f9SDimitry Andric     //    |-----------|    Live through.
121917a519f9SDimitry Andric     //    ___________--    Reload on exit.
122017a519f9SDimitry Andric     //
122117a519f9SDimitry Andric     selectIntv(IntvOut);
122217a519f9SDimitry Andric     SlotIndex Idx = enterIntvAtEnd(*MBB);
122317a519f9SDimitry Andric     assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
122417a519f9SDimitry Andric     (void)Idx;
122517a519f9SDimitry Andric     return;
122617a519f9SDimitry Andric   }
122717a519f9SDimitry Andric 
122817a519f9SDimitry Andric   if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
122917a519f9SDimitry Andric     DEBUG(dbgs() << ", straight through.\n");
123017a519f9SDimitry Andric     //
123117a519f9SDimitry Andric     //    |-----------|    Live through.
123217a519f9SDimitry Andric     //    -------------    Straight through, same intv, no interference.
123317a519f9SDimitry Andric     //
123417a519f9SDimitry Andric     selectIntv(IntvOut);
123517a519f9SDimitry Andric     useIntv(Start, Stop);
123617a519f9SDimitry Andric     return;
123717a519f9SDimitry Andric   }
123817a519f9SDimitry Andric 
123917a519f9SDimitry Andric   // We cannot legally insert splits after LSP.
124017a519f9SDimitry Andric   SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
12416122f3e6SDimitry Andric   assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
124217a519f9SDimitry Andric 
124317a519f9SDimitry Andric   if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
124417a519f9SDimitry Andric                   LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
124517a519f9SDimitry Andric     DEBUG(dbgs() << ", switch avoiding interference.\n");
124617a519f9SDimitry Andric     //
124717a519f9SDimitry Andric     //    >>>>     <<<<    Non-overlapping EnterAfter/LeaveBefore interference.
124817a519f9SDimitry Andric     //    |-----------|    Live through.
124917a519f9SDimitry Andric     //    ------=======    Switch intervals between interference.
125017a519f9SDimitry Andric     //
125117a519f9SDimitry Andric     selectIntv(IntvOut);
12526122f3e6SDimitry Andric     SlotIndex Idx;
12536122f3e6SDimitry Andric     if (LeaveBefore && LeaveBefore < LSP) {
12546122f3e6SDimitry Andric       Idx = enterIntvBefore(LeaveBefore);
125517a519f9SDimitry Andric       useIntv(Idx, Stop);
12566122f3e6SDimitry Andric     } else {
12576122f3e6SDimitry Andric       Idx = enterIntvAtEnd(*MBB);
12586122f3e6SDimitry Andric     }
125917a519f9SDimitry Andric     selectIntv(IntvIn);
126017a519f9SDimitry Andric     useIntv(Start, Idx);
126117a519f9SDimitry Andric     assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
126217a519f9SDimitry Andric     assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
126317a519f9SDimitry Andric     return;
126417a519f9SDimitry Andric   }
126517a519f9SDimitry Andric 
126617a519f9SDimitry Andric   DEBUG(dbgs() << ", create local intv for interference.\n");
126717a519f9SDimitry Andric   //
126817a519f9SDimitry Andric   //    >>><><><><<<<    Overlapping EnterAfter/LeaveBefore interference.
126917a519f9SDimitry Andric   //    |-----------|    Live through.
127017a519f9SDimitry Andric   //    ==---------==    Switch intervals before/after interference.
127117a519f9SDimitry Andric   //
127217a519f9SDimitry Andric   assert(LeaveBefore <= EnterAfter && "Missed case");
127317a519f9SDimitry Andric 
127417a519f9SDimitry Andric   selectIntv(IntvOut);
127517a519f9SDimitry Andric   SlotIndex Idx = enterIntvAfter(EnterAfter);
127617a519f9SDimitry Andric   useIntv(Idx, Stop);
127717a519f9SDimitry Andric   assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
127817a519f9SDimitry Andric 
127917a519f9SDimitry Andric   selectIntv(IntvIn);
128017a519f9SDimitry Andric   Idx = leaveIntvBefore(LeaveBefore);
128117a519f9SDimitry Andric   useIntv(Start, Idx);
128217a519f9SDimitry Andric   assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
128317a519f9SDimitry Andric }
128417a519f9SDimitry Andric 
128517a519f9SDimitry Andric 
128617a519f9SDimitry Andric void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
128717a519f9SDimitry Andric                                   unsigned IntvIn, SlotIndex LeaveBefore) {
128817a519f9SDimitry Andric   SlotIndex Start, Stop;
128917a519f9SDimitry Andric   tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
129017a519f9SDimitry Andric 
129117a519f9SDimitry Andric   DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
12926122f3e6SDimitry Andric                << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
129317a519f9SDimitry Andric                << ", reg-in " << IntvIn << ", leave before " << LeaveBefore
129417a519f9SDimitry Andric                << (BI.LiveOut ? ", stack-out" : ", killed in block"));
129517a519f9SDimitry Andric 
129617a519f9SDimitry Andric   assert(IntvIn && "Must have register in");
129717a519f9SDimitry Andric   assert(BI.LiveIn && "Must be live-in");
129817a519f9SDimitry Andric   assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
129917a519f9SDimitry Andric 
13006122f3e6SDimitry Andric   if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
130117a519f9SDimitry Andric     DEBUG(dbgs() << " before interference.\n");
130217a519f9SDimitry Andric     //
130317a519f9SDimitry Andric     //               <<<    Interference after kill.
130417a519f9SDimitry Andric     //     |---o---x   |    Killed in block.
130517a519f9SDimitry Andric     //     =========        Use IntvIn everywhere.
130617a519f9SDimitry Andric     //
130717a519f9SDimitry Andric     selectIntv(IntvIn);
13086122f3e6SDimitry Andric     useIntv(Start, BI.LastInstr);
130917a519f9SDimitry Andric     return;
131017a519f9SDimitry Andric   }
131117a519f9SDimitry Andric 
131217a519f9SDimitry Andric   SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
131317a519f9SDimitry Andric 
13146122f3e6SDimitry Andric   if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
131517a519f9SDimitry Andric     //
131617a519f9SDimitry Andric     //               <<<    Possible interference after last use.
131717a519f9SDimitry Andric     //     |---o---o---|    Live-out on stack.
131817a519f9SDimitry Andric     //     =========____    Leave IntvIn after last use.
131917a519f9SDimitry Andric     //
132017a519f9SDimitry Andric     //                 <    Interference after last use.
132117a519f9SDimitry Andric     //     |---o---o--o|    Live-out on stack, late last use.
132217a519f9SDimitry Andric     //     ============     Copy to stack after LSP, overlap IntvIn.
132317a519f9SDimitry Andric     //            \_____    Stack interval is live-out.
132417a519f9SDimitry Andric     //
13256122f3e6SDimitry Andric     if (BI.LastInstr < LSP) {
132617a519f9SDimitry Andric       DEBUG(dbgs() << ", spill after last use before interference.\n");
132717a519f9SDimitry Andric       selectIntv(IntvIn);
13286122f3e6SDimitry Andric       SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
132917a519f9SDimitry Andric       useIntv(Start, Idx);
133017a519f9SDimitry Andric       assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
133117a519f9SDimitry Andric     } else {
133217a519f9SDimitry Andric       DEBUG(dbgs() << ", spill before last split point.\n");
133317a519f9SDimitry Andric       selectIntv(IntvIn);
133417a519f9SDimitry Andric       SlotIndex Idx = leaveIntvBefore(LSP);
13356122f3e6SDimitry Andric       overlapIntv(Idx, BI.LastInstr);
133617a519f9SDimitry Andric       useIntv(Start, Idx);
133717a519f9SDimitry Andric       assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
133817a519f9SDimitry Andric     }
133917a519f9SDimitry Andric     return;
134017a519f9SDimitry Andric   }
134117a519f9SDimitry Andric 
134217a519f9SDimitry Andric   // The interference is overlapping somewhere we wanted to use IntvIn. That
134317a519f9SDimitry Andric   // means we need to create a local interval that can be allocated a
134417a519f9SDimitry Andric   // different register.
134517a519f9SDimitry Andric   unsigned LocalIntv = openIntv();
134617a519f9SDimitry Andric   (void)LocalIntv;
134717a519f9SDimitry Andric   DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
134817a519f9SDimitry Andric 
13496122f3e6SDimitry Andric   if (!BI.LiveOut || BI.LastInstr < LSP) {
135017a519f9SDimitry Andric     //
135117a519f9SDimitry Andric     //           <<<<<<<    Interference overlapping uses.
135217a519f9SDimitry Andric     //     |---o---o---|    Live-out on stack.
135317a519f9SDimitry Andric     //     =====----____    Leave IntvIn before interference, then spill.
135417a519f9SDimitry Andric     //
13556122f3e6SDimitry Andric     SlotIndex To = leaveIntvAfter(BI.LastInstr);
135617a519f9SDimitry Andric     SlotIndex From = enterIntvBefore(LeaveBefore);
135717a519f9SDimitry Andric     useIntv(From, To);
135817a519f9SDimitry Andric     selectIntv(IntvIn);
135917a519f9SDimitry Andric     useIntv(Start, From);
136017a519f9SDimitry Andric     assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
136117a519f9SDimitry Andric     return;
136217a519f9SDimitry Andric   }
136317a519f9SDimitry Andric 
136417a519f9SDimitry Andric   //           <<<<<<<    Interference overlapping uses.
136517a519f9SDimitry Andric   //     |---o---o--o|    Live-out on stack, late last use.
136617a519f9SDimitry Andric   //     =====-------     Copy to stack before LSP, overlap LocalIntv.
136717a519f9SDimitry Andric   //            \_____    Stack interval is live-out.
136817a519f9SDimitry Andric   //
136917a519f9SDimitry Andric   SlotIndex To = leaveIntvBefore(LSP);
13706122f3e6SDimitry Andric   overlapIntv(To, BI.LastInstr);
137117a519f9SDimitry Andric   SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
137217a519f9SDimitry Andric   useIntv(From, To);
137317a519f9SDimitry Andric   selectIntv(IntvIn);
137417a519f9SDimitry Andric   useIntv(Start, From);
137517a519f9SDimitry Andric   assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
137617a519f9SDimitry Andric }
137717a519f9SDimitry Andric 
137817a519f9SDimitry Andric void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
137917a519f9SDimitry Andric                                    unsigned IntvOut, SlotIndex EnterAfter) {
138017a519f9SDimitry Andric   SlotIndex Start, Stop;
138117a519f9SDimitry Andric   tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
138217a519f9SDimitry Andric 
138317a519f9SDimitry Andric   DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
13846122f3e6SDimitry Andric                << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
138517a519f9SDimitry Andric                << ", reg-out " << IntvOut << ", enter after " << EnterAfter
138617a519f9SDimitry Andric                << (BI.LiveIn ? ", stack-in" : ", defined in block"));
138717a519f9SDimitry Andric 
138817a519f9SDimitry Andric   SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
138917a519f9SDimitry Andric 
139017a519f9SDimitry Andric   assert(IntvOut && "Must have register out");
139117a519f9SDimitry Andric   assert(BI.LiveOut && "Must be live-out");
139217a519f9SDimitry Andric   assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
139317a519f9SDimitry Andric 
13946122f3e6SDimitry Andric   if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
139517a519f9SDimitry Andric     DEBUG(dbgs() << " after interference.\n");
139617a519f9SDimitry Andric     //
139717a519f9SDimitry Andric     //    >>>>             Interference before def.
139817a519f9SDimitry Andric     //    |   o---o---|    Defined in block.
139917a519f9SDimitry Andric     //        =========    Use IntvOut everywhere.
140017a519f9SDimitry Andric     //
140117a519f9SDimitry Andric     selectIntv(IntvOut);
14026122f3e6SDimitry Andric     useIntv(BI.FirstInstr, Stop);
140317a519f9SDimitry Andric     return;
140417a519f9SDimitry Andric   }
140517a519f9SDimitry Andric 
14066122f3e6SDimitry Andric   if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
140717a519f9SDimitry Andric     DEBUG(dbgs() << ", reload after interference.\n");
140817a519f9SDimitry Andric     //
140917a519f9SDimitry Andric     //    >>>>             Interference before def.
141017a519f9SDimitry Andric     //    |---o---o---|    Live-through, stack-in.
141117a519f9SDimitry Andric     //    ____=========    Enter IntvOut before first use.
141217a519f9SDimitry Andric     //
141317a519f9SDimitry Andric     selectIntv(IntvOut);
14146122f3e6SDimitry Andric     SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
141517a519f9SDimitry Andric     useIntv(Idx, Stop);
141617a519f9SDimitry Andric     assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
141717a519f9SDimitry Andric     return;
141817a519f9SDimitry Andric   }
141917a519f9SDimitry Andric 
142017a519f9SDimitry Andric   // The interference is overlapping somewhere we wanted to use IntvOut. That
142117a519f9SDimitry Andric   // means we need to create a local interval that can be allocated a
142217a519f9SDimitry Andric   // different register.
142317a519f9SDimitry Andric   DEBUG(dbgs() << ", interference overlaps uses.\n");
142417a519f9SDimitry Andric   //
142517a519f9SDimitry Andric   //    >>>>>>>          Interference overlapping uses.
142617a519f9SDimitry Andric   //    |---o---o---|    Live-through, stack-in.
142717a519f9SDimitry Andric   //    ____---======    Create local interval for interference range.
142817a519f9SDimitry Andric   //
142917a519f9SDimitry Andric   selectIntv(IntvOut);
143017a519f9SDimitry Andric   SlotIndex Idx = enterIntvAfter(EnterAfter);
143117a519f9SDimitry Andric   useIntv(Idx, Stop);
143217a519f9SDimitry Andric   assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
143317a519f9SDimitry Andric 
143417a519f9SDimitry Andric   openIntv();
14356122f3e6SDimitry Andric   SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
143617a519f9SDimitry Andric   useIntv(From, Idx);
143717a519f9SDimitry Andric }
1438