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"
17e580952dSDimitry Andric #include "VirtRegMap.h"
183b0f4066SDimitry Andric #include "llvm/ADT/Statistic.h"
19e580952dSDimitry Andric #include "llvm/CodeGen/LiveIntervalAnalysis.h"
20dff0c46cSDimitry Andric #include "llvm/CodeGen/LiveRangeEdit.h"
212754fe60SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
22e580952dSDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
236122f3e6SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h"
24e580952dSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.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) {
2176122f3e6SDimitry Andric         assert(LVI->start == LVI->valno->def && "Dangling LiveRange 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 
2486122f3e6SDimitry Andric         // A LiveRange that starts in the middle of the block must be a def.
2496122f3e6SDimitry Andric         assert(LVI->start == LVI->valno->def && "Dangling LiveRange 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,
3283b0f4066SDimitry Andric                          MachineDominatorTree &mdt)
3293b0f4066SDimitry Andric   : SA(sa), LIS(lis), VRM(vrm),
3303b0f4066SDimitry Andric     MRI(vrm.getMachineFunction().getRegInfo()),
3313b0f4066SDimitry Andric     MDT(mdt),
3323b0f4066SDimitry Andric     TII(*vrm.getMachineFunction().getTarget().getInstrInfo()),
3333b0f4066SDimitry Andric     TRI(*vrm.getMachineFunction().getTarget().getRegisterInfo()),
3343b0f4066SDimitry Andric     Edit(0),
3353b0f4066SDimitry Andric     OpenIdx(0),
3366122f3e6SDimitry Andric     SpillMode(SM_Partition),
3373b0f4066SDimitry Andric     RegAssign(Allocator)
3383b0f4066SDimitry Andric {}
339e580952dSDimitry Andric 
3406122f3e6SDimitry Andric void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
3416122f3e6SDimitry Andric   Edit = &LRE;
3426122f3e6SDimitry Andric   SpillMode = SM;
3433b0f4066SDimitry Andric   OpenIdx = 0;
3443b0f4066SDimitry Andric   RegAssign.clear();
3452754fe60SDimitry Andric   Values.clear();
3463b0f4066SDimitry Andric 
3476122f3e6SDimitry Andric   // Reset the LiveRangeCalc instances needed for this spill mode.
3486122f3e6SDimitry Andric   LRCalc[0].reset(&VRM.getMachineFunction());
3496122f3e6SDimitry Andric   if (SpillMode)
3506122f3e6SDimitry Andric     LRCalc[1].reset(&VRM.getMachineFunction());
3513b0f4066SDimitry Andric 
3523b0f4066SDimitry Andric   // We don't need an AliasAnalysis since we will only be performing
3533b0f4066SDimitry Andric   // cheap-as-a-copy remats anyway.
354dff0c46cSDimitry Andric   Edit->anyRematerializable(0);
3552754fe60SDimitry Andric }
3562754fe60SDimitry Andric 
3573b0f4066SDimitry Andric void SplitEditor::dump() const {
3583b0f4066SDimitry Andric   if (RegAssign.empty()) {
3593b0f4066SDimitry Andric     dbgs() << " empty\n";
3603b0f4066SDimitry Andric     return;
3612754fe60SDimitry Andric   }
3622754fe60SDimitry Andric 
3633b0f4066SDimitry Andric   for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
3643b0f4066SDimitry Andric     dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
3653b0f4066SDimitry Andric   dbgs() << '\n';
3663b0f4066SDimitry Andric }
3673b0f4066SDimitry Andric 
3683b0f4066SDimitry Andric VNInfo *SplitEditor::defValue(unsigned RegIdx,
3693b0f4066SDimitry Andric                               const VNInfo *ParentVNI,
3703b0f4066SDimitry Andric                               SlotIndex Idx) {
371e580952dSDimitry Andric   assert(ParentVNI && "Mapping  NULL value");
372e580952dSDimitry Andric   assert(Idx.isValid() && "Invalid SlotIndex");
3733b0f4066SDimitry Andric   assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
3743b0f4066SDimitry Andric   LiveInterval *LI = Edit->get(RegIdx);
3752754fe60SDimitry Andric 
3762754fe60SDimitry Andric   // Create a new value.
377dff0c46cSDimitry Andric   VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
3782754fe60SDimitry Andric 
379e580952dSDimitry Andric   // Use insert for lookup, so we can add missing values with a second lookup.
380e580952dSDimitry Andric   std::pair<ValueMap::iterator, bool> InsP =
3816122f3e6SDimitry Andric     Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id),
3826122f3e6SDimitry Andric                                  ValueForcePair(VNI, false)));
3832754fe60SDimitry Andric 
3843b0f4066SDimitry Andric   // This was the first time (RegIdx, ParentVNI) was mapped.
3853b0f4066SDimitry Andric   // Keep it as a simple def without any liveness.
3863b0f4066SDimitry Andric   if (InsP.second)
3873b0f4066SDimitry Andric     return VNI;
3883b0f4066SDimitry Andric 
3893b0f4066SDimitry Andric   // If the previous value was a simple mapping, add liveness for it now.
3906122f3e6SDimitry Andric   if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
3913b0f4066SDimitry Andric     SlotIndex Def = OldVNI->def;
392dff0c46cSDimitry Andric     LI->addRange(LiveRange(Def, Def.getDeadSlot(), OldVNI));
3936122f3e6SDimitry Andric     // No longer a simple mapping.  Switch to a complex, non-forced mapping.
3946122f3e6SDimitry Andric     InsP.first->second = ValueForcePair();
3953b0f4066SDimitry Andric   }
3963b0f4066SDimitry Andric 
3973b0f4066SDimitry Andric   // This is a complex mapping, add liveness for VNI
3983b0f4066SDimitry Andric   SlotIndex Def = VNI->def;
399dff0c46cSDimitry Andric   LI->addRange(LiveRange(Def, Def.getDeadSlot(), VNI));
4002754fe60SDimitry Andric 
4012754fe60SDimitry Andric   return VNI;
4022754fe60SDimitry Andric }
4032754fe60SDimitry Andric 
4046122f3e6SDimitry Andric void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) {
4052754fe60SDimitry Andric   assert(ParentVNI && "Mapping  NULL value");
4066122f3e6SDimitry Andric   ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)];
4076122f3e6SDimitry Andric   VNInfo *VNI = VFP.getPointer();
4083b0f4066SDimitry Andric 
4096122f3e6SDimitry Andric   // ParentVNI was either unmapped or already complex mapped. Either way, just
4106122f3e6SDimitry Andric   // set the force bit.
4116122f3e6SDimitry Andric   if (!VNI) {
4126122f3e6SDimitry Andric     VFP.setInt(true);
4133b0f4066SDimitry Andric     return;
4146122f3e6SDimitry Andric   }
4153b0f4066SDimitry Andric 
4163b0f4066SDimitry Andric   // This was previously a single mapping. Make sure the old def is represented
4173b0f4066SDimitry Andric   // by a trivial live range.
4183b0f4066SDimitry Andric   SlotIndex Def = VNI->def;
419dff0c46cSDimitry Andric   Edit->get(RegIdx)->addRange(LiveRange(Def, Def.getDeadSlot(), VNI));
4206122f3e6SDimitry Andric   // Mark as complex mapped, forced.
4216122f3e6SDimitry Andric   VFP = ValueForcePair(0, true);
422e580952dSDimitry Andric }
423e580952dSDimitry Andric 
4242754fe60SDimitry Andric VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
4252754fe60SDimitry Andric                                    VNInfo *ParentVNI,
4262754fe60SDimitry Andric                                    SlotIndex UseIdx,
427e580952dSDimitry Andric                                    MachineBasicBlock &MBB,
428e580952dSDimitry Andric                                    MachineBasicBlock::iterator I) {
4292754fe60SDimitry Andric   MachineInstr *CopyMI = 0;
4302754fe60SDimitry Andric   SlotIndex Def;
4313b0f4066SDimitry Andric   LiveInterval *LI = Edit->get(RegIdx);
4323b0f4066SDimitry Andric 
4333b0f4066SDimitry Andric   // We may be trying to avoid interference that ends at a deleted instruction,
4343b0f4066SDimitry Andric   // so always begin RegIdx 0 early and all others late.
4353b0f4066SDimitry Andric   bool Late = RegIdx != 0;
4362754fe60SDimitry Andric 
4372754fe60SDimitry Andric   // Attempt cheap-as-a-copy rematerialization.
4382754fe60SDimitry Andric   LiveRangeEdit::Remat RM(ParentVNI);
439dff0c46cSDimitry Andric   if (Edit->canRematerializeAt(RM, UseIdx, true)) {
440dff0c46cSDimitry Andric     Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, TRI, Late);
441bd5abe19SDimitry Andric     ++NumRemats;
4422754fe60SDimitry Andric   } else {
4432754fe60SDimitry Andric     // Can't remat, just insert a copy from parent.
4442754fe60SDimitry Andric     CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
4453b0f4066SDimitry Andric                .addReg(Edit->getReg());
4463b0f4066SDimitry Andric     Def = LIS.getSlotIndexes()->insertMachineInstrInMaps(CopyMI, Late)
447dff0c46cSDimitry Andric             .getRegSlot();
448bd5abe19SDimitry Andric     ++NumCopies;
4492754fe60SDimitry Andric   }
4502754fe60SDimitry Andric 
4512754fe60SDimitry Andric   // Define the value in Reg.
452dff0c46cSDimitry Andric   return defValue(RegIdx, ParentVNI, Def);
453e580952dSDimitry Andric }
454e580952dSDimitry Andric 
455e580952dSDimitry Andric /// Create a new virtual register and live interval.
4563b0f4066SDimitry Andric unsigned SplitEditor::openIntv() {
4572754fe60SDimitry Andric   // Create the complement as index 0.
4583b0f4066SDimitry Andric   if (Edit->empty())
459dff0c46cSDimitry Andric     Edit->create();
460e580952dSDimitry Andric 
4612754fe60SDimitry Andric   // Create the open interval.
4623b0f4066SDimitry Andric   OpenIdx = Edit->size();
463dff0c46cSDimitry Andric   Edit->create();
4643b0f4066SDimitry Andric   return OpenIdx;
4653b0f4066SDimitry Andric }
4663b0f4066SDimitry Andric 
4673b0f4066SDimitry Andric void SplitEditor::selectIntv(unsigned Idx) {
4683b0f4066SDimitry Andric   assert(Idx != 0 && "Cannot select the complement interval");
4693b0f4066SDimitry Andric   assert(Idx < Edit->size() && "Can only select previously opened interval");
47017a519f9SDimitry Andric   DEBUG(dbgs() << "    selectIntv " << OpenIdx << " -> " << Idx << '\n');
4713b0f4066SDimitry Andric   OpenIdx = Idx;
4722754fe60SDimitry Andric }
473e580952dSDimitry Andric 
4742754fe60SDimitry Andric SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
4752754fe60SDimitry Andric   assert(OpenIdx && "openIntv not called before enterIntvBefore");
4762754fe60SDimitry Andric   DEBUG(dbgs() << "    enterIntvBefore " << Idx);
4772754fe60SDimitry Andric   Idx = Idx.getBaseIndex();
4783b0f4066SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
4792754fe60SDimitry Andric   if (!ParentVNI) {
4802754fe60SDimitry Andric     DEBUG(dbgs() << ": not live\n");
4812754fe60SDimitry Andric     return Idx;
4822754fe60SDimitry Andric   }
4832754fe60SDimitry Andric   DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
4842754fe60SDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
485e580952dSDimitry Andric   assert(MI && "enterIntvBefore called with invalid index");
486e580952dSDimitry Andric 
4872754fe60SDimitry Andric   VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
4882754fe60SDimitry Andric   return VNI->def;
489e580952dSDimitry Andric }
490e580952dSDimitry Andric 
49117a519f9SDimitry Andric SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
49217a519f9SDimitry Andric   assert(OpenIdx && "openIntv not called before enterIntvAfter");
49317a519f9SDimitry Andric   DEBUG(dbgs() << "    enterIntvAfter " << Idx);
49417a519f9SDimitry Andric   Idx = Idx.getBoundaryIndex();
49517a519f9SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
49617a519f9SDimitry Andric   if (!ParentVNI) {
49717a519f9SDimitry Andric     DEBUG(dbgs() << ": not live\n");
49817a519f9SDimitry Andric     return Idx;
49917a519f9SDimitry Andric   }
50017a519f9SDimitry Andric   DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
50117a519f9SDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
50217a519f9SDimitry Andric   assert(MI && "enterIntvAfter called with invalid index");
50317a519f9SDimitry Andric 
50417a519f9SDimitry Andric   VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
50517a519f9SDimitry Andric                               llvm::next(MachineBasicBlock::iterator(MI)));
50617a519f9SDimitry Andric   return VNI->def;
50717a519f9SDimitry Andric }
50817a519f9SDimitry Andric 
5092754fe60SDimitry Andric SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
5102754fe60SDimitry Andric   assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
5112754fe60SDimitry Andric   SlotIndex End = LIS.getMBBEndIdx(&MBB);
5122754fe60SDimitry Andric   SlotIndex Last = End.getPrevSlot();
5132754fe60SDimitry Andric   DEBUG(dbgs() << "    enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
5143b0f4066SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
5152754fe60SDimitry Andric   if (!ParentVNI) {
5162754fe60SDimitry Andric     DEBUG(dbgs() << ": not live\n");
5172754fe60SDimitry Andric     return End;
5182754fe60SDimitry Andric   }
5192754fe60SDimitry Andric   DEBUG(dbgs() << ": valno " << ParentVNI->id);
5202754fe60SDimitry Andric   VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
521dff0c46cSDimitry Andric                               SA.getLastSplitPointIter(&MBB));
5222754fe60SDimitry Andric   RegAssign.insert(VNI->def, End, OpenIdx);
5232754fe60SDimitry Andric   DEBUG(dump());
5242754fe60SDimitry Andric   return VNI->def;
525e580952dSDimitry Andric }
526e580952dSDimitry Andric 
5272754fe60SDimitry Andric /// useIntv - indicate that all instructions in MBB should use OpenLI.
528e580952dSDimitry Andric void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
5292754fe60SDimitry Andric   useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
530e580952dSDimitry Andric }
531e580952dSDimitry Andric 
532e580952dSDimitry Andric void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
5332754fe60SDimitry Andric   assert(OpenIdx && "openIntv not called before useIntv");
5342754fe60SDimitry Andric   DEBUG(dbgs() << "    useIntv [" << Start << ';' << End << "):");
5352754fe60SDimitry Andric   RegAssign.insert(Start, End, OpenIdx);
5362754fe60SDimitry Andric   DEBUG(dump());
537e580952dSDimitry Andric }
538e580952dSDimitry Andric 
5392754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
5402754fe60SDimitry Andric   assert(OpenIdx && "openIntv not called before leaveIntvAfter");
5412754fe60SDimitry Andric   DEBUG(dbgs() << "    leaveIntvAfter " << Idx);
5422754fe60SDimitry Andric 
5432754fe60SDimitry Andric   // The interval must be live beyond the instruction at Idx.
5446122f3e6SDimitry Andric   SlotIndex Boundary = Idx.getBoundaryIndex();
5456122f3e6SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
5462754fe60SDimitry Andric   if (!ParentVNI) {
5472754fe60SDimitry Andric     DEBUG(dbgs() << ": not live\n");
5486122f3e6SDimitry Andric     return Boundary.getNextSlot();
5492754fe60SDimitry Andric   }
5502754fe60SDimitry Andric   DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
5516122f3e6SDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
5522754fe60SDimitry Andric   assert(MI && "No instruction at index");
5536122f3e6SDimitry Andric 
5546122f3e6SDimitry Andric   // In spill mode, make live ranges as short as possible by inserting the copy
5556122f3e6SDimitry Andric   // before MI.  This is only possible if that instruction doesn't redefine the
5566122f3e6SDimitry Andric   // value.  The inserted COPY is not a kill, and we don't need to recompute
5576122f3e6SDimitry Andric   // the source live range.  The spiller also won't try to hoist this copy.
5586122f3e6SDimitry Andric   if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
5596122f3e6SDimitry Andric       MI->readsVirtualRegister(Edit->getReg())) {
5606122f3e6SDimitry Andric     forceRecompute(0, ParentVNI);
5616122f3e6SDimitry Andric     defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
5626122f3e6SDimitry Andric     return Idx;
5636122f3e6SDimitry Andric   }
5646122f3e6SDimitry Andric 
5656122f3e6SDimitry Andric   VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
5662754fe60SDimitry Andric                               llvm::next(MachineBasicBlock::iterator(MI)));
5672754fe60SDimitry Andric   return VNI->def;
568e580952dSDimitry Andric }
569e580952dSDimitry Andric 
5702754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
5712754fe60SDimitry Andric   assert(OpenIdx && "openIntv not called before leaveIntvBefore");
5722754fe60SDimitry Andric   DEBUG(dbgs() << "    leaveIntvBefore " << Idx);
573e580952dSDimitry Andric 
5742754fe60SDimitry Andric   // The interval must be live into the instruction at Idx.
5756122f3e6SDimitry Andric   Idx = Idx.getBaseIndex();
5763b0f4066SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
5772754fe60SDimitry Andric   if (!ParentVNI) {
5782754fe60SDimitry Andric     DEBUG(dbgs() << ": not live\n");
5792754fe60SDimitry Andric     return Idx.getNextSlot();
5802754fe60SDimitry Andric   }
5812754fe60SDimitry Andric   DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
5822754fe60SDimitry Andric 
5832754fe60SDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
5842754fe60SDimitry Andric   assert(MI && "No instruction at index");
5852754fe60SDimitry Andric   VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
5862754fe60SDimitry Andric   return VNI->def;
587e580952dSDimitry Andric }
588e580952dSDimitry Andric 
5892754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
5902754fe60SDimitry Andric   assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
5912754fe60SDimitry Andric   SlotIndex Start = LIS.getMBBStartIdx(&MBB);
5922754fe60SDimitry Andric   DEBUG(dbgs() << "    leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
5932754fe60SDimitry Andric 
5943b0f4066SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
5952754fe60SDimitry Andric   if (!ParentVNI) {
5962754fe60SDimitry Andric     DEBUG(dbgs() << ": not live\n");
5972754fe60SDimitry Andric     return Start;
598e580952dSDimitry Andric   }
599e580952dSDimitry Andric 
6002754fe60SDimitry Andric   VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
6012754fe60SDimitry Andric                               MBB.SkipPHIsAndLabels(MBB.begin()));
6022754fe60SDimitry Andric   RegAssign.insert(Start, VNI->def, OpenIdx);
6032754fe60SDimitry Andric   DEBUG(dump());
6042754fe60SDimitry Andric   return VNI->def;
605e580952dSDimitry Andric }
606e580952dSDimitry Andric 
6072754fe60SDimitry Andric void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
6082754fe60SDimitry Andric   assert(OpenIdx && "openIntv not called before overlapIntv");
6093b0f4066SDimitry Andric   const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
610dff0c46cSDimitry Andric   assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
6112754fe60SDimitry Andric          "Parent changes value in extended range");
6122754fe60SDimitry Andric   assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
6132754fe60SDimitry Andric          "Range cannot span basic blocks");
614e580952dSDimitry Andric 
6156122f3e6SDimitry Andric   // The complement interval will be extended as needed by LRCalc.extend().
6163b0f4066SDimitry Andric   if (ParentVNI)
6176122f3e6SDimitry Andric     forceRecompute(0, ParentVNI);
6182754fe60SDimitry Andric   DEBUG(dbgs() << "    overlapIntv [" << Start << ';' << End << "):");
6192754fe60SDimitry Andric   RegAssign.insert(Start, End, OpenIdx);
6202754fe60SDimitry Andric   DEBUG(dump());
621e580952dSDimitry Andric }
622e580952dSDimitry Andric 
6236122f3e6SDimitry Andric //===----------------------------------------------------------------------===//
6246122f3e6SDimitry Andric //                                  Spill modes
6256122f3e6SDimitry Andric //===----------------------------------------------------------------------===//
6266122f3e6SDimitry Andric 
6276122f3e6SDimitry Andric void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
6286122f3e6SDimitry Andric   LiveInterval *LI = Edit->get(0);
6296122f3e6SDimitry Andric   DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
6306122f3e6SDimitry Andric   RegAssignMap::iterator AssignI;
6316122f3e6SDimitry Andric   AssignI.setMap(RegAssign);
6326122f3e6SDimitry Andric 
6336122f3e6SDimitry Andric   for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
6346122f3e6SDimitry Andric     VNInfo *VNI = Copies[i];
6356122f3e6SDimitry Andric     SlotIndex Def = VNI->def;
6366122f3e6SDimitry Andric     MachineInstr *MI = LIS.getInstructionFromIndex(Def);
6376122f3e6SDimitry Andric     assert(MI && "No instruction for back-copy");
6386122f3e6SDimitry Andric 
6396122f3e6SDimitry Andric     MachineBasicBlock *MBB = MI->getParent();
6406122f3e6SDimitry Andric     MachineBasicBlock::iterator MBBI(MI);
6416122f3e6SDimitry Andric     bool AtBegin;
6426122f3e6SDimitry Andric     do AtBegin = MBBI == MBB->begin();
6436122f3e6SDimitry Andric     while (!AtBegin && (--MBBI)->isDebugValue());
6446122f3e6SDimitry Andric 
6456122f3e6SDimitry Andric     DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
6466122f3e6SDimitry Andric     LI->removeValNo(VNI);
6476122f3e6SDimitry Andric     LIS.RemoveMachineInstrFromMaps(MI);
6486122f3e6SDimitry Andric     MI->eraseFromParent();
6496122f3e6SDimitry Andric 
6506122f3e6SDimitry Andric     // Adjust RegAssign if a register assignment is killed at VNI->def.  We
6516122f3e6SDimitry Andric     // want to avoid calculating the live range of the source register if
6526122f3e6SDimitry Andric     // possible.
6536122f3e6SDimitry Andric     AssignI.find(VNI->def.getPrevSlot());
6546122f3e6SDimitry Andric     if (!AssignI.valid() || AssignI.start() >= Def)
6556122f3e6SDimitry Andric       continue;
6566122f3e6SDimitry Andric     // If MI doesn't kill the assigned register, just leave it.
6576122f3e6SDimitry Andric     if (AssignI.stop() != Def)
6586122f3e6SDimitry Andric       continue;
6596122f3e6SDimitry Andric     unsigned RegIdx = AssignI.value();
6606122f3e6SDimitry Andric     if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) {
6616122f3e6SDimitry Andric       DEBUG(dbgs() << "  cannot find simple kill of RegIdx " << RegIdx << '\n');
6626122f3e6SDimitry Andric       forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def));
6636122f3e6SDimitry Andric     } else {
664dff0c46cSDimitry Andric       SlotIndex Kill = LIS.getInstructionIndex(MBBI).getRegSlot();
6656122f3e6SDimitry Andric       DEBUG(dbgs() << "  move kill to " << Kill << '\t' << *MBBI);
6666122f3e6SDimitry Andric       AssignI.setStop(Kill);
6676122f3e6SDimitry Andric     }
6686122f3e6SDimitry Andric   }
6696122f3e6SDimitry Andric }
6706122f3e6SDimitry Andric 
6716122f3e6SDimitry Andric MachineBasicBlock*
6726122f3e6SDimitry Andric SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
6736122f3e6SDimitry Andric                                   MachineBasicBlock *DefMBB) {
6746122f3e6SDimitry Andric   if (MBB == DefMBB)
6756122f3e6SDimitry Andric     return MBB;
6766122f3e6SDimitry Andric   assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
6776122f3e6SDimitry Andric 
6786122f3e6SDimitry Andric   const MachineLoopInfo &Loops = SA.Loops;
6796122f3e6SDimitry Andric   const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
6806122f3e6SDimitry Andric   MachineDomTreeNode *DefDomNode = MDT[DefMBB];
6816122f3e6SDimitry Andric 
6826122f3e6SDimitry Andric   // Best candidate so far.
6836122f3e6SDimitry Andric   MachineBasicBlock *BestMBB = MBB;
6846122f3e6SDimitry Andric   unsigned BestDepth = UINT_MAX;
6856122f3e6SDimitry Andric 
6866122f3e6SDimitry Andric   for (;;) {
6876122f3e6SDimitry Andric     const MachineLoop *Loop = Loops.getLoopFor(MBB);
6886122f3e6SDimitry Andric 
6896122f3e6SDimitry Andric     // MBB isn't in a loop, it doesn't get any better.  All dominators have a
6906122f3e6SDimitry Andric     // higher frequency by definition.
6916122f3e6SDimitry Andric     if (!Loop) {
6926122f3e6SDimitry Andric       DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
6936122f3e6SDimitry Andric                    << MBB->getNumber() << " at depth 0\n");
6946122f3e6SDimitry Andric       return MBB;
6956122f3e6SDimitry Andric     }
6966122f3e6SDimitry Andric 
6976122f3e6SDimitry Andric     // We'll never be able to exit the DefLoop.
6986122f3e6SDimitry Andric     if (Loop == DefLoop) {
6996122f3e6SDimitry Andric       DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
7006122f3e6SDimitry Andric                    << MBB->getNumber() << " in the same loop\n");
7016122f3e6SDimitry Andric       return MBB;
7026122f3e6SDimitry Andric     }
7036122f3e6SDimitry Andric 
7046122f3e6SDimitry Andric     // Least busy dominator seen so far.
7056122f3e6SDimitry Andric     unsigned Depth = Loop->getLoopDepth();
7066122f3e6SDimitry Andric     if (Depth < BestDepth) {
7076122f3e6SDimitry Andric       BestMBB = MBB;
7086122f3e6SDimitry Andric       BestDepth = Depth;
7096122f3e6SDimitry Andric       DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
7106122f3e6SDimitry Andric                    << MBB->getNumber() << " at depth " << Depth << '\n');
7116122f3e6SDimitry Andric     }
7126122f3e6SDimitry Andric 
7136122f3e6SDimitry Andric     // Leave loop by going to the immediate dominator of the loop header.
7146122f3e6SDimitry Andric     // This is a bigger stride than simply walking up the dominator tree.
7156122f3e6SDimitry Andric     MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
7166122f3e6SDimitry Andric 
7176122f3e6SDimitry Andric     // Too far up the dominator tree?
7186122f3e6SDimitry Andric     if (!IDom || !MDT.dominates(DefDomNode, IDom))
7196122f3e6SDimitry Andric       return BestMBB;
7206122f3e6SDimitry Andric 
7216122f3e6SDimitry Andric     MBB = IDom->getBlock();
7226122f3e6SDimitry Andric   }
7236122f3e6SDimitry Andric }
7246122f3e6SDimitry Andric 
7256122f3e6SDimitry Andric void SplitEditor::hoistCopiesForSize() {
7266122f3e6SDimitry Andric   // Get the complement interval, always RegIdx 0.
7276122f3e6SDimitry Andric   LiveInterval *LI = Edit->get(0);
7286122f3e6SDimitry Andric   LiveInterval *Parent = &Edit->getParent();
7296122f3e6SDimitry Andric 
7306122f3e6SDimitry Andric   // Track the nearest common dominator for all back-copies for each ParentVNI,
7316122f3e6SDimitry Andric   // indexed by ParentVNI->id.
7326122f3e6SDimitry Andric   typedef std::pair<MachineBasicBlock*, SlotIndex> DomPair;
7336122f3e6SDimitry Andric   SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
7346122f3e6SDimitry Andric 
7356122f3e6SDimitry Andric   // Find the nearest common dominator for parent values with multiple
7366122f3e6SDimitry Andric   // back-copies.  If a single back-copy dominates, put it in DomPair.second.
7376122f3e6SDimitry Andric   for (LiveInterval::vni_iterator VI = LI->vni_begin(), VE = LI->vni_end();
7386122f3e6SDimitry Andric        VI != VE; ++VI) {
7396122f3e6SDimitry Andric     VNInfo *VNI = *VI;
7406122f3e6SDimitry Andric     VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
7416122f3e6SDimitry Andric     assert(ParentVNI && "Parent not live at complement def");
7426122f3e6SDimitry Andric 
7436122f3e6SDimitry Andric     // Don't hoist remats.  The complement is probably going to disappear
7446122f3e6SDimitry Andric     // completely anyway.
7456122f3e6SDimitry Andric     if (Edit->didRematerialize(ParentVNI))
7466122f3e6SDimitry Andric       continue;
7476122f3e6SDimitry Andric 
7486122f3e6SDimitry Andric     MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
7496122f3e6SDimitry Andric     DomPair &Dom = NearestDom[ParentVNI->id];
7506122f3e6SDimitry Andric 
7516122f3e6SDimitry Andric     // Keep directly defined parent values.  This is either a PHI or an
7526122f3e6SDimitry Andric     // instruction in the complement range.  All other copies of ParentVNI
7536122f3e6SDimitry Andric     // should be eliminated.
7546122f3e6SDimitry Andric     if (VNI->def == ParentVNI->def) {
7556122f3e6SDimitry Andric       DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
7566122f3e6SDimitry Andric       Dom = DomPair(ValMBB, VNI->def);
7576122f3e6SDimitry Andric       continue;
7586122f3e6SDimitry Andric     }
7596122f3e6SDimitry Andric     // Skip the singly mapped values.  There is nothing to gain from hoisting a
7606122f3e6SDimitry Andric     // single back-copy.
7616122f3e6SDimitry Andric     if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
7626122f3e6SDimitry Andric       DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
7636122f3e6SDimitry Andric       continue;
7646122f3e6SDimitry Andric     }
7656122f3e6SDimitry Andric 
7666122f3e6SDimitry Andric     if (!Dom.first) {
7676122f3e6SDimitry Andric       // First time we see ParentVNI.  VNI dominates itself.
7686122f3e6SDimitry Andric       Dom = DomPair(ValMBB, VNI->def);
7696122f3e6SDimitry Andric     } else if (Dom.first == ValMBB) {
7706122f3e6SDimitry Andric       // Two defs in the same block.  Pick the earlier def.
7716122f3e6SDimitry Andric       if (!Dom.second.isValid() || VNI->def < Dom.second)
7726122f3e6SDimitry Andric         Dom.second = VNI->def;
7736122f3e6SDimitry Andric     } else {
7746122f3e6SDimitry Andric       // Different basic blocks. Check if one dominates.
7756122f3e6SDimitry Andric       MachineBasicBlock *Near =
7766122f3e6SDimitry Andric         MDT.findNearestCommonDominator(Dom.first, ValMBB);
7776122f3e6SDimitry Andric       if (Near == ValMBB)
7786122f3e6SDimitry Andric         // Def ValMBB dominates.
7796122f3e6SDimitry Andric         Dom = DomPair(ValMBB, VNI->def);
7806122f3e6SDimitry Andric       else if (Near != Dom.first)
7816122f3e6SDimitry Andric         // None dominate. Hoist to common dominator, need new def.
7826122f3e6SDimitry Andric         Dom = DomPair(Near, SlotIndex());
7836122f3e6SDimitry Andric     }
7846122f3e6SDimitry Andric 
7856122f3e6SDimitry Andric     DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def
7866122f3e6SDimitry Andric                  << " for parent " << ParentVNI->id << '@' << ParentVNI->def
7876122f3e6SDimitry Andric                  << " hoist to BB#" << Dom.first->getNumber() << ' '
7886122f3e6SDimitry Andric                  << Dom.second << '\n');
7896122f3e6SDimitry Andric   }
7906122f3e6SDimitry Andric 
7916122f3e6SDimitry Andric   // Insert the hoisted copies.
7926122f3e6SDimitry Andric   for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
7936122f3e6SDimitry Andric     DomPair &Dom = NearestDom[i];
7946122f3e6SDimitry Andric     if (!Dom.first || Dom.second.isValid())
7956122f3e6SDimitry Andric       continue;
7966122f3e6SDimitry Andric     // This value needs a hoisted copy inserted at the end of Dom.first.
7976122f3e6SDimitry Andric     VNInfo *ParentVNI = Parent->getValNumInfo(i);
7986122f3e6SDimitry Andric     MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
7996122f3e6SDimitry Andric     // Get a less loopy dominator than Dom.first.
8006122f3e6SDimitry Andric     Dom.first = findShallowDominator(Dom.first, DefMBB);
8016122f3e6SDimitry Andric     SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot();
8026122f3e6SDimitry Andric     Dom.second =
8036122f3e6SDimitry Andric       defFromParent(0, ParentVNI, Last, *Dom.first,
804dff0c46cSDimitry Andric                     SA.getLastSplitPointIter(Dom.first))->def;
8056122f3e6SDimitry Andric   }
8066122f3e6SDimitry Andric 
8076122f3e6SDimitry Andric   // Remove redundant back-copies that are now known to be dominated by another
8086122f3e6SDimitry Andric   // def with the same value.
8096122f3e6SDimitry Andric   SmallVector<VNInfo*, 8> BackCopies;
8106122f3e6SDimitry Andric   for (LiveInterval::vni_iterator VI = LI->vni_begin(), VE = LI->vni_end();
8116122f3e6SDimitry Andric        VI != VE; ++VI) {
8126122f3e6SDimitry Andric     VNInfo *VNI = *VI;
8136122f3e6SDimitry Andric     VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
8146122f3e6SDimitry Andric     const DomPair &Dom = NearestDom[ParentVNI->id];
8156122f3e6SDimitry Andric     if (!Dom.first || Dom.second == VNI->def)
8166122f3e6SDimitry Andric       continue;
8176122f3e6SDimitry Andric     BackCopies.push_back(VNI);
8186122f3e6SDimitry Andric     forceRecompute(0, ParentVNI);
8196122f3e6SDimitry Andric   }
8206122f3e6SDimitry Andric   removeBackCopies(BackCopies);
8216122f3e6SDimitry Andric }
8226122f3e6SDimitry Andric 
8236122f3e6SDimitry Andric 
8243b0f4066SDimitry Andric /// transferValues - Transfer all possible values to the new live ranges.
8256122f3e6SDimitry Andric /// Values that were rematerialized are left alone, they need LRCalc.extend().
8263b0f4066SDimitry Andric bool SplitEditor::transferValues() {
8273b0f4066SDimitry Andric   bool Skipped = false;
8283b0f4066SDimitry Andric   RegAssignMap::const_iterator AssignI = RegAssign.begin();
8293b0f4066SDimitry Andric   for (LiveInterval::const_iterator ParentI = Edit->getParent().begin(),
8303b0f4066SDimitry Andric          ParentE = Edit->getParent().end(); ParentI != ParentE; ++ParentI) {
8313b0f4066SDimitry Andric     DEBUG(dbgs() << "  blit " << *ParentI << ':');
8323b0f4066SDimitry Andric     VNInfo *ParentVNI = ParentI->valno;
8333b0f4066SDimitry Andric     // RegAssign has holes where RegIdx 0 should be used.
8343b0f4066SDimitry Andric     SlotIndex Start = ParentI->start;
8353b0f4066SDimitry Andric     AssignI.advanceTo(Start);
8363b0f4066SDimitry Andric     do {
8373b0f4066SDimitry Andric       unsigned RegIdx;
8383b0f4066SDimitry Andric       SlotIndex End = ParentI->end;
8393b0f4066SDimitry Andric       if (!AssignI.valid()) {
8403b0f4066SDimitry Andric         RegIdx = 0;
8413b0f4066SDimitry Andric       } else if (AssignI.start() <= Start) {
8423b0f4066SDimitry Andric         RegIdx = AssignI.value();
8433b0f4066SDimitry Andric         if (AssignI.stop() < End) {
8443b0f4066SDimitry Andric           End = AssignI.stop();
8453b0f4066SDimitry Andric           ++AssignI;
8463b0f4066SDimitry Andric         }
8473b0f4066SDimitry Andric       } else {
8483b0f4066SDimitry Andric         RegIdx = 0;
8493b0f4066SDimitry Andric         End = std::min(End, AssignI.start());
850e580952dSDimitry Andric       }
851e580952dSDimitry Andric 
8523b0f4066SDimitry Andric       // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
8533b0f4066SDimitry Andric       DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx);
8543b0f4066SDimitry Andric       LiveInterval *LI = Edit->get(RegIdx);
8553b0f4066SDimitry Andric 
8563b0f4066SDimitry Andric       // Check for a simply defined value that can be blitted directly.
8576122f3e6SDimitry Andric       ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
8586122f3e6SDimitry Andric       if (VNInfo *VNI = VFP.getPointer()) {
8593b0f4066SDimitry Andric         DEBUG(dbgs() << ':' << VNI->id);
8603b0f4066SDimitry Andric         LI->addRange(LiveRange(Start, End, VNI));
8613b0f4066SDimitry Andric         Start = End;
8623b0f4066SDimitry Andric         continue;
8633b0f4066SDimitry Andric       }
8643b0f4066SDimitry Andric 
8656122f3e6SDimitry Andric       // Skip values with forced recomputation.
8666122f3e6SDimitry Andric       if (VFP.getInt()) {
8676122f3e6SDimitry Andric         DEBUG(dbgs() << "(recalc)");
8683b0f4066SDimitry Andric         Skipped = true;
8693b0f4066SDimitry Andric         Start = End;
8703b0f4066SDimitry Andric         continue;
8713b0f4066SDimitry Andric       }
8723b0f4066SDimitry Andric 
8736122f3e6SDimitry Andric       LiveRangeCalc &LRC = getLRCalc(RegIdx);
8743b0f4066SDimitry Andric 
8753b0f4066SDimitry Andric       // This value has multiple defs in RegIdx, but it wasn't rematerialized,
8763b0f4066SDimitry Andric       // so the live range is accurate. Add live-in blocks in [Start;End) to the
8773b0f4066SDimitry Andric       // LiveInBlocks.
8783b0f4066SDimitry Andric       MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
8793b0f4066SDimitry Andric       SlotIndex BlockStart, BlockEnd;
8803b0f4066SDimitry Andric       tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(MBB);
8813b0f4066SDimitry Andric 
8823b0f4066SDimitry Andric       // The first block may be live-in, or it may have its own def.
8833b0f4066SDimitry Andric       if (Start != BlockStart) {
8846122f3e6SDimitry Andric         VNInfo *VNI = LI->extendInBlock(BlockStart, std::min(BlockEnd, End));
8853b0f4066SDimitry Andric         assert(VNI && "Missing def for complex mapped value");
8863b0f4066SDimitry Andric         DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber());
8873b0f4066SDimitry Andric         // MBB has its own def. Is it also live-out?
8886122f3e6SDimitry Andric         if (BlockEnd <= End)
8896122f3e6SDimitry Andric           LRC.setLiveOutValue(MBB, VNI);
8906122f3e6SDimitry Andric 
8913b0f4066SDimitry Andric         // Skip to the next block for live-in.
8923b0f4066SDimitry Andric         ++MBB;
8933b0f4066SDimitry Andric         BlockStart = BlockEnd;
8943b0f4066SDimitry Andric       }
8953b0f4066SDimitry Andric 
8963b0f4066SDimitry Andric       // Handle the live-in blocks covered by [Start;End).
8973b0f4066SDimitry Andric       assert(Start <= BlockStart && "Expected live-in block");
8983b0f4066SDimitry Andric       while (BlockStart < End) {
8993b0f4066SDimitry Andric         DEBUG(dbgs() << ">BB#" << MBB->getNumber());
9003b0f4066SDimitry Andric         BlockEnd = LIS.getMBBEndIdx(MBB);
9013b0f4066SDimitry Andric         if (BlockStart == ParentVNI->def) {
9023b0f4066SDimitry Andric           // This block has the def of a parent PHI, so it isn't live-in.
9033b0f4066SDimitry Andric           assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
9046122f3e6SDimitry Andric           VNInfo *VNI = LI->extendInBlock(BlockStart, std::min(BlockEnd, End));
9053b0f4066SDimitry Andric           assert(VNI && "Missing def for complex mapped parent PHI");
9066122f3e6SDimitry Andric           if (End >= BlockEnd)
9076122f3e6SDimitry Andric             LRC.setLiveOutValue(MBB, VNI); // Live-out as well.
9083b0f4066SDimitry Andric         } else {
9096122f3e6SDimitry Andric           // This block needs a live-in value.  The last block covered may not
9106122f3e6SDimitry Andric           // be live-out.
9113b0f4066SDimitry Andric           if (End < BlockEnd)
9126122f3e6SDimitry Andric             LRC.addLiveInBlock(LI, MDT[MBB], End);
9133b0f4066SDimitry Andric           else {
9146122f3e6SDimitry Andric             // Live-through, and we don't know the value.
9156122f3e6SDimitry Andric             LRC.addLiveInBlock(LI, MDT[MBB]);
9166122f3e6SDimitry Andric             LRC.setLiveOutValue(MBB, 0);
9173b0f4066SDimitry Andric           }
9183b0f4066SDimitry Andric         }
9193b0f4066SDimitry Andric         BlockStart = BlockEnd;
9203b0f4066SDimitry Andric         ++MBB;
9213b0f4066SDimitry Andric       }
9223b0f4066SDimitry Andric       Start = End;
9233b0f4066SDimitry Andric     } while (Start != ParentI->end);
9243b0f4066SDimitry Andric     DEBUG(dbgs() << '\n');
9253b0f4066SDimitry Andric   }
9263b0f4066SDimitry Andric 
9276122f3e6SDimitry Andric   LRCalc[0].calculateValues(LIS.getSlotIndexes(), &MDT,
9286122f3e6SDimitry Andric                             &LIS.getVNInfoAllocator());
9296122f3e6SDimitry Andric   if (SpillMode)
9306122f3e6SDimitry Andric     LRCalc[1].calculateValues(LIS.getSlotIndexes(), &MDT,
9316122f3e6SDimitry Andric                               &LIS.getVNInfoAllocator());
9323b0f4066SDimitry Andric 
9333b0f4066SDimitry Andric   return Skipped;
9343b0f4066SDimitry Andric }
9353b0f4066SDimitry Andric 
9363b0f4066SDimitry Andric void SplitEditor::extendPHIKillRanges() {
9373b0f4066SDimitry Andric     // Extend live ranges to be live-out for successor PHI values.
9383b0f4066SDimitry Andric   for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
9393b0f4066SDimitry Andric        E = Edit->getParent().vni_end(); I != E; ++I) {
9403b0f4066SDimitry Andric     const VNInfo *PHIVNI = *I;
9413b0f4066SDimitry Andric     if (PHIVNI->isUnused() || !PHIVNI->isPHIDef())
9423b0f4066SDimitry Andric       continue;
9433b0f4066SDimitry Andric     unsigned RegIdx = RegAssign.lookup(PHIVNI->def);
9446122f3e6SDimitry Andric     LiveInterval *LI = Edit->get(RegIdx);
9456122f3e6SDimitry Andric     LiveRangeCalc &LRC = getLRCalc(RegIdx);
9463b0f4066SDimitry Andric     MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
9473b0f4066SDimitry Andric     for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
9483b0f4066SDimitry Andric          PE = MBB->pred_end(); PI != PE; ++PI) {
9496122f3e6SDimitry Andric       SlotIndex End = LIS.getMBBEndIdx(*PI);
9506122f3e6SDimitry Andric       SlotIndex LastUse = End.getPrevSlot();
9513b0f4066SDimitry Andric       // The predecessor may not have a live-out value. That is OK, like an
9523b0f4066SDimitry Andric       // undef PHI operand.
9536122f3e6SDimitry Andric       if (Edit->getParent().liveAt(LastUse)) {
9546122f3e6SDimitry Andric         assert(RegAssign.lookup(LastUse) == RegIdx &&
9553b0f4066SDimitry Andric                "Different register assignment in phi predecessor");
9566122f3e6SDimitry Andric         LRC.extend(LI, End,
9576122f3e6SDimitry Andric                    LIS.getSlotIndexes(), &MDT, &LIS.getVNInfoAllocator());
9583b0f4066SDimitry Andric       }
9593b0f4066SDimitry Andric     }
9603b0f4066SDimitry Andric   }
9613b0f4066SDimitry Andric }
9623b0f4066SDimitry Andric 
9633b0f4066SDimitry Andric /// rewriteAssigned - Rewrite all uses of Edit->getReg().
9643b0f4066SDimitry Andric void SplitEditor::rewriteAssigned(bool ExtendRanges) {
9653b0f4066SDimitry Andric   for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
9662754fe60SDimitry Andric        RE = MRI.reg_end(); RI != RE;) {
967e580952dSDimitry Andric     MachineOperand &MO = RI.getOperand();
968e580952dSDimitry Andric     MachineInstr *MI = MO.getParent();
969e580952dSDimitry Andric     ++RI;
9702754fe60SDimitry Andric     // LiveDebugVariables should have handled all DBG_VALUE instructions.
971e580952dSDimitry Andric     if (MI->isDebugValue()) {
972e580952dSDimitry Andric       DEBUG(dbgs() << "Zapping " << *MI);
973e580952dSDimitry Andric       MO.setReg(0);
974e580952dSDimitry Andric       continue;
975e580952dSDimitry Andric     }
9762754fe60SDimitry Andric 
9776122f3e6SDimitry Andric     // <undef> operands don't really read the register, so it doesn't matter
9786122f3e6SDimitry Andric     // which register we choose.  When the use operand is tied to a def, we must
9796122f3e6SDimitry Andric     // use the same register as the def, so just do that always.
9802754fe60SDimitry Andric     SlotIndex Idx = LIS.getInstructionIndex(MI);
9816122f3e6SDimitry Andric     if (MO.isDef() || MO.isUndef())
982dff0c46cSDimitry Andric       Idx = Idx.getRegSlot(MO.isEarlyClobber());
9832754fe60SDimitry Andric 
9842754fe60SDimitry Andric     // Rewrite to the mapped register at Idx.
9852754fe60SDimitry Andric     unsigned RegIdx = RegAssign.lookup(Idx);
9866122f3e6SDimitry Andric     LiveInterval *LI = Edit->get(RegIdx);
9876122f3e6SDimitry Andric     MO.setReg(LI->reg);
9882754fe60SDimitry Andric     DEBUG(dbgs() << "  rewr BB#" << MI->getParent()->getNumber() << '\t'
9892754fe60SDimitry Andric                  << Idx << ':' << RegIdx << '\t' << *MI);
9902754fe60SDimitry Andric 
9913b0f4066SDimitry Andric     // Extend liveness to Idx if the instruction reads reg.
9926122f3e6SDimitry Andric     if (!ExtendRanges || MO.isUndef())
9932754fe60SDimitry Andric       continue;
9943b0f4066SDimitry Andric 
9953b0f4066SDimitry Andric     // Skip instructions that don't read Reg.
9963b0f4066SDimitry Andric     if (MO.isDef()) {
9973b0f4066SDimitry Andric       if (!MO.getSubReg() && !MO.isEarlyClobber())
9983b0f4066SDimitry Andric         continue;
9993b0f4066SDimitry Andric       // We may wan't to extend a live range for a partial redef, or for a use
10003b0f4066SDimitry Andric       // tied to an early clobber.
10013b0f4066SDimitry Andric       Idx = Idx.getPrevSlot();
10023b0f4066SDimitry Andric       if (!Edit->getParent().liveAt(Idx))
10033b0f4066SDimitry Andric         continue;
10043b0f4066SDimitry Andric     } else
1005dff0c46cSDimitry Andric       Idx = Idx.getRegSlot(true);
10063b0f4066SDimitry Andric 
10076122f3e6SDimitry Andric     getLRCalc(RegIdx).extend(LI, Idx.getNextSlot(), LIS.getSlotIndexes(),
10086122f3e6SDimitry Andric                              &MDT, &LIS.getVNInfoAllocator());
1009e580952dSDimitry Andric   }
1010e580952dSDimitry Andric }
1011e580952dSDimitry Andric 
10123b0f4066SDimitry Andric void SplitEditor::deleteRematVictims() {
10133b0f4066SDimitry Andric   SmallVector<MachineInstr*, 8> Dead;
10143b0f4066SDimitry Andric   for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
10153b0f4066SDimitry Andric     LiveInterval *LI = *I;
10163b0f4066SDimitry Andric     for (LiveInterval::const_iterator LII = LI->begin(), LIE = LI->end();
10173b0f4066SDimitry Andric            LII != LIE; ++LII) {
1018dff0c46cSDimitry Andric       // Dead defs end at the dead slot.
1019dff0c46cSDimitry Andric       if (LII->end != LII->valno->def.getDeadSlot())
10203b0f4066SDimitry Andric         continue;
10213b0f4066SDimitry Andric       MachineInstr *MI = LIS.getInstructionFromIndex(LII->valno->def);
10223b0f4066SDimitry Andric       assert(MI && "Missing instruction for dead def");
10233b0f4066SDimitry Andric       MI->addRegisterDead(LI->reg, &TRI);
10243b0f4066SDimitry Andric 
10253b0f4066SDimitry Andric       if (!MI->allDefsAreDead())
10263b0f4066SDimitry Andric         continue;
10273b0f4066SDimitry Andric 
10283b0f4066SDimitry Andric       DEBUG(dbgs() << "All defs dead: " << *MI);
10293b0f4066SDimitry Andric       Dead.push_back(MI);
10303b0f4066SDimitry Andric     }
10313b0f4066SDimitry Andric   }
10323b0f4066SDimitry Andric 
10333b0f4066SDimitry Andric   if (Dead.empty())
10343b0f4066SDimitry Andric     return;
10353b0f4066SDimitry Andric 
1036dff0c46cSDimitry Andric   Edit->eliminateDeadDefs(Dead);
10373b0f4066SDimitry Andric }
10383b0f4066SDimitry Andric 
10393b0f4066SDimitry Andric void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
10403b0f4066SDimitry Andric   ++NumFinished;
10412754fe60SDimitry Andric 
10422754fe60SDimitry Andric   // At this point, the live intervals in Edit contain VNInfos corresponding to
10432754fe60SDimitry Andric   // the inserted copies.
10442754fe60SDimitry Andric 
10452754fe60SDimitry Andric   // Add the original defs from the parent interval.
10463b0f4066SDimitry Andric   for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
10473b0f4066SDimitry Andric          E = Edit->getParent().vni_end(); I != E; ++I) {
10482754fe60SDimitry Andric     const VNInfo *ParentVNI = *I;
10492754fe60SDimitry Andric     if (ParentVNI->isUnused())
10502754fe60SDimitry Andric       continue;
10513b0f4066SDimitry Andric     unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
10523b0f4066SDimitry Andric     VNInfo *VNI = defValue(RegIdx, ParentVNI, ParentVNI->def);
10533b0f4066SDimitry Andric     VNI->setIsPHIDef(ParentVNI->isPHIDef());
10543b0f4066SDimitry Andric 
10556122f3e6SDimitry Andric     // Force rematted values to be recomputed everywhere.
10563b0f4066SDimitry Andric     // The new live ranges may be truncated.
10573b0f4066SDimitry Andric     if (Edit->didRematerialize(ParentVNI))
10583b0f4066SDimitry Andric       for (unsigned i = 0, e = Edit->size(); i != e; ++i)
10596122f3e6SDimitry Andric         forceRecompute(i, ParentVNI);
10606122f3e6SDimitry Andric   }
10616122f3e6SDimitry Andric 
10626122f3e6SDimitry Andric   // Hoist back-copies to the complement interval when in spill mode.
10636122f3e6SDimitry Andric   switch (SpillMode) {
10646122f3e6SDimitry Andric   case SM_Partition:
10656122f3e6SDimitry Andric     // Leave all back-copies as is.
10666122f3e6SDimitry Andric     break;
10676122f3e6SDimitry Andric   case SM_Size:
10686122f3e6SDimitry Andric     hoistCopiesForSize();
10696122f3e6SDimitry Andric     break;
10706122f3e6SDimitry Andric   case SM_Speed:
10716122f3e6SDimitry Andric     llvm_unreachable("Spill mode 'speed' not implemented yet");
10722754fe60SDimitry Andric   }
10732754fe60SDimitry Andric 
10743b0f4066SDimitry Andric   // Transfer the simply mapped values, check if any are skipped.
10753b0f4066SDimitry Andric   bool Skipped = transferValues();
10763b0f4066SDimitry Andric   if (Skipped)
10773b0f4066SDimitry Andric     extendPHIKillRanges();
10782754fe60SDimitry Andric   else
10793b0f4066SDimitry Andric     ++NumSimple;
10802754fe60SDimitry Andric 
10813b0f4066SDimitry Andric   // Rewrite virtual registers, possibly extending ranges.
10823b0f4066SDimitry Andric   rewriteAssigned(Skipped);
10832754fe60SDimitry Andric 
10843b0f4066SDimitry Andric   // Delete defs that were rematted everywhere.
10853b0f4066SDimitry Andric   if (Skipped)
10863b0f4066SDimitry Andric     deleteRematVictims();
10872754fe60SDimitry Andric 
10882754fe60SDimitry Andric   // Get rid of unused values and set phi-kill flags.
10893b0f4066SDimitry Andric   for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I)
10902754fe60SDimitry Andric     (*I)->RenumberValues(LIS);
10912754fe60SDimitry Andric 
10923b0f4066SDimitry Andric   // Provide a reverse mapping from original indices to Edit ranges.
10933b0f4066SDimitry Andric   if (LRMap) {
10943b0f4066SDimitry Andric     LRMap->clear();
10953b0f4066SDimitry Andric     for (unsigned i = 0, e = Edit->size(); i != e; ++i)
10963b0f4066SDimitry Andric       LRMap->push_back(i);
10973b0f4066SDimitry Andric   }
10983b0f4066SDimitry Andric 
10992754fe60SDimitry Andric   // Now check if any registers were separated into multiple components.
11002754fe60SDimitry Andric   ConnectedVNInfoEqClasses ConEQ(LIS);
11013b0f4066SDimitry Andric   for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
11022754fe60SDimitry Andric     // Don't use iterators, they are invalidated by create() below.
11033b0f4066SDimitry Andric     LiveInterval *li = Edit->get(i);
11042754fe60SDimitry Andric     unsigned NumComp = ConEQ.Classify(li);
11052754fe60SDimitry Andric     if (NumComp <= 1)
11062754fe60SDimitry Andric       continue;
11072754fe60SDimitry Andric     DEBUG(dbgs() << "  " << NumComp << " components: " << *li << '\n');
11082754fe60SDimitry Andric     SmallVector<LiveInterval*, 8> dups;
11092754fe60SDimitry Andric     dups.push_back(li);
11103b0f4066SDimitry Andric     for (unsigned j = 1; j != NumComp; ++j)
1111dff0c46cSDimitry Andric       dups.push_back(&Edit->create());
11123b0f4066SDimitry Andric     ConEQ.Distribute(&dups[0], MRI);
11133b0f4066SDimitry Andric     // The new intervals all map back to i.
11143b0f4066SDimitry Andric     if (LRMap)
11153b0f4066SDimitry Andric       LRMap->resize(Edit->size(), i);
11162754fe60SDimitry Andric   }
11172754fe60SDimitry Andric 
1118e580952dSDimitry Andric   // Calculate spill weight and allocation hints for new intervals.
1119dff0c46cSDimitry Andric   Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops);
11203b0f4066SDimitry Andric 
11213b0f4066SDimitry Andric   assert(!LRMap || LRMap->size() == Edit->size());
1122e580952dSDimitry Andric }
1123e580952dSDimitry Andric 
1124e580952dSDimitry Andric 
1125e580952dSDimitry Andric //===----------------------------------------------------------------------===//
1126e580952dSDimitry Andric //                            Single Block Splitting
1127e580952dSDimitry Andric //===----------------------------------------------------------------------===//
1128e580952dSDimitry Andric 
11296122f3e6SDimitry Andric bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
11306122f3e6SDimitry Andric                                            bool SingleInstrs) const {
11316122f3e6SDimitry Andric   // Always split for multiple instructions.
11326122f3e6SDimitry Andric   if (!BI.isOneInstr())
11336122f3e6SDimitry Andric     return true;
11346122f3e6SDimitry Andric   // Don't split for single instructions unless explicitly requested.
11356122f3e6SDimitry Andric   if (!SingleInstrs)
11362754fe60SDimitry Andric     return false;
11376122f3e6SDimitry Andric   // Splitting a live-through range always makes progress.
11386122f3e6SDimitry Andric   if (BI.LiveIn && BI.LiveOut)
11396122f3e6SDimitry Andric     return true;
11406122f3e6SDimitry Andric   // No point in isolating a copy. It has no register class constraints.
11416122f3e6SDimitry Andric   if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
11426122f3e6SDimitry Andric     return false;
11436122f3e6SDimitry Andric   // Finally, don't isolate an end point that was created by earlier splits.
11446122f3e6SDimitry Andric   return isOriginalEndpoint(BI.FirstInstr);
1145e580952dSDimitry Andric }
1146e580952dSDimitry Andric 
11473b0f4066SDimitry Andric void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
11483b0f4066SDimitry Andric   openIntv();
11493b0f4066SDimitry Andric   SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber());
11506122f3e6SDimitry Andric   SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
11513b0f4066SDimitry Andric     LastSplitPoint));
11526122f3e6SDimitry Andric   if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
11536122f3e6SDimitry Andric     useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
11543b0f4066SDimitry Andric   } else {
11553b0f4066SDimitry Andric       // The last use is after the last valid split point.
11563b0f4066SDimitry Andric     SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
11573b0f4066SDimitry Andric     useIntv(SegStart, SegStop);
11586122f3e6SDimitry Andric     overlapIntv(SegStop, BI.LastInstr);
11593b0f4066SDimitry Andric   }
11603b0f4066SDimitry Andric }
11613b0f4066SDimitry Andric 
116217a519f9SDimitry Andric 
116317a519f9SDimitry Andric //===----------------------------------------------------------------------===//
116417a519f9SDimitry Andric //                    Global Live Range Splitting Support
116517a519f9SDimitry Andric //===----------------------------------------------------------------------===//
116617a519f9SDimitry Andric 
116717a519f9SDimitry Andric // These methods support a method of global live range splitting that uses a
116817a519f9SDimitry Andric // global algorithm to decide intervals for CFG edges. They will insert split
116917a519f9SDimitry Andric // points and color intervals in basic blocks while avoiding interference.
117017a519f9SDimitry Andric //
117117a519f9SDimitry Andric // Note that splitSingleBlock is also useful for blocks where both CFG edges
117217a519f9SDimitry Andric // are on the stack.
117317a519f9SDimitry Andric 
117417a519f9SDimitry Andric void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
117517a519f9SDimitry Andric                                         unsigned IntvIn, SlotIndex LeaveBefore,
117617a519f9SDimitry Andric                                         unsigned IntvOut, SlotIndex EnterAfter){
117717a519f9SDimitry Andric   SlotIndex Start, Stop;
117817a519f9SDimitry Andric   tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
117917a519f9SDimitry Andric 
118017a519f9SDimitry Andric   DEBUG(dbgs() << "BB#" << MBBNum << " [" << Start << ';' << Stop
118117a519f9SDimitry Andric                << ") intf " << LeaveBefore << '-' << EnterAfter
118217a519f9SDimitry Andric                << ", live-through " << IntvIn << " -> " << IntvOut);
118317a519f9SDimitry Andric 
118417a519f9SDimitry Andric   assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
118517a519f9SDimitry Andric 
11866122f3e6SDimitry Andric   assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
11876122f3e6SDimitry Andric   assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
11886122f3e6SDimitry Andric   assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
11896122f3e6SDimitry Andric 
11906122f3e6SDimitry Andric   MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
11916122f3e6SDimitry Andric 
119217a519f9SDimitry Andric   if (!IntvOut) {
119317a519f9SDimitry Andric     DEBUG(dbgs() << ", spill on entry.\n");
119417a519f9SDimitry Andric     //
119517a519f9SDimitry Andric     //        <<<<<<<<<    Possible LeaveBefore interference.
119617a519f9SDimitry Andric     //    |-----------|    Live through.
119717a519f9SDimitry Andric     //    -____________    Spill on entry.
119817a519f9SDimitry Andric     //
119917a519f9SDimitry Andric     selectIntv(IntvIn);
120017a519f9SDimitry Andric     SlotIndex Idx = leaveIntvAtTop(*MBB);
120117a519f9SDimitry Andric     assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
120217a519f9SDimitry Andric     (void)Idx;
120317a519f9SDimitry Andric     return;
120417a519f9SDimitry Andric   }
120517a519f9SDimitry Andric 
120617a519f9SDimitry Andric   if (!IntvIn) {
120717a519f9SDimitry Andric     DEBUG(dbgs() << ", reload on exit.\n");
120817a519f9SDimitry Andric     //
120917a519f9SDimitry Andric     //    >>>>>>>          Possible EnterAfter interference.
121017a519f9SDimitry Andric     //    |-----------|    Live through.
121117a519f9SDimitry Andric     //    ___________--    Reload on exit.
121217a519f9SDimitry Andric     //
121317a519f9SDimitry Andric     selectIntv(IntvOut);
121417a519f9SDimitry Andric     SlotIndex Idx = enterIntvAtEnd(*MBB);
121517a519f9SDimitry Andric     assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
121617a519f9SDimitry Andric     (void)Idx;
121717a519f9SDimitry Andric     return;
121817a519f9SDimitry Andric   }
121917a519f9SDimitry Andric 
122017a519f9SDimitry Andric   if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
122117a519f9SDimitry Andric     DEBUG(dbgs() << ", straight through.\n");
122217a519f9SDimitry Andric     //
122317a519f9SDimitry Andric     //    |-----------|    Live through.
122417a519f9SDimitry Andric     //    -------------    Straight through, same intv, no interference.
122517a519f9SDimitry Andric     //
122617a519f9SDimitry Andric     selectIntv(IntvOut);
122717a519f9SDimitry Andric     useIntv(Start, Stop);
122817a519f9SDimitry Andric     return;
122917a519f9SDimitry Andric   }
123017a519f9SDimitry Andric 
123117a519f9SDimitry Andric   // We cannot legally insert splits after LSP.
123217a519f9SDimitry Andric   SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
12336122f3e6SDimitry Andric   assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
123417a519f9SDimitry Andric 
123517a519f9SDimitry Andric   if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
123617a519f9SDimitry Andric                   LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
123717a519f9SDimitry Andric     DEBUG(dbgs() << ", switch avoiding interference.\n");
123817a519f9SDimitry Andric     //
123917a519f9SDimitry Andric     //    >>>>     <<<<    Non-overlapping EnterAfter/LeaveBefore interference.
124017a519f9SDimitry Andric     //    |-----------|    Live through.
124117a519f9SDimitry Andric     //    ------=======    Switch intervals between interference.
124217a519f9SDimitry Andric     //
124317a519f9SDimitry Andric     selectIntv(IntvOut);
12446122f3e6SDimitry Andric     SlotIndex Idx;
12456122f3e6SDimitry Andric     if (LeaveBefore && LeaveBefore < LSP) {
12466122f3e6SDimitry Andric       Idx = enterIntvBefore(LeaveBefore);
124717a519f9SDimitry Andric       useIntv(Idx, Stop);
12486122f3e6SDimitry Andric     } else {
12496122f3e6SDimitry Andric       Idx = enterIntvAtEnd(*MBB);
12506122f3e6SDimitry Andric     }
125117a519f9SDimitry Andric     selectIntv(IntvIn);
125217a519f9SDimitry Andric     useIntv(Start, Idx);
125317a519f9SDimitry Andric     assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
125417a519f9SDimitry Andric     assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
125517a519f9SDimitry Andric     return;
125617a519f9SDimitry Andric   }
125717a519f9SDimitry Andric 
125817a519f9SDimitry Andric   DEBUG(dbgs() << ", create local intv for interference.\n");
125917a519f9SDimitry Andric   //
126017a519f9SDimitry Andric   //    >>><><><><<<<    Overlapping EnterAfter/LeaveBefore interference.
126117a519f9SDimitry Andric   //    |-----------|    Live through.
126217a519f9SDimitry Andric   //    ==---------==    Switch intervals before/after interference.
126317a519f9SDimitry Andric   //
126417a519f9SDimitry Andric   assert(LeaveBefore <= EnterAfter && "Missed case");
126517a519f9SDimitry Andric 
126617a519f9SDimitry Andric   selectIntv(IntvOut);
126717a519f9SDimitry Andric   SlotIndex Idx = enterIntvAfter(EnterAfter);
126817a519f9SDimitry Andric   useIntv(Idx, Stop);
126917a519f9SDimitry Andric   assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
127017a519f9SDimitry Andric 
127117a519f9SDimitry Andric   selectIntv(IntvIn);
127217a519f9SDimitry Andric   Idx = leaveIntvBefore(LeaveBefore);
127317a519f9SDimitry Andric   useIntv(Start, Idx);
127417a519f9SDimitry Andric   assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
127517a519f9SDimitry Andric }
127617a519f9SDimitry Andric 
127717a519f9SDimitry Andric 
127817a519f9SDimitry Andric void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
127917a519f9SDimitry Andric                                   unsigned IntvIn, SlotIndex LeaveBefore) {
128017a519f9SDimitry Andric   SlotIndex Start, Stop;
128117a519f9SDimitry Andric   tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
128217a519f9SDimitry Andric 
128317a519f9SDimitry Andric   DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
12846122f3e6SDimitry Andric                << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
128517a519f9SDimitry Andric                << ", reg-in " << IntvIn << ", leave before " << LeaveBefore
128617a519f9SDimitry Andric                << (BI.LiveOut ? ", stack-out" : ", killed in block"));
128717a519f9SDimitry Andric 
128817a519f9SDimitry Andric   assert(IntvIn && "Must have register in");
128917a519f9SDimitry Andric   assert(BI.LiveIn && "Must be live-in");
129017a519f9SDimitry Andric   assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
129117a519f9SDimitry Andric 
12926122f3e6SDimitry Andric   if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
129317a519f9SDimitry Andric     DEBUG(dbgs() << " before interference.\n");
129417a519f9SDimitry Andric     //
129517a519f9SDimitry Andric     //               <<<    Interference after kill.
129617a519f9SDimitry Andric     //     |---o---x   |    Killed in block.
129717a519f9SDimitry Andric     //     =========        Use IntvIn everywhere.
129817a519f9SDimitry Andric     //
129917a519f9SDimitry Andric     selectIntv(IntvIn);
13006122f3e6SDimitry Andric     useIntv(Start, BI.LastInstr);
130117a519f9SDimitry Andric     return;
130217a519f9SDimitry Andric   }
130317a519f9SDimitry Andric 
130417a519f9SDimitry Andric   SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
130517a519f9SDimitry Andric 
13066122f3e6SDimitry Andric   if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
130717a519f9SDimitry Andric     //
130817a519f9SDimitry Andric     //               <<<    Possible interference after last use.
130917a519f9SDimitry Andric     //     |---o---o---|    Live-out on stack.
131017a519f9SDimitry Andric     //     =========____    Leave IntvIn after last use.
131117a519f9SDimitry Andric     //
131217a519f9SDimitry Andric     //                 <    Interference after last use.
131317a519f9SDimitry Andric     //     |---o---o--o|    Live-out on stack, late last use.
131417a519f9SDimitry Andric     //     ============     Copy to stack after LSP, overlap IntvIn.
131517a519f9SDimitry Andric     //            \_____    Stack interval is live-out.
131617a519f9SDimitry Andric     //
13176122f3e6SDimitry Andric     if (BI.LastInstr < LSP) {
131817a519f9SDimitry Andric       DEBUG(dbgs() << ", spill after last use before interference.\n");
131917a519f9SDimitry Andric       selectIntv(IntvIn);
13206122f3e6SDimitry Andric       SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
132117a519f9SDimitry Andric       useIntv(Start, Idx);
132217a519f9SDimitry Andric       assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
132317a519f9SDimitry Andric     } else {
132417a519f9SDimitry Andric       DEBUG(dbgs() << ", spill before last split point.\n");
132517a519f9SDimitry Andric       selectIntv(IntvIn);
132617a519f9SDimitry Andric       SlotIndex Idx = leaveIntvBefore(LSP);
13276122f3e6SDimitry Andric       overlapIntv(Idx, BI.LastInstr);
132817a519f9SDimitry Andric       useIntv(Start, Idx);
132917a519f9SDimitry Andric       assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
133017a519f9SDimitry Andric     }
133117a519f9SDimitry Andric     return;
133217a519f9SDimitry Andric   }
133317a519f9SDimitry Andric 
133417a519f9SDimitry Andric   // The interference is overlapping somewhere we wanted to use IntvIn. That
133517a519f9SDimitry Andric   // means we need to create a local interval that can be allocated a
133617a519f9SDimitry Andric   // different register.
133717a519f9SDimitry Andric   unsigned LocalIntv = openIntv();
133817a519f9SDimitry Andric   (void)LocalIntv;
133917a519f9SDimitry Andric   DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
134017a519f9SDimitry Andric 
13416122f3e6SDimitry Andric   if (!BI.LiveOut || BI.LastInstr < LSP) {
134217a519f9SDimitry Andric     //
134317a519f9SDimitry Andric     //           <<<<<<<    Interference overlapping uses.
134417a519f9SDimitry Andric     //     |---o---o---|    Live-out on stack.
134517a519f9SDimitry Andric     //     =====----____    Leave IntvIn before interference, then spill.
134617a519f9SDimitry Andric     //
13476122f3e6SDimitry Andric     SlotIndex To = leaveIntvAfter(BI.LastInstr);
134817a519f9SDimitry Andric     SlotIndex From = enterIntvBefore(LeaveBefore);
134917a519f9SDimitry Andric     useIntv(From, To);
135017a519f9SDimitry Andric     selectIntv(IntvIn);
135117a519f9SDimitry Andric     useIntv(Start, From);
135217a519f9SDimitry Andric     assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
135317a519f9SDimitry Andric     return;
135417a519f9SDimitry Andric   }
135517a519f9SDimitry Andric 
135617a519f9SDimitry Andric   //           <<<<<<<    Interference overlapping uses.
135717a519f9SDimitry Andric   //     |---o---o--o|    Live-out on stack, late last use.
135817a519f9SDimitry Andric   //     =====-------     Copy to stack before LSP, overlap LocalIntv.
135917a519f9SDimitry Andric   //            \_____    Stack interval is live-out.
136017a519f9SDimitry Andric   //
136117a519f9SDimitry Andric   SlotIndex To = leaveIntvBefore(LSP);
13626122f3e6SDimitry Andric   overlapIntv(To, BI.LastInstr);
136317a519f9SDimitry Andric   SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
136417a519f9SDimitry Andric   useIntv(From, To);
136517a519f9SDimitry Andric   selectIntv(IntvIn);
136617a519f9SDimitry Andric   useIntv(Start, From);
136717a519f9SDimitry Andric   assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
136817a519f9SDimitry Andric }
136917a519f9SDimitry Andric 
137017a519f9SDimitry Andric void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
137117a519f9SDimitry Andric                                    unsigned IntvOut, SlotIndex EnterAfter) {
137217a519f9SDimitry Andric   SlotIndex Start, Stop;
137317a519f9SDimitry Andric   tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
137417a519f9SDimitry Andric 
137517a519f9SDimitry Andric   DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
13766122f3e6SDimitry Andric                << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
137717a519f9SDimitry Andric                << ", reg-out " << IntvOut << ", enter after " << EnterAfter
137817a519f9SDimitry Andric                << (BI.LiveIn ? ", stack-in" : ", defined in block"));
137917a519f9SDimitry Andric 
138017a519f9SDimitry Andric   SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
138117a519f9SDimitry Andric 
138217a519f9SDimitry Andric   assert(IntvOut && "Must have register out");
138317a519f9SDimitry Andric   assert(BI.LiveOut && "Must be live-out");
138417a519f9SDimitry Andric   assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
138517a519f9SDimitry Andric 
13866122f3e6SDimitry Andric   if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
138717a519f9SDimitry Andric     DEBUG(dbgs() << " after interference.\n");
138817a519f9SDimitry Andric     //
138917a519f9SDimitry Andric     //    >>>>             Interference before def.
139017a519f9SDimitry Andric     //    |   o---o---|    Defined in block.
139117a519f9SDimitry Andric     //        =========    Use IntvOut everywhere.
139217a519f9SDimitry Andric     //
139317a519f9SDimitry Andric     selectIntv(IntvOut);
13946122f3e6SDimitry Andric     useIntv(BI.FirstInstr, Stop);
139517a519f9SDimitry Andric     return;
139617a519f9SDimitry Andric   }
139717a519f9SDimitry Andric 
13986122f3e6SDimitry Andric   if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
139917a519f9SDimitry Andric     DEBUG(dbgs() << ", reload after interference.\n");
140017a519f9SDimitry Andric     //
140117a519f9SDimitry Andric     //    >>>>             Interference before def.
140217a519f9SDimitry Andric     //    |---o---o---|    Live-through, stack-in.
140317a519f9SDimitry Andric     //    ____=========    Enter IntvOut before first use.
140417a519f9SDimitry Andric     //
140517a519f9SDimitry Andric     selectIntv(IntvOut);
14066122f3e6SDimitry Andric     SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
140717a519f9SDimitry Andric     useIntv(Idx, Stop);
140817a519f9SDimitry Andric     assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
140917a519f9SDimitry Andric     return;
141017a519f9SDimitry Andric   }
141117a519f9SDimitry Andric 
141217a519f9SDimitry Andric   // The interference is overlapping somewhere we wanted to use IntvOut. That
141317a519f9SDimitry Andric   // means we need to create a local interval that can be allocated a
141417a519f9SDimitry Andric   // different register.
141517a519f9SDimitry Andric   DEBUG(dbgs() << ", interference overlaps uses.\n");
141617a519f9SDimitry Andric   //
141717a519f9SDimitry Andric   //    >>>>>>>          Interference overlapping uses.
141817a519f9SDimitry Andric   //    |---o---o---|    Live-through, stack-in.
141917a519f9SDimitry Andric   //    ____---======    Create local interval for interference range.
142017a519f9SDimitry Andric   //
142117a519f9SDimitry Andric   selectIntv(IntvOut);
142217a519f9SDimitry Andric   SlotIndex Idx = enterIntvAfter(EnterAfter);
142317a519f9SDimitry Andric   useIntv(Idx, Stop);
142417a519f9SDimitry Andric   assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
142517a519f9SDimitry Andric 
142617a519f9SDimitry Andric   openIntv();
14276122f3e6SDimitry Andric   SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
142817a519f9SDimitry Andric   useIntv(From, Idx);
142917a519f9SDimitry Andric }
1430