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 
15e580952dSDimitry Andric #include "SplitKit.h"
163b0f4066SDimitry Andric #include "llvm/ADT/Statistic.h"
17e580952dSDimitry Andric #include "llvm/CodeGen/LiveIntervalAnalysis.h"
18dff0c46cSDimitry Andric #include "llvm/CodeGen/LiveRangeEdit.h"
192754fe60SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
20e580952dSDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
216122f3e6SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h"
22e580952dSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
23139f7f9bSDimitry Andric #include "llvm/CodeGen/VirtRegMap.h"
24e580952dSDimitry Andric #include "llvm/Support/Debug.h"
25e580952dSDimitry Andric #include "llvm/Support/raw_ostream.h"
26e580952dSDimitry Andric #include "llvm/Target/TargetInstrInfo.h"
27e580952dSDimitry Andric #include "llvm/Target/TargetMachine.h"
28e580952dSDimitry Andric 
29e580952dSDimitry Andric using namespace llvm;
30e580952dSDimitry Andric 
3191bc56edSDimitry Andric #define DEBUG_TYPE "regalloc"
3291bc56edSDimitry Andric 
333b0f4066SDimitry Andric STATISTIC(NumFinished, "Number of splits finished");
343b0f4066SDimitry Andric STATISTIC(NumSimple,   "Number of splits that were simple");
35bd5abe19SDimitry Andric STATISTIC(NumCopies,   "Number of copies inserted for splitting");
36bd5abe19SDimitry Andric STATISTIC(NumRemats,   "Number of rematerialized defs for splitting");
37bd5abe19SDimitry Andric STATISTIC(NumRepairs,  "Number of invalid live ranges repaired");
38e580952dSDimitry Andric 
39e580952dSDimitry Andric //===----------------------------------------------------------------------===//
40e580952dSDimitry Andric //                                 Split Analysis
41e580952dSDimitry Andric //===----------------------------------------------------------------------===//
42e580952dSDimitry Andric 
432754fe60SDimitry Andric SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm,
44e580952dSDimitry Andric                              const LiveIntervals &lis,
45e580952dSDimitry Andric                              const MachineLoopInfo &mli)
462754fe60SDimitry Andric   : MF(vrm.getMachineFunction()),
472754fe60SDimitry Andric     VRM(vrm),
482754fe60SDimitry Andric     LIS(lis),
492754fe60SDimitry Andric     Loops(mli),
502754fe60SDimitry Andric     TII(*MF.getTarget().getInstrInfo()),
5191bc56edSDimitry Andric     CurLI(nullptr),
523b0f4066SDimitry Andric     LastSplitPoint(MF.getNumBlockIDs()) {}
53e580952dSDimitry Andric 
54e580952dSDimitry Andric void SplitAnalysis::clear() {
552754fe60SDimitry Andric   UseSlots.clear();
563b0f4066SDimitry Andric   UseBlocks.clear();
573b0f4066SDimitry Andric   ThroughBlocks.clear();
5891bc56edSDimitry Andric   CurLI = nullptr;
59bd5abe19SDimitry Andric   DidRepairRange = false;
60e580952dSDimitry Andric }
61e580952dSDimitry Andric 
623b0f4066SDimitry Andric SlotIndex SplitAnalysis::computeLastSplitPoint(unsigned Num) {
633b0f4066SDimitry Andric   const MachineBasicBlock *MBB = MF.getBlockNumbered(Num);
643b0f4066SDimitry Andric   const MachineBasicBlock *LPad = MBB->getLandingPadSuccessor();
653b0f4066SDimitry Andric   std::pair<SlotIndex, SlotIndex> &LSP = LastSplitPoint[Num];
66dff0c46cSDimitry Andric   SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB);
673b0f4066SDimitry Andric 
683b0f4066SDimitry Andric   // Compute split points on the first call. The pair is independent of the
693b0f4066SDimitry Andric   // current live interval.
703b0f4066SDimitry Andric   if (!LSP.first.isValid()) {
713b0f4066SDimitry Andric     MachineBasicBlock::const_iterator FirstTerm = MBB->getFirstTerminator();
723b0f4066SDimitry Andric     if (FirstTerm == MBB->end())
73dff0c46cSDimitry Andric       LSP.first = MBBEnd;
743b0f4066SDimitry Andric     else
753b0f4066SDimitry Andric       LSP.first = LIS.getInstructionIndex(FirstTerm);
763b0f4066SDimitry Andric 
773b0f4066SDimitry Andric     // If there is a landing pad successor, also find the call instruction.
783b0f4066SDimitry Andric     if (!LPad)
793b0f4066SDimitry Andric       return LSP.first;
803b0f4066SDimitry Andric     // There may not be a call instruction (?) in which case we ignore LPad.
813b0f4066SDimitry Andric     LSP.second = LSP.first;
8217a519f9SDimitry Andric     for (MachineBasicBlock::const_iterator I = MBB->end(), E = MBB->begin();
8317a519f9SDimitry Andric          I != E;) {
8417a519f9SDimitry Andric       --I;
85dff0c46cSDimitry Andric       if (I->isCall()) {
863b0f4066SDimitry Andric         LSP.second = LIS.getInstructionIndex(I);
873b0f4066SDimitry Andric         break;
883b0f4066SDimitry Andric       }
893b0f4066SDimitry Andric     }
9017a519f9SDimitry Andric   }
913b0f4066SDimitry Andric 
923b0f4066SDimitry Andric   // If CurLI is live into a landing pad successor, move the last split point
933b0f4066SDimitry Andric   // back to the call that may throw.
94dff0c46cSDimitry Andric   if (!LPad || !LSP.second || !LIS.isLiveInToMBB(*CurLI, LPad))
953b0f4066SDimitry Andric     return LSP.first;
96dff0c46cSDimitry Andric 
97dff0c46cSDimitry Andric   // Find the value leaving MBB.
98dff0c46cSDimitry Andric   const VNInfo *VNI = CurLI->getVNInfoBefore(MBBEnd);
99dff0c46cSDimitry Andric   if (!VNI)
100dff0c46cSDimitry Andric     return LSP.first;
101dff0c46cSDimitry Andric 
102dff0c46cSDimitry Andric   // If the value leaving MBB was defined after the call in MBB, it can't
103dff0c46cSDimitry Andric   // really be live-in to the landing pad.  This can happen if the landing pad
104dff0c46cSDimitry Andric   // has a PHI, and this register is undef on the exceptional edge.
105dff0c46cSDimitry Andric   // <rdar://problem/10664933>
106dff0c46cSDimitry Andric   if (!SlotIndex::isEarlierInstr(VNI->def, LSP.second) && VNI->def < MBBEnd)
107dff0c46cSDimitry Andric     return LSP.first;
108dff0c46cSDimitry Andric 
109dff0c46cSDimitry Andric   // Value is properly live-in to the landing pad.
110dff0c46cSDimitry Andric   // Only allow splits before the call.
111dff0c46cSDimitry Andric   return LSP.second;
112dff0c46cSDimitry Andric }
113dff0c46cSDimitry Andric 
114dff0c46cSDimitry Andric MachineBasicBlock::iterator
115dff0c46cSDimitry Andric SplitAnalysis::getLastSplitPointIter(MachineBasicBlock *MBB) {
116dff0c46cSDimitry Andric   SlotIndex LSP = getLastSplitPoint(MBB->getNumber());
117dff0c46cSDimitry Andric   if (LSP == LIS.getMBBEndIdx(MBB))
118dff0c46cSDimitry Andric     return MBB->end();
119dff0c46cSDimitry Andric   return LIS.getInstructionFromIndex(LSP);
120e580952dSDimitry Andric }
121e580952dSDimitry Andric 
1222754fe60SDimitry Andric /// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
123e580952dSDimitry Andric void SplitAnalysis::analyzeUses() {
1243b0f4066SDimitry Andric   assert(UseSlots.empty() && "Call clear first");
1253b0f4066SDimitry Andric 
1263b0f4066SDimitry Andric   // First get all the defs from the interval values. This provides the correct
1273b0f4066SDimitry Andric   // slots for early clobbers.
1283b0f4066SDimitry Andric   for (LiveInterval::const_vni_iterator I = CurLI->vni_begin(),
1293b0f4066SDimitry Andric        E = CurLI->vni_end(); I != E; ++I)
1303b0f4066SDimitry Andric     if (!(*I)->isPHIDef() && !(*I)->isUnused())
1313b0f4066SDimitry Andric       UseSlots.push_back((*I)->def);
1323b0f4066SDimitry Andric 
1333b0f4066SDimitry Andric   // Get use slots form the use-def chain.
1342754fe60SDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
13591bc56edSDimitry Andric   for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg))
13691bc56edSDimitry Andric     if (!MO.isUndef())
13791bc56edSDimitry Andric       UseSlots.push_back(LIS.getInstructionIndex(MO.getParent()).getRegSlot());
1383b0f4066SDimitry Andric 
1392754fe60SDimitry Andric   array_pod_sort(UseSlots.begin(), UseSlots.end());
1403b0f4066SDimitry Andric 
1413b0f4066SDimitry Andric   // Remove duplicates, keeping the smaller slot for each instruction.
1423b0f4066SDimitry Andric   // That is what we want for early clobbers.
1433b0f4066SDimitry Andric   UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
1443b0f4066SDimitry Andric                              SlotIndex::isSameInstr),
1453b0f4066SDimitry Andric                  UseSlots.end());
1463b0f4066SDimitry Andric 
1473b0f4066SDimitry Andric   // Compute per-live block info.
1483b0f4066SDimitry Andric   if (!calcLiveBlockInfo()) {
1493b0f4066SDimitry Andric     // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
15017a519f9SDimitry Andric     // I am looking at you, RegisterCoalescer!
151bd5abe19SDimitry Andric     DidRepairRange = true;
152bd5abe19SDimitry Andric     ++NumRepairs;
1533b0f4066SDimitry Andric     DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
1543b0f4066SDimitry Andric     const_cast<LiveIntervals&>(LIS)
1553b0f4066SDimitry Andric       .shrinkToUses(const_cast<LiveInterval*>(CurLI));
1563b0f4066SDimitry Andric     UseBlocks.clear();
1573b0f4066SDimitry Andric     ThroughBlocks.clear();
1583b0f4066SDimitry Andric     bool fixed = calcLiveBlockInfo();
1593b0f4066SDimitry Andric     (void)fixed;
1603b0f4066SDimitry Andric     assert(fixed && "Couldn't fix broken live interval");
1613b0f4066SDimitry Andric   }
1623b0f4066SDimitry Andric 
1633b0f4066SDimitry Andric   DEBUG(dbgs() << "Analyze counted "
1643b0f4066SDimitry Andric                << UseSlots.size() << " instrs in "
1653b0f4066SDimitry Andric                << UseBlocks.size() << " blocks, through "
1663b0f4066SDimitry Andric                << NumThroughBlocks << " blocks.\n");
167e580952dSDimitry Andric }
168e580952dSDimitry Andric 
1692754fe60SDimitry Andric /// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
1702754fe60SDimitry Andric /// where CurLI is live.
1713b0f4066SDimitry Andric bool SplitAnalysis::calcLiveBlockInfo() {
1723b0f4066SDimitry Andric   ThroughBlocks.resize(MF.getNumBlockIDs());
173bd5abe19SDimitry Andric   NumThroughBlocks = NumGapBlocks = 0;
1742754fe60SDimitry Andric   if (CurLI->empty())
1753b0f4066SDimitry Andric     return true;
176e580952dSDimitry Andric 
1772754fe60SDimitry Andric   LiveInterval::const_iterator LVI = CurLI->begin();
1782754fe60SDimitry Andric   LiveInterval::const_iterator LVE = CurLI->end();
179e580952dSDimitry Andric 
1802754fe60SDimitry Andric   SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
1812754fe60SDimitry Andric   UseI = UseSlots.begin();
1822754fe60SDimitry Andric   UseE = UseSlots.end();
1832754fe60SDimitry Andric 
1842754fe60SDimitry Andric   // Loop over basic blocks where CurLI is live.
1852754fe60SDimitry Andric   MachineFunction::iterator MFI = LIS.getMBBFromIndex(LVI->start);
1862754fe60SDimitry Andric   for (;;) {
1872754fe60SDimitry Andric     BlockInfo BI;
1882754fe60SDimitry Andric     BI.MBB = MFI;
1892754fe60SDimitry Andric     SlotIndex Start, Stop;
19091bc56edSDimitry Andric     std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
1912754fe60SDimitry Andric 
192bd5abe19SDimitry Andric     // If the block contains no uses, the range must be live through. At one
19317a519f9SDimitry Andric     // point, RegisterCoalescer could create dangling ranges that ended
194bd5abe19SDimitry Andric     // mid-block.
195bd5abe19SDimitry Andric     if (UseI == UseE || *UseI >= Stop) {
196bd5abe19SDimitry Andric       ++NumThroughBlocks;
197bd5abe19SDimitry Andric       ThroughBlocks.set(BI.MBB->getNumber());
198bd5abe19SDimitry Andric       // The range shouldn't end mid-block if there are no uses. This shouldn't
199bd5abe19SDimitry Andric       // happen.
200bd5abe19SDimitry Andric       if (LVI->end < Stop)
201bd5abe19SDimitry Andric         return false;
202bd5abe19SDimitry Andric     } else {
203bd5abe19SDimitry Andric       // This block has uses. Find the first and last uses in the block.
2046122f3e6SDimitry Andric       BI.FirstInstr = *UseI;
2056122f3e6SDimitry Andric       assert(BI.FirstInstr >= Start);
2062754fe60SDimitry Andric       do ++UseI;
2072754fe60SDimitry Andric       while (UseI != UseE && *UseI < Stop);
2086122f3e6SDimitry Andric       BI.LastInstr = UseI[-1];
2096122f3e6SDimitry Andric       assert(BI.LastInstr < Stop);
210bd5abe19SDimitry Andric 
211bd5abe19SDimitry Andric       // LVI is the first live segment overlapping MBB.
212bd5abe19SDimitry Andric       BI.LiveIn = LVI->start <= Start;
213e580952dSDimitry Andric 
2146122f3e6SDimitry Andric       // When not live in, the first use should be a def.
2156122f3e6SDimitry Andric       if (!BI.LiveIn) {
216f785676fSDimitry Andric         assert(LVI->start == LVI->valno->def && "Dangling Segment start");
2176122f3e6SDimitry Andric         assert(LVI->start == BI.FirstInstr && "First instr should be a def");
2186122f3e6SDimitry Andric         BI.FirstDef = BI.FirstInstr;
2196122f3e6SDimitry Andric       }
2206122f3e6SDimitry Andric 
2212754fe60SDimitry Andric       // Look for gaps in the live range.
2222754fe60SDimitry Andric       BI.LiveOut = true;
2232754fe60SDimitry Andric       while (LVI->end < Stop) {
2242754fe60SDimitry Andric         SlotIndex LastStop = LVI->end;
2252754fe60SDimitry Andric         if (++LVI == LVE || LVI->start >= Stop) {
2262754fe60SDimitry Andric           BI.LiveOut = false;
2276122f3e6SDimitry Andric           BI.LastInstr = LastStop;
228e580952dSDimitry Andric           break;
229e580952dSDimitry Andric         }
2306122f3e6SDimitry Andric 
2312754fe60SDimitry Andric         if (LastStop < LVI->start) {
232bd5abe19SDimitry Andric           // There is a gap in the live range. Create duplicate entries for the
233bd5abe19SDimitry Andric           // live-in snippet and the live-out snippet.
234bd5abe19SDimitry Andric           ++NumGapBlocks;
235bd5abe19SDimitry Andric 
236bd5abe19SDimitry Andric           // Push the Live-in part.
237bd5abe19SDimitry Andric           BI.LiveOut = false;
238bd5abe19SDimitry Andric           UseBlocks.push_back(BI);
2396122f3e6SDimitry Andric           UseBlocks.back().LastInstr = LastStop;
240bd5abe19SDimitry Andric 
241bd5abe19SDimitry Andric           // Set up BI for the live-out part.
242bd5abe19SDimitry Andric           BI.LiveIn = false;
243bd5abe19SDimitry Andric           BI.LiveOut = true;
2446122f3e6SDimitry Andric           BI.FirstInstr = BI.FirstDef = LVI->start;
245e580952dSDimitry Andric         }
246e580952dSDimitry Andric 
247f785676fSDimitry Andric         // A Segment that starts in the middle of the block must be a def.
248f785676fSDimitry Andric         assert(LVI->start == LVI->valno->def && "Dangling Segment start");
2496122f3e6SDimitry Andric         if (!BI.FirstDef)
2506122f3e6SDimitry Andric           BI.FirstDef = LVI->start;
2516122f3e6SDimitry Andric       }
2526122f3e6SDimitry Andric 
2533b0f4066SDimitry Andric       UseBlocks.push_back(BI);
254e580952dSDimitry Andric 
2552754fe60SDimitry Andric       // LVI is now at LVE or LVI->end >= Stop.
2562754fe60SDimitry Andric       if (LVI == LVE)
2572754fe60SDimitry Andric         break;
258bd5abe19SDimitry Andric     }
2592754fe60SDimitry Andric 
2602754fe60SDimitry Andric     // Live segment ends exactly at Stop. Move to the next segment.
2612754fe60SDimitry Andric     if (LVI->end == Stop && ++LVI == LVE)
2622754fe60SDimitry Andric       break;
2632754fe60SDimitry Andric 
2642754fe60SDimitry Andric     // Pick the next basic block.
2652754fe60SDimitry Andric     if (LVI->start < Stop)
2662754fe60SDimitry Andric       ++MFI;
2672754fe60SDimitry Andric     else
2682754fe60SDimitry Andric       MFI = LIS.getMBBFromIndex(LVI->start);
2692754fe60SDimitry Andric   }
270bd5abe19SDimitry Andric 
271bd5abe19SDimitry Andric   assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
2723b0f4066SDimitry Andric   return true;
2733b0f4066SDimitry Andric }
2743b0f4066SDimitry Andric 
2753b0f4066SDimitry Andric unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
2763b0f4066SDimitry Andric   if (cli->empty())
2773b0f4066SDimitry Andric     return 0;
2783b0f4066SDimitry Andric   LiveInterval *li = const_cast<LiveInterval*>(cli);
2793b0f4066SDimitry Andric   LiveInterval::iterator LVI = li->begin();
2803b0f4066SDimitry Andric   LiveInterval::iterator LVE = li->end();
2813b0f4066SDimitry Andric   unsigned Count = 0;
2823b0f4066SDimitry Andric 
2833b0f4066SDimitry Andric   // Loop over basic blocks where li is live.
2843b0f4066SDimitry Andric   MachineFunction::const_iterator MFI = LIS.getMBBFromIndex(LVI->start);
2853b0f4066SDimitry Andric   SlotIndex Stop = LIS.getMBBEndIdx(MFI);
2863b0f4066SDimitry Andric   for (;;) {
2873b0f4066SDimitry Andric     ++Count;
2883b0f4066SDimitry Andric     LVI = li->advanceTo(LVI, Stop);
2893b0f4066SDimitry Andric     if (LVI == LVE)
2903b0f4066SDimitry Andric       return Count;
2913b0f4066SDimitry Andric     do {
2923b0f4066SDimitry Andric       ++MFI;
2933b0f4066SDimitry Andric       Stop = LIS.getMBBEndIdx(MFI);
2943b0f4066SDimitry Andric     } while (Stop <= LVI->start);
2953b0f4066SDimitry Andric   }
296e580952dSDimitry Andric }
297e580952dSDimitry Andric 
298dd6029ffSDimitry Andric bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
299dd6029ffSDimitry Andric   unsigned OrigReg = VRM.getOriginal(CurLI->reg);
300dd6029ffSDimitry Andric   const LiveInterval &Orig = LIS.getInterval(OrigReg);
301dd6029ffSDimitry Andric   assert(!Orig.empty() && "Splitting empty interval?");
302dd6029ffSDimitry Andric   LiveInterval::const_iterator I = Orig.find(Idx);
303dd6029ffSDimitry Andric 
304dd6029ffSDimitry Andric   // Range containing Idx should begin at Idx.
305dd6029ffSDimitry Andric   if (I != Orig.end() && I->start <= Idx)
306dd6029ffSDimitry Andric     return I->start == Idx;
307dd6029ffSDimitry Andric 
308dd6029ffSDimitry Andric   // Range does not contain Idx, previous must end at Idx.
309dd6029ffSDimitry Andric   return I != Orig.begin() && (--I)->end == Idx;
310dd6029ffSDimitry Andric }
311dd6029ffSDimitry Andric 
312e580952dSDimitry Andric void SplitAnalysis::analyze(const LiveInterval *li) {
313e580952dSDimitry Andric   clear();
3142754fe60SDimitry Andric   CurLI = li;
315e580952dSDimitry Andric   analyzeUses();
316e580952dSDimitry Andric }
317e580952dSDimitry Andric 
318e580952dSDimitry Andric 
319e580952dSDimitry Andric //===----------------------------------------------------------------------===//
3203b0f4066SDimitry Andric //                               Split Editor
321e580952dSDimitry Andric //===----------------------------------------------------------------------===//
322e580952dSDimitry Andric 
3233b0f4066SDimitry Andric /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
3243b0f4066SDimitry Andric SplitEditor::SplitEditor(SplitAnalysis &sa,
3253b0f4066SDimitry Andric                          LiveIntervals &lis,
3263b0f4066SDimitry Andric                          VirtRegMap &vrm,
327f785676fSDimitry Andric                          MachineDominatorTree &mdt,
328f785676fSDimitry Andric                          MachineBlockFrequencyInfo &mbfi)
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()),
334f785676fSDimitry Andric     MBFI(mbfi),
33591bc56edSDimitry Andric     Edit(nullptr),
3363b0f4066SDimitry Andric     OpenIdx(0),
3376122f3e6SDimitry Andric     SpillMode(SM_Partition),
3383b0f4066SDimitry Andric     RegAssign(Allocator)
3393b0f4066SDimitry Andric {}
340e580952dSDimitry Andric 
3416122f3e6SDimitry Andric void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
3426122f3e6SDimitry Andric   Edit = &LRE;
3436122f3e6SDimitry Andric   SpillMode = SM;
3443b0f4066SDimitry Andric   OpenIdx = 0;
3453b0f4066SDimitry Andric   RegAssign.clear();
3462754fe60SDimitry Andric   Values.clear();
3473b0f4066SDimitry Andric 
3486122f3e6SDimitry Andric   // Reset the LiveRangeCalc instances needed for this spill mode.
3497ae0e2c9SDimitry Andric   LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
3507ae0e2c9SDimitry Andric                   &LIS.getVNInfoAllocator());
3516122f3e6SDimitry Andric   if (SpillMode)
3527ae0e2c9SDimitry Andric     LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
3537ae0e2c9SDimitry Andric                     &LIS.getVNInfoAllocator());
3543b0f4066SDimitry Andric 
3553b0f4066SDimitry Andric   // We don't need an AliasAnalysis since we will only be performing
3563b0f4066SDimitry Andric   // cheap-as-a-copy remats anyway.
35791bc56edSDimitry Andric   Edit->anyRematerializable(nullptr);
3582754fe60SDimitry Andric }
3592754fe60SDimitry Andric 
3603861d79fSDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3613b0f4066SDimitry Andric void SplitEditor::dump() const {
3623b0f4066SDimitry Andric   if (RegAssign.empty()) {
3633b0f4066SDimitry Andric     dbgs() << " empty\n";
3643b0f4066SDimitry Andric     return;
3652754fe60SDimitry Andric   }
3662754fe60SDimitry Andric 
3673b0f4066SDimitry Andric   for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
3683b0f4066SDimitry Andric     dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
3693b0f4066SDimitry Andric   dbgs() << '\n';
3703b0f4066SDimitry Andric }
3713861d79fSDimitry Andric #endif
3723b0f4066SDimitry Andric 
3733b0f4066SDimitry Andric VNInfo *SplitEditor::defValue(unsigned RegIdx,
3743b0f4066SDimitry Andric                               const VNInfo *ParentVNI,
3753b0f4066SDimitry Andric                               SlotIndex Idx) {
376e580952dSDimitry Andric   assert(ParentVNI && "Mapping  NULL value");
377e580952dSDimitry Andric   assert(Idx.isValid() && "Invalid SlotIndex");
3783b0f4066SDimitry Andric   assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
379f785676fSDimitry Andric   LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
3802754fe60SDimitry Andric 
3812754fe60SDimitry Andric   // Create a new value.
382dff0c46cSDimitry Andric   VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
3832754fe60SDimitry Andric 
384e580952dSDimitry Andric   // Use insert for lookup, so we can add missing values with a second lookup.
385e580952dSDimitry Andric   std::pair<ValueMap::iterator, bool> InsP =
3866122f3e6SDimitry Andric     Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id),
3876122f3e6SDimitry Andric                                  ValueForcePair(VNI, false)));
3882754fe60SDimitry Andric 
3893b0f4066SDimitry Andric   // This was the first time (RegIdx, ParentVNI) was mapped.
3903b0f4066SDimitry Andric   // Keep it as a simple def without any liveness.
3913b0f4066SDimitry Andric   if (InsP.second)
3923b0f4066SDimitry Andric     return VNI;
3933b0f4066SDimitry Andric 
3943b0f4066SDimitry Andric   // If the previous value was a simple mapping, add liveness for it now.
3956122f3e6SDimitry Andric   if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
3963b0f4066SDimitry Andric     SlotIndex Def = OldVNI->def;
397f785676fSDimitry Andric     LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), OldVNI));
3986122f3e6SDimitry Andric     // No longer a simple mapping.  Switch to a complex, non-forced mapping.
3996122f3e6SDimitry Andric     InsP.first->second = ValueForcePair();
4003b0f4066SDimitry Andric   }
4013b0f4066SDimitry Andric 
4023b0f4066SDimitry Andric   // This is a complex mapping, add liveness for VNI
4033b0f4066SDimitry Andric   SlotIndex Def = VNI->def;
404f785676fSDimitry Andric   LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
4052754fe60SDimitry Andric 
4062754fe60SDimitry Andric   return VNI;
4072754fe60SDimitry Andric }
4082754fe60SDimitry Andric 
4096122f3e6SDimitry Andric void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) {
4102754fe60SDimitry Andric   assert(ParentVNI && "Mapping  NULL value");
4116122f3e6SDimitry Andric   ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)];
4126122f3e6SDimitry Andric   VNInfo *VNI = VFP.getPointer();
4133b0f4066SDimitry Andric 
4146122f3e6SDimitry Andric   // ParentVNI was either unmapped or already complex mapped. Either way, just
4156122f3e6SDimitry Andric   // set the force bit.
4166122f3e6SDimitry Andric   if (!VNI) {
4176122f3e6SDimitry Andric     VFP.setInt(true);
4183b0f4066SDimitry Andric     return;
4196122f3e6SDimitry Andric   }
4203b0f4066SDimitry Andric 
4213b0f4066SDimitry Andric   // This was previously a single mapping. Make sure the old def is represented
4223b0f4066SDimitry Andric   // by a trivial live range.
4233b0f4066SDimitry Andric   SlotIndex Def = VNI->def;
424f785676fSDimitry Andric   LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
425f785676fSDimitry Andric   LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
4266122f3e6SDimitry Andric   // Mark as complex mapped, forced.
42791bc56edSDimitry Andric   VFP = ValueForcePair(nullptr, true);
428e580952dSDimitry Andric }
429e580952dSDimitry Andric 
4302754fe60SDimitry Andric VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
4312754fe60SDimitry Andric                                    VNInfo *ParentVNI,
4322754fe60SDimitry Andric                                    SlotIndex UseIdx,
433e580952dSDimitry Andric                                    MachineBasicBlock &MBB,
434e580952dSDimitry Andric                                    MachineBasicBlock::iterator I) {
43591bc56edSDimitry Andric   MachineInstr *CopyMI = nullptr;
4362754fe60SDimitry Andric   SlotIndex Def;
437f785676fSDimitry Andric   LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
4383b0f4066SDimitry Andric 
4393b0f4066SDimitry Andric   // We may be trying to avoid interference that ends at a deleted instruction,
4403b0f4066SDimitry Andric   // so always begin RegIdx 0 early and all others late.
4413b0f4066SDimitry Andric   bool Late = RegIdx != 0;
4422754fe60SDimitry Andric 
4432754fe60SDimitry Andric   // Attempt cheap-as-a-copy rematerialization.
4442754fe60SDimitry Andric   LiveRangeEdit::Remat RM(ParentVNI);
445dff0c46cSDimitry Andric   if (Edit->canRematerializeAt(RM, UseIdx, true)) {
446dff0c46cSDimitry Andric     Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, TRI, Late);
447bd5abe19SDimitry Andric     ++NumRemats;
4482754fe60SDimitry Andric   } else {
4492754fe60SDimitry Andric     // Can't remat, just insert a copy from parent.
4502754fe60SDimitry Andric     CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
4513b0f4066SDimitry Andric                .addReg(Edit->getReg());
4523b0f4066SDimitry Andric     Def = LIS.getSlotIndexes()->insertMachineInstrInMaps(CopyMI, Late)
453dff0c46cSDimitry Andric             .getRegSlot();
454bd5abe19SDimitry Andric     ++NumCopies;
4552754fe60SDimitry Andric   }
4562754fe60SDimitry Andric 
4572754fe60SDimitry Andric   // Define the value in Reg.
458dff0c46cSDimitry Andric   return defValue(RegIdx, ParentVNI, Def);
459e580952dSDimitry Andric }
460e580952dSDimitry Andric 
461e580952dSDimitry Andric /// Create a new virtual register and live interval.
4623b0f4066SDimitry Andric unsigned SplitEditor::openIntv() {
4632754fe60SDimitry Andric   // Create the complement as index 0.
4643b0f4066SDimitry Andric   if (Edit->empty())
465f785676fSDimitry Andric     Edit->createEmptyInterval();
466e580952dSDimitry Andric 
4672754fe60SDimitry Andric   // Create the open interval.
4683b0f4066SDimitry Andric   OpenIdx = Edit->size();
469f785676fSDimitry Andric   Edit->createEmptyInterval();
4703b0f4066SDimitry Andric   return OpenIdx;
4713b0f4066SDimitry Andric }
4723b0f4066SDimitry Andric 
4733b0f4066SDimitry Andric void SplitEditor::selectIntv(unsigned Idx) {
4743b0f4066SDimitry Andric   assert(Idx != 0 && "Cannot select the complement interval");
4753b0f4066SDimitry Andric   assert(Idx < Edit->size() && "Can only select previously opened interval");
47617a519f9SDimitry Andric   DEBUG(dbgs() << "    selectIntv " << OpenIdx << " -> " << Idx << '\n');
4773b0f4066SDimitry Andric   OpenIdx = Idx;
4782754fe60SDimitry Andric }
479e580952dSDimitry Andric 
4802754fe60SDimitry Andric SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
4812754fe60SDimitry Andric   assert(OpenIdx && "openIntv not called before enterIntvBefore");
4822754fe60SDimitry Andric   DEBUG(dbgs() << "    enterIntvBefore " << Idx);
4832754fe60SDimitry Andric   Idx = Idx.getBaseIndex();
4843b0f4066SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
4852754fe60SDimitry Andric   if (!ParentVNI) {
4862754fe60SDimitry Andric     DEBUG(dbgs() << ": not live\n");
4872754fe60SDimitry Andric     return Idx;
4882754fe60SDimitry Andric   }
4892754fe60SDimitry Andric   DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
4902754fe60SDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
491e580952dSDimitry Andric   assert(MI && "enterIntvBefore called with invalid index");
492e580952dSDimitry Andric 
4932754fe60SDimitry Andric   VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
4942754fe60SDimitry Andric   return VNI->def;
495e580952dSDimitry Andric }
496e580952dSDimitry Andric 
49717a519f9SDimitry Andric SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
49817a519f9SDimitry Andric   assert(OpenIdx && "openIntv not called before enterIntvAfter");
49917a519f9SDimitry Andric   DEBUG(dbgs() << "    enterIntvAfter " << Idx);
50017a519f9SDimitry Andric   Idx = Idx.getBoundaryIndex();
50117a519f9SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
50217a519f9SDimitry Andric   if (!ParentVNI) {
50317a519f9SDimitry Andric     DEBUG(dbgs() << ": not live\n");
50417a519f9SDimitry Andric     return Idx;
50517a519f9SDimitry Andric   }
50617a519f9SDimitry Andric   DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
50717a519f9SDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
50817a519f9SDimitry Andric   assert(MI && "enterIntvAfter called with invalid index");
50917a519f9SDimitry Andric 
51017a519f9SDimitry Andric   VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
51191bc56edSDimitry Andric                               std::next(MachineBasicBlock::iterator(MI)));
51217a519f9SDimitry Andric   return VNI->def;
51317a519f9SDimitry Andric }
51417a519f9SDimitry Andric 
5152754fe60SDimitry Andric SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
5162754fe60SDimitry Andric   assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
5172754fe60SDimitry Andric   SlotIndex End = LIS.getMBBEndIdx(&MBB);
5182754fe60SDimitry Andric   SlotIndex Last = End.getPrevSlot();
5192754fe60SDimitry Andric   DEBUG(dbgs() << "    enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
5203b0f4066SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
5212754fe60SDimitry Andric   if (!ParentVNI) {
5222754fe60SDimitry Andric     DEBUG(dbgs() << ": not live\n");
5232754fe60SDimitry Andric     return End;
5242754fe60SDimitry Andric   }
5252754fe60SDimitry Andric   DEBUG(dbgs() << ": valno " << ParentVNI->id);
5262754fe60SDimitry Andric   VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
527dff0c46cSDimitry Andric                               SA.getLastSplitPointIter(&MBB));
5282754fe60SDimitry Andric   RegAssign.insert(VNI->def, End, OpenIdx);
5292754fe60SDimitry Andric   DEBUG(dump());
5302754fe60SDimitry Andric   return VNI->def;
531e580952dSDimitry Andric }
532e580952dSDimitry Andric 
5332754fe60SDimitry Andric /// useIntv - indicate that all instructions in MBB should use OpenLI.
534e580952dSDimitry Andric void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
5352754fe60SDimitry Andric   useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
536e580952dSDimitry Andric }
537e580952dSDimitry Andric 
538e580952dSDimitry Andric void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
5392754fe60SDimitry Andric   assert(OpenIdx && "openIntv not called before useIntv");
5402754fe60SDimitry Andric   DEBUG(dbgs() << "    useIntv [" << Start << ';' << End << "):");
5412754fe60SDimitry Andric   RegAssign.insert(Start, End, OpenIdx);
5422754fe60SDimitry Andric   DEBUG(dump());
543e580952dSDimitry Andric }
544e580952dSDimitry Andric 
5452754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
5462754fe60SDimitry Andric   assert(OpenIdx && "openIntv not called before leaveIntvAfter");
5472754fe60SDimitry Andric   DEBUG(dbgs() << "    leaveIntvAfter " << Idx);
5482754fe60SDimitry Andric 
5492754fe60SDimitry Andric   // The interval must be live beyond the instruction at Idx.
5506122f3e6SDimitry Andric   SlotIndex Boundary = Idx.getBoundaryIndex();
5516122f3e6SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
5522754fe60SDimitry Andric   if (!ParentVNI) {
5532754fe60SDimitry Andric     DEBUG(dbgs() << ": not live\n");
5546122f3e6SDimitry Andric     return Boundary.getNextSlot();
5552754fe60SDimitry Andric   }
5562754fe60SDimitry Andric   DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
5576122f3e6SDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
5582754fe60SDimitry Andric   assert(MI && "No instruction at index");
5596122f3e6SDimitry Andric 
5606122f3e6SDimitry Andric   // In spill mode, make live ranges as short as possible by inserting the copy
5616122f3e6SDimitry Andric   // before MI.  This is only possible if that instruction doesn't redefine the
5626122f3e6SDimitry Andric   // value.  The inserted COPY is not a kill, and we don't need to recompute
5636122f3e6SDimitry Andric   // the source live range.  The spiller also won't try to hoist this copy.
5646122f3e6SDimitry Andric   if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
5656122f3e6SDimitry Andric       MI->readsVirtualRegister(Edit->getReg())) {
5666122f3e6SDimitry Andric     forceRecompute(0, ParentVNI);
5676122f3e6SDimitry Andric     defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
5686122f3e6SDimitry Andric     return Idx;
5696122f3e6SDimitry Andric   }
5706122f3e6SDimitry Andric 
5716122f3e6SDimitry Andric   VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
57291bc56edSDimitry Andric                               std::next(MachineBasicBlock::iterator(MI)));
5732754fe60SDimitry Andric   return VNI->def;
574e580952dSDimitry Andric }
575e580952dSDimitry Andric 
5762754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
5772754fe60SDimitry Andric   assert(OpenIdx && "openIntv not called before leaveIntvBefore");
5782754fe60SDimitry Andric   DEBUG(dbgs() << "    leaveIntvBefore " << Idx);
579e580952dSDimitry Andric 
5802754fe60SDimitry Andric   // The interval must be live into the instruction at Idx.
5816122f3e6SDimitry Andric   Idx = Idx.getBaseIndex();
5823b0f4066SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
5832754fe60SDimitry Andric   if (!ParentVNI) {
5842754fe60SDimitry Andric     DEBUG(dbgs() << ": not live\n");
5852754fe60SDimitry Andric     return Idx.getNextSlot();
5862754fe60SDimitry Andric   }
5872754fe60SDimitry Andric   DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
5882754fe60SDimitry Andric 
5892754fe60SDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
5902754fe60SDimitry Andric   assert(MI && "No instruction at index");
5912754fe60SDimitry Andric   VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
5922754fe60SDimitry Andric   return VNI->def;
593e580952dSDimitry Andric }
594e580952dSDimitry Andric 
5952754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
5962754fe60SDimitry Andric   assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
5972754fe60SDimitry Andric   SlotIndex Start = LIS.getMBBStartIdx(&MBB);
5982754fe60SDimitry Andric   DEBUG(dbgs() << "    leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
5992754fe60SDimitry Andric 
6003b0f4066SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
6012754fe60SDimitry Andric   if (!ParentVNI) {
6022754fe60SDimitry Andric     DEBUG(dbgs() << ": not live\n");
6032754fe60SDimitry Andric     return Start;
604e580952dSDimitry Andric   }
605e580952dSDimitry Andric 
6062754fe60SDimitry Andric   VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
6072754fe60SDimitry Andric                               MBB.SkipPHIsAndLabels(MBB.begin()));
6082754fe60SDimitry Andric   RegAssign.insert(Start, VNI->def, OpenIdx);
6092754fe60SDimitry Andric   DEBUG(dump());
6102754fe60SDimitry Andric   return VNI->def;
611e580952dSDimitry Andric }
612e580952dSDimitry Andric 
6132754fe60SDimitry Andric void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
6142754fe60SDimitry Andric   assert(OpenIdx && "openIntv not called before overlapIntv");
6153b0f4066SDimitry Andric   const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
616dff0c46cSDimitry Andric   assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
6172754fe60SDimitry Andric          "Parent changes value in extended range");
6182754fe60SDimitry Andric   assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
6192754fe60SDimitry Andric          "Range cannot span basic blocks");
620e580952dSDimitry Andric 
6216122f3e6SDimitry Andric   // The complement interval will be extended as needed by LRCalc.extend().
6223b0f4066SDimitry Andric   if (ParentVNI)
6236122f3e6SDimitry Andric     forceRecompute(0, ParentVNI);
6242754fe60SDimitry Andric   DEBUG(dbgs() << "    overlapIntv [" << Start << ';' << End << "):");
6252754fe60SDimitry Andric   RegAssign.insert(Start, End, OpenIdx);
6262754fe60SDimitry Andric   DEBUG(dump());
627e580952dSDimitry Andric }
628e580952dSDimitry Andric 
6296122f3e6SDimitry Andric //===----------------------------------------------------------------------===//
6306122f3e6SDimitry Andric //                                  Spill modes
6316122f3e6SDimitry Andric //===----------------------------------------------------------------------===//
6326122f3e6SDimitry Andric 
6336122f3e6SDimitry Andric void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
634f785676fSDimitry Andric   LiveInterval *LI = &LIS.getInterval(Edit->get(0));
6356122f3e6SDimitry Andric   DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
6366122f3e6SDimitry Andric   RegAssignMap::iterator AssignI;
6376122f3e6SDimitry Andric   AssignI.setMap(RegAssign);
6386122f3e6SDimitry Andric 
6396122f3e6SDimitry Andric   for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
6406122f3e6SDimitry Andric     VNInfo *VNI = Copies[i];
6416122f3e6SDimitry Andric     SlotIndex Def = VNI->def;
6426122f3e6SDimitry Andric     MachineInstr *MI = LIS.getInstructionFromIndex(Def);
6436122f3e6SDimitry Andric     assert(MI && "No instruction for back-copy");
6446122f3e6SDimitry Andric 
6456122f3e6SDimitry Andric     MachineBasicBlock *MBB = MI->getParent();
6466122f3e6SDimitry Andric     MachineBasicBlock::iterator MBBI(MI);
6476122f3e6SDimitry Andric     bool AtBegin;
6486122f3e6SDimitry Andric     do AtBegin = MBBI == MBB->begin();
6496122f3e6SDimitry Andric     while (!AtBegin && (--MBBI)->isDebugValue());
6506122f3e6SDimitry Andric 
6516122f3e6SDimitry Andric     DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
6526122f3e6SDimitry Andric     LI->removeValNo(VNI);
6536122f3e6SDimitry Andric     LIS.RemoveMachineInstrFromMaps(MI);
6546122f3e6SDimitry Andric     MI->eraseFromParent();
6556122f3e6SDimitry Andric 
6566122f3e6SDimitry Andric     // Adjust RegAssign if a register assignment is killed at VNI->def.  We
6576122f3e6SDimitry Andric     // want to avoid calculating the live range of the source register if
6586122f3e6SDimitry Andric     // possible.
6597ae0e2c9SDimitry Andric     AssignI.find(Def.getPrevSlot());
6606122f3e6SDimitry Andric     if (!AssignI.valid() || AssignI.start() >= Def)
6616122f3e6SDimitry Andric       continue;
6626122f3e6SDimitry Andric     // If MI doesn't kill the assigned register, just leave it.
6636122f3e6SDimitry Andric     if (AssignI.stop() != Def)
6646122f3e6SDimitry Andric       continue;
6656122f3e6SDimitry Andric     unsigned RegIdx = AssignI.value();
6666122f3e6SDimitry Andric     if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) {
6676122f3e6SDimitry Andric       DEBUG(dbgs() << "  cannot find simple kill of RegIdx " << RegIdx << '\n');
6686122f3e6SDimitry Andric       forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def));
6696122f3e6SDimitry Andric     } else {
670dff0c46cSDimitry Andric       SlotIndex Kill = LIS.getInstructionIndex(MBBI).getRegSlot();
6716122f3e6SDimitry Andric       DEBUG(dbgs() << "  move kill to " << Kill << '\t' << *MBBI);
6726122f3e6SDimitry Andric       AssignI.setStop(Kill);
6736122f3e6SDimitry Andric     }
6746122f3e6SDimitry Andric   }
6756122f3e6SDimitry Andric }
6766122f3e6SDimitry Andric 
6776122f3e6SDimitry Andric MachineBasicBlock*
6786122f3e6SDimitry Andric SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
6796122f3e6SDimitry Andric                                   MachineBasicBlock *DefMBB) {
6806122f3e6SDimitry Andric   if (MBB == DefMBB)
6816122f3e6SDimitry Andric     return MBB;
6826122f3e6SDimitry Andric   assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
6836122f3e6SDimitry Andric 
6846122f3e6SDimitry Andric   const MachineLoopInfo &Loops = SA.Loops;
6856122f3e6SDimitry Andric   const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
6866122f3e6SDimitry Andric   MachineDomTreeNode *DefDomNode = MDT[DefMBB];
6876122f3e6SDimitry Andric 
6886122f3e6SDimitry Andric   // Best candidate so far.
6896122f3e6SDimitry Andric   MachineBasicBlock *BestMBB = MBB;
6906122f3e6SDimitry Andric   unsigned BestDepth = UINT_MAX;
6916122f3e6SDimitry Andric 
6926122f3e6SDimitry Andric   for (;;) {
6936122f3e6SDimitry Andric     const MachineLoop *Loop = Loops.getLoopFor(MBB);
6946122f3e6SDimitry Andric 
6956122f3e6SDimitry Andric     // MBB isn't in a loop, it doesn't get any better.  All dominators have a
6966122f3e6SDimitry Andric     // higher frequency by definition.
6976122f3e6SDimitry Andric     if (!Loop) {
6986122f3e6SDimitry Andric       DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
6996122f3e6SDimitry Andric                    << MBB->getNumber() << " at depth 0\n");
7006122f3e6SDimitry Andric       return MBB;
7016122f3e6SDimitry Andric     }
7026122f3e6SDimitry Andric 
7036122f3e6SDimitry Andric     // We'll never be able to exit the DefLoop.
7046122f3e6SDimitry Andric     if (Loop == DefLoop) {
7056122f3e6SDimitry Andric       DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
7066122f3e6SDimitry Andric                    << MBB->getNumber() << " in the same loop\n");
7076122f3e6SDimitry Andric       return MBB;
7086122f3e6SDimitry Andric     }
7096122f3e6SDimitry Andric 
7106122f3e6SDimitry Andric     // Least busy dominator seen so far.
7116122f3e6SDimitry Andric     unsigned Depth = Loop->getLoopDepth();
7126122f3e6SDimitry Andric     if (Depth < BestDepth) {
7136122f3e6SDimitry Andric       BestMBB = MBB;
7146122f3e6SDimitry Andric       BestDepth = Depth;
7156122f3e6SDimitry Andric       DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
7166122f3e6SDimitry Andric                    << MBB->getNumber() << " at depth " << Depth << '\n');
7176122f3e6SDimitry Andric     }
7186122f3e6SDimitry Andric 
7196122f3e6SDimitry Andric     // Leave loop by going to the immediate dominator of the loop header.
7206122f3e6SDimitry Andric     // This is a bigger stride than simply walking up the dominator tree.
7216122f3e6SDimitry Andric     MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
7226122f3e6SDimitry Andric 
7236122f3e6SDimitry Andric     // Too far up the dominator tree?
7246122f3e6SDimitry Andric     if (!IDom || !MDT.dominates(DefDomNode, IDom))
7256122f3e6SDimitry Andric       return BestMBB;
7266122f3e6SDimitry Andric 
7276122f3e6SDimitry Andric     MBB = IDom->getBlock();
7286122f3e6SDimitry Andric   }
7296122f3e6SDimitry Andric }
7306122f3e6SDimitry Andric 
7316122f3e6SDimitry Andric void SplitEditor::hoistCopiesForSize() {
7326122f3e6SDimitry Andric   // Get the complement interval, always RegIdx 0.
733f785676fSDimitry Andric   LiveInterval *LI = &LIS.getInterval(Edit->get(0));
7346122f3e6SDimitry Andric   LiveInterval *Parent = &Edit->getParent();
7356122f3e6SDimitry Andric 
7366122f3e6SDimitry Andric   // Track the nearest common dominator for all back-copies for each ParentVNI,
7376122f3e6SDimitry Andric   // indexed by ParentVNI->id.
7386122f3e6SDimitry Andric   typedef std::pair<MachineBasicBlock*, SlotIndex> DomPair;
7396122f3e6SDimitry Andric   SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
7406122f3e6SDimitry Andric 
7416122f3e6SDimitry Andric   // Find the nearest common dominator for parent values with multiple
7426122f3e6SDimitry Andric   // back-copies.  If a single back-copy dominates, put it in DomPair.second.
7436122f3e6SDimitry Andric   for (LiveInterval::vni_iterator VI = LI->vni_begin(), VE = LI->vni_end();
7446122f3e6SDimitry Andric        VI != VE; ++VI) {
7456122f3e6SDimitry Andric     VNInfo *VNI = *VI;
7467ae0e2c9SDimitry Andric     if (VNI->isUnused())
7477ae0e2c9SDimitry Andric       continue;
7486122f3e6SDimitry Andric     VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
7496122f3e6SDimitry Andric     assert(ParentVNI && "Parent not live at complement def");
7506122f3e6SDimitry Andric 
7516122f3e6SDimitry Andric     // Don't hoist remats.  The complement is probably going to disappear
7526122f3e6SDimitry Andric     // completely anyway.
7536122f3e6SDimitry Andric     if (Edit->didRematerialize(ParentVNI))
7546122f3e6SDimitry Andric       continue;
7556122f3e6SDimitry Andric 
7566122f3e6SDimitry Andric     MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
7576122f3e6SDimitry Andric     DomPair &Dom = NearestDom[ParentVNI->id];
7586122f3e6SDimitry Andric 
7596122f3e6SDimitry Andric     // Keep directly defined parent values.  This is either a PHI or an
7606122f3e6SDimitry Andric     // instruction in the complement range.  All other copies of ParentVNI
7616122f3e6SDimitry Andric     // should be eliminated.
7626122f3e6SDimitry Andric     if (VNI->def == ParentVNI->def) {
7636122f3e6SDimitry Andric       DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
7646122f3e6SDimitry Andric       Dom = DomPair(ValMBB, VNI->def);
7656122f3e6SDimitry Andric       continue;
7666122f3e6SDimitry Andric     }
7676122f3e6SDimitry Andric     // Skip the singly mapped values.  There is nothing to gain from hoisting a
7686122f3e6SDimitry Andric     // single back-copy.
7696122f3e6SDimitry Andric     if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
7706122f3e6SDimitry Andric       DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
7716122f3e6SDimitry Andric       continue;
7726122f3e6SDimitry Andric     }
7736122f3e6SDimitry Andric 
7746122f3e6SDimitry Andric     if (!Dom.first) {
7756122f3e6SDimitry Andric       // First time we see ParentVNI.  VNI dominates itself.
7766122f3e6SDimitry Andric       Dom = DomPair(ValMBB, VNI->def);
7776122f3e6SDimitry Andric     } else if (Dom.first == ValMBB) {
7786122f3e6SDimitry Andric       // Two defs in the same block.  Pick the earlier def.
7796122f3e6SDimitry Andric       if (!Dom.second.isValid() || VNI->def < Dom.second)
7806122f3e6SDimitry Andric         Dom.second = VNI->def;
7816122f3e6SDimitry Andric     } else {
7826122f3e6SDimitry Andric       // Different basic blocks. Check if one dominates.
7836122f3e6SDimitry Andric       MachineBasicBlock *Near =
7846122f3e6SDimitry Andric         MDT.findNearestCommonDominator(Dom.first, ValMBB);
7856122f3e6SDimitry Andric       if (Near == ValMBB)
7866122f3e6SDimitry Andric         // Def ValMBB dominates.
7876122f3e6SDimitry Andric         Dom = DomPair(ValMBB, VNI->def);
7886122f3e6SDimitry Andric       else if (Near != Dom.first)
7896122f3e6SDimitry Andric         // None dominate. Hoist to common dominator, need new def.
7906122f3e6SDimitry Andric         Dom = DomPair(Near, SlotIndex());
7916122f3e6SDimitry Andric     }
7926122f3e6SDimitry Andric 
7936122f3e6SDimitry Andric     DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def
7946122f3e6SDimitry Andric                  << " for parent " << ParentVNI->id << '@' << ParentVNI->def
7956122f3e6SDimitry Andric                  << " hoist to BB#" << Dom.first->getNumber() << ' '
7966122f3e6SDimitry Andric                  << Dom.second << '\n');
7976122f3e6SDimitry Andric   }
7986122f3e6SDimitry Andric 
7996122f3e6SDimitry Andric   // Insert the hoisted copies.
8006122f3e6SDimitry Andric   for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
8016122f3e6SDimitry Andric     DomPair &Dom = NearestDom[i];
8026122f3e6SDimitry Andric     if (!Dom.first || Dom.second.isValid())
8036122f3e6SDimitry Andric       continue;
8046122f3e6SDimitry Andric     // This value needs a hoisted copy inserted at the end of Dom.first.
8056122f3e6SDimitry Andric     VNInfo *ParentVNI = Parent->getValNumInfo(i);
8066122f3e6SDimitry Andric     MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
8076122f3e6SDimitry Andric     // Get a less loopy dominator than Dom.first.
8086122f3e6SDimitry Andric     Dom.first = findShallowDominator(Dom.first, DefMBB);
8096122f3e6SDimitry Andric     SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot();
8106122f3e6SDimitry Andric     Dom.second =
8116122f3e6SDimitry Andric       defFromParent(0, ParentVNI, Last, *Dom.first,
812dff0c46cSDimitry Andric                     SA.getLastSplitPointIter(Dom.first))->def;
8136122f3e6SDimitry Andric   }
8146122f3e6SDimitry Andric 
8156122f3e6SDimitry Andric   // Remove redundant back-copies that are now known to be dominated by another
8166122f3e6SDimitry Andric   // def with the same value.
8176122f3e6SDimitry Andric   SmallVector<VNInfo*, 8> BackCopies;
8186122f3e6SDimitry Andric   for (LiveInterval::vni_iterator VI = LI->vni_begin(), VE = LI->vni_end();
8196122f3e6SDimitry Andric        VI != VE; ++VI) {
8206122f3e6SDimitry Andric     VNInfo *VNI = *VI;
8217ae0e2c9SDimitry Andric     if (VNI->isUnused())
8227ae0e2c9SDimitry Andric       continue;
8236122f3e6SDimitry Andric     VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
8246122f3e6SDimitry Andric     const DomPair &Dom = NearestDom[ParentVNI->id];
8256122f3e6SDimitry Andric     if (!Dom.first || Dom.second == VNI->def)
8266122f3e6SDimitry Andric       continue;
8276122f3e6SDimitry Andric     BackCopies.push_back(VNI);
8286122f3e6SDimitry Andric     forceRecompute(0, ParentVNI);
8296122f3e6SDimitry Andric   }
8306122f3e6SDimitry Andric   removeBackCopies(BackCopies);
8316122f3e6SDimitry Andric }
8326122f3e6SDimitry Andric 
8336122f3e6SDimitry Andric 
8343b0f4066SDimitry Andric /// transferValues - Transfer all possible values to the new live ranges.
8356122f3e6SDimitry Andric /// Values that were rematerialized are left alone, they need LRCalc.extend().
8363b0f4066SDimitry Andric bool SplitEditor::transferValues() {
8373b0f4066SDimitry Andric   bool Skipped = false;
8383b0f4066SDimitry Andric   RegAssignMap::const_iterator AssignI = RegAssign.begin();
8393b0f4066SDimitry Andric   for (LiveInterval::const_iterator ParentI = Edit->getParent().begin(),
8403b0f4066SDimitry Andric          ParentE = Edit->getParent().end(); ParentI != ParentE; ++ParentI) {
8413b0f4066SDimitry Andric     DEBUG(dbgs() << "  blit " << *ParentI << ':');
8423b0f4066SDimitry Andric     VNInfo *ParentVNI = ParentI->valno;
8433b0f4066SDimitry Andric     // RegAssign has holes where RegIdx 0 should be used.
8443b0f4066SDimitry Andric     SlotIndex Start = ParentI->start;
8453b0f4066SDimitry Andric     AssignI.advanceTo(Start);
8463b0f4066SDimitry Andric     do {
8473b0f4066SDimitry Andric       unsigned RegIdx;
8483b0f4066SDimitry Andric       SlotIndex End = ParentI->end;
8493b0f4066SDimitry Andric       if (!AssignI.valid()) {
8503b0f4066SDimitry Andric         RegIdx = 0;
8513b0f4066SDimitry Andric       } else if (AssignI.start() <= Start) {
8523b0f4066SDimitry Andric         RegIdx = AssignI.value();
8533b0f4066SDimitry Andric         if (AssignI.stop() < End) {
8543b0f4066SDimitry Andric           End = AssignI.stop();
8553b0f4066SDimitry Andric           ++AssignI;
8563b0f4066SDimitry Andric         }
8573b0f4066SDimitry Andric       } else {
8583b0f4066SDimitry Andric         RegIdx = 0;
8593b0f4066SDimitry Andric         End = std::min(End, AssignI.start());
860e580952dSDimitry Andric       }
861e580952dSDimitry Andric 
8623b0f4066SDimitry Andric       // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
8633b0f4066SDimitry Andric       DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx);
864f785676fSDimitry Andric       LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
8653b0f4066SDimitry Andric 
8663b0f4066SDimitry Andric       // Check for a simply defined value that can be blitted directly.
8676122f3e6SDimitry Andric       ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
8686122f3e6SDimitry Andric       if (VNInfo *VNI = VFP.getPointer()) {
8693b0f4066SDimitry Andric         DEBUG(dbgs() << ':' << VNI->id);
870f785676fSDimitry Andric         LR.addSegment(LiveInterval::Segment(Start, End, VNI));
8713b0f4066SDimitry Andric         Start = End;
8723b0f4066SDimitry Andric         continue;
8733b0f4066SDimitry Andric       }
8743b0f4066SDimitry Andric 
8756122f3e6SDimitry Andric       // Skip values with forced recomputation.
8766122f3e6SDimitry Andric       if (VFP.getInt()) {
8776122f3e6SDimitry Andric         DEBUG(dbgs() << "(recalc)");
8783b0f4066SDimitry Andric         Skipped = true;
8793b0f4066SDimitry Andric         Start = End;
8803b0f4066SDimitry Andric         continue;
8813b0f4066SDimitry Andric       }
8823b0f4066SDimitry Andric 
8836122f3e6SDimitry Andric       LiveRangeCalc &LRC = getLRCalc(RegIdx);
8843b0f4066SDimitry Andric 
8853b0f4066SDimitry Andric       // This value has multiple defs in RegIdx, but it wasn't rematerialized,
8863b0f4066SDimitry Andric       // so the live range is accurate. Add live-in blocks in [Start;End) to the
8873b0f4066SDimitry Andric       // LiveInBlocks.
8883b0f4066SDimitry Andric       MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
8893b0f4066SDimitry Andric       SlotIndex BlockStart, BlockEnd;
89091bc56edSDimitry Andric       std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(MBB);
8913b0f4066SDimitry Andric 
8923b0f4066SDimitry Andric       // The first block may be live-in, or it may have its own def.
8933b0f4066SDimitry Andric       if (Start != BlockStart) {
894f785676fSDimitry Andric         VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
8953b0f4066SDimitry Andric         assert(VNI && "Missing def for complex mapped value");
8963b0f4066SDimitry Andric         DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber());
8973b0f4066SDimitry Andric         // MBB has its own def. Is it also live-out?
8986122f3e6SDimitry Andric         if (BlockEnd <= End)
8996122f3e6SDimitry Andric           LRC.setLiveOutValue(MBB, VNI);
9006122f3e6SDimitry Andric 
9013b0f4066SDimitry Andric         // Skip to the next block for live-in.
9023b0f4066SDimitry Andric         ++MBB;
9033b0f4066SDimitry Andric         BlockStart = BlockEnd;
9043b0f4066SDimitry Andric       }
9053b0f4066SDimitry Andric 
9063b0f4066SDimitry Andric       // Handle the live-in blocks covered by [Start;End).
9073b0f4066SDimitry Andric       assert(Start <= BlockStart && "Expected live-in block");
9083b0f4066SDimitry Andric       while (BlockStart < End) {
9093b0f4066SDimitry Andric         DEBUG(dbgs() << ">BB#" << MBB->getNumber());
9103b0f4066SDimitry Andric         BlockEnd = LIS.getMBBEndIdx(MBB);
9113b0f4066SDimitry Andric         if (BlockStart == ParentVNI->def) {
9123b0f4066SDimitry Andric           // This block has the def of a parent PHI, so it isn't live-in.
9133b0f4066SDimitry Andric           assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
914f785676fSDimitry Andric           VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
9153b0f4066SDimitry Andric           assert(VNI && "Missing def for complex mapped parent PHI");
9166122f3e6SDimitry Andric           if (End >= BlockEnd)
9176122f3e6SDimitry Andric             LRC.setLiveOutValue(MBB, VNI); // Live-out as well.
9183b0f4066SDimitry Andric         } else {
9196122f3e6SDimitry Andric           // This block needs a live-in value.  The last block covered may not
9206122f3e6SDimitry Andric           // be live-out.
9213b0f4066SDimitry Andric           if (End < BlockEnd)
922f785676fSDimitry Andric             LRC.addLiveInBlock(LR, MDT[MBB], End);
9233b0f4066SDimitry Andric           else {
9246122f3e6SDimitry Andric             // Live-through, and we don't know the value.
925f785676fSDimitry Andric             LRC.addLiveInBlock(LR, MDT[MBB]);
92691bc56edSDimitry Andric             LRC.setLiveOutValue(MBB, nullptr);
9273b0f4066SDimitry Andric           }
9283b0f4066SDimitry Andric         }
9293b0f4066SDimitry Andric         BlockStart = BlockEnd;
9303b0f4066SDimitry Andric         ++MBB;
9313b0f4066SDimitry Andric       }
9323b0f4066SDimitry Andric       Start = End;
9333b0f4066SDimitry Andric     } while (Start != ParentI->end);
9343b0f4066SDimitry Andric     DEBUG(dbgs() << '\n');
9353b0f4066SDimitry Andric   }
9363b0f4066SDimitry Andric 
9377ae0e2c9SDimitry Andric   LRCalc[0].calculateValues();
9386122f3e6SDimitry Andric   if (SpillMode)
9397ae0e2c9SDimitry Andric     LRCalc[1].calculateValues();
9403b0f4066SDimitry Andric 
9413b0f4066SDimitry Andric   return Skipped;
9423b0f4066SDimitry Andric }
9433b0f4066SDimitry Andric 
9443b0f4066SDimitry Andric void SplitEditor::extendPHIKillRanges() {
9453b0f4066SDimitry Andric     // Extend live ranges to be live-out for successor PHI values.
9463b0f4066SDimitry Andric   for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
9473b0f4066SDimitry Andric        E = Edit->getParent().vni_end(); I != E; ++I) {
9483b0f4066SDimitry Andric     const VNInfo *PHIVNI = *I;
9493b0f4066SDimitry Andric     if (PHIVNI->isUnused() || !PHIVNI->isPHIDef())
9503b0f4066SDimitry Andric       continue;
9513b0f4066SDimitry Andric     unsigned RegIdx = RegAssign.lookup(PHIVNI->def);
952f785676fSDimitry Andric     LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
9536122f3e6SDimitry Andric     LiveRangeCalc &LRC = getLRCalc(RegIdx);
9543b0f4066SDimitry Andric     MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
9553b0f4066SDimitry Andric     for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
9563b0f4066SDimitry Andric          PE = MBB->pred_end(); PI != PE; ++PI) {
9576122f3e6SDimitry Andric       SlotIndex End = LIS.getMBBEndIdx(*PI);
9586122f3e6SDimitry Andric       SlotIndex LastUse = End.getPrevSlot();
9593b0f4066SDimitry Andric       // The predecessor may not have a live-out value. That is OK, like an
9603b0f4066SDimitry Andric       // undef PHI operand.
9616122f3e6SDimitry Andric       if (Edit->getParent().liveAt(LastUse)) {
9626122f3e6SDimitry Andric         assert(RegAssign.lookup(LastUse) == RegIdx &&
9633b0f4066SDimitry Andric                "Different register assignment in phi predecessor");
964f785676fSDimitry Andric         LRC.extend(LR, End);
9653b0f4066SDimitry Andric       }
9663b0f4066SDimitry Andric     }
9673b0f4066SDimitry Andric   }
9683b0f4066SDimitry Andric }
9693b0f4066SDimitry Andric 
9703b0f4066SDimitry Andric /// rewriteAssigned - Rewrite all uses of Edit->getReg().
9713b0f4066SDimitry Andric void SplitEditor::rewriteAssigned(bool ExtendRanges) {
9723b0f4066SDimitry Andric   for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
9732754fe60SDimitry Andric        RE = MRI.reg_end(); RI != RE;) {
97491bc56edSDimitry Andric     MachineOperand &MO = *RI;
975e580952dSDimitry Andric     MachineInstr *MI = MO.getParent();
976e580952dSDimitry Andric     ++RI;
9772754fe60SDimitry Andric     // LiveDebugVariables should have handled all DBG_VALUE instructions.
978e580952dSDimitry Andric     if (MI->isDebugValue()) {
979e580952dSDimitry Andric       DEBUG(dbgs() << "Zapping " << *MI);
980e580952dSDimitry Andric       MO.setReg(0);
981e580952dSDimitry Andric       continue;
982e580952dSDimitry Andric     }
9832754fe60SDimitry Andric 
9846122f3e6SDimitry Andric     // <undef> operands don't really read the register, so it doesn't matter
9856122f3e6SDimitry Andric     // which register we choose.  When the use operand is tied to a def, we must
9866122f3e6SDimitry Andric     // use the same register as the def, so just do that always.
9872754fe60SDimitry Andric     SlotIndex Idx = LIS.getInstructionIndex(MI);
9886122f3e6SDimitry Andric     if (MO.isDef() || MO.isUndef())
989dff0c46cSDimitry Andric       Idx = Idx.getRegSlot(MO.isEarlyClobber());
9902754fe60SDimitry Andric 
9912754fe60SDimitry Andric     // Rewrite to the mapped register at Idx.
9922754fe60SDimitry Andric     unsigned RegIdx = RegAssign.lookup(Idx);
993f785676fSDimitry Andric     LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
9946122f3e6SDimitry Andric     MO.setReg(LI->reg);
9952754fe60SDimitry Andric     DEBUG(dbgs() << "  rewr BB#" << MI->getParent()->getNumber() << '\t'
9962754fe60SDimitry Andric                  << Idx << ':' << RegIdx << '\t' << *MI);
9972754fe60SDimitry Andric 
9983b0f4066SDimitry Andric     // Extend liveness to Idx if the instruction reads reg.
9996122f3e6SDimitry Andric     if (!ExtendRanges || MO.isUndef())
10002754fe60SDimitry Andric       continue;
10013b0f4066SDimitry Andric 
10023b0f4066SDimitry Andric     // Skip instructions that don't read Reg.
10033b0f4066SDimitry Andric     if (MO.isDef()) {
10043b0f4066SDimitry Andric       if (!MO.getSubReg() && !MO.isEarlyClobber())
10053b0f4066SDimitry Andric         continue;
10063b0f4066SDimitry Andric       // We may wan't to extend a live range for a partial redef, or for a use
10073b0f4066SDimitry Andric       // tied to an early clobber.
10083b0f4066SDimitry Andric       Idx = Idx.getPrevSlot();
10093b0f4066SDimitry Andric       if (!Edit->getParent().liveAt(Idx))
10103b0f4066SDimitry Andric         continue;
10113b0f4066SDimitry Andric     } else
1012dff0c46cSDimitry Andric       Idx = Idx.getRegSlot(true);
10133b0f4066SDimitry Andric 
1014f785676fSDimitry Andric     getLRCalc(RegIdx).extend(*LI, Idx.getNextSlot());
1015e580952dSDimitry Andric   }
1016e580952dSDimitry Andric }
1017e580952dSDimitry Andric 
10183b0f4066SDimitry Andric void SplitEditor::deleteRematVictims() {
10193b0f4066SDimitry Andric   SmallVector<MachineInstr*, 8> Dead;
10203b0f4066SDimitry Andric   for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
1021f785676fSDimitry Andric     LiveInterval *LI = &LIS.getInterval(*I);
10223b0f4066SDimitry Andric     for (LiveInterval::const_iterator LII = LI->begin(), LIE = LI->end();
10233b0f4066SDimitry Andric            LII != LIE; ++LII) {
1024dff0c46cSDimitry Andric       // Dead defs end at the dead slot.
1025dff0c46cSDimitry Andric       if (LII->end != LII->valno->def.getDeadSlot())
10263b0f4066SDimitry Andric         continue;
10273b0f4066SDimitry Andric       MachineInstr *MI = LIS.getInstructionFromIndex(LII->valno->def);
10283b0f4066SDimitry Andric       assert(MI && "Missing instruction for dead def");
10293b0f4066SDimitry Andric       MI->addRegisterDead(LI->reg, &TRI);
10303b0f4066SDimitry Andric 
10313b0f4066SDimitry Andric       if (!MI->allDefsAreDead())
10323b0f4066SDimitry Andric         continue;
10333b0f4066SDimitry Andric 
10343b0f4066SDimitry Andric       DEBUG(dbgs() << "All defs dead: " << *MI);
10353b0f4066SDimitry Andric       Dead.push_back(MI);
10363b0f4066SDimitry Andric     }
10373b0f4066SDimitry Andric   }
10383b0f4066SDimitry Andric 
10393b0f4066SDimitry Andric   if (Dead.empty())
10403b0f4066SDimitry Andric     return;
10413b0f4066SDimitry Andric 
1042dff0c46cSDimitry Andric   Edit->eliminateDeadDefs(Dead);
10433b0f4066SDimitry Andric }
10443b0f4066SDimitry Andric 
10453b0f4066SDimitry Andric void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
10463b0f4066SDimitry Andric   ++NumFinished;
10472754fe60SDimitry Andric 
10482754fe60SDimitry Andric   // At this point, the live intervals in Edit contain VNInfos corresponding to
10492754fe60SDimitry Andric   // the inserted copies.
10502754fe60SDimitry Andric 
10512754fe60SDimitry Andric   // Add the original defs from the parent interval.
10523b0f4066SDimitry Andric   for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
10533b0f4066SDimitry Andric          E = Edit->getParent().vni_end(); I != E; ++I) {
10542754fe60SDimitry Andric     const VNInfo *ParentVNI = *I;
10552754fe60SDimitry Andric     if (ParentVNI->isUnused())
10562754fe60SDimitry Andric       continue;
10573b0f4066SDimitry Andric     unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
10587ae0e2c9SDimitry Andric     defValue(RegIdx, ParentVNI, ParentVNI->def);
10593b0f4066SDimitry Andric 
10606122f3e6SDimitry Andric     // Force rematted values to be recomputed everywhere.
10613b0f4066SDimitry Andric     // The new live ranges may be truncated.
10623b0f4066SDimitry Andric     if (Edit->didRematerialize(ParentVNI))
10633b0f4066SDimitry Andric       for (unsigned i = 0, e = Edit->size(); i != e; ++i)
10646122f3e6SDimitry Andric         forceRecompute(i, ParentVNI);
10656122f3e6SDimitry Andric   }
10666122f3e6SDimitry Andric 
10676122f3e6SDimitry Andric   // Hoist back-copies to the complement interval when in spill mode.
10686122f3e6SDimitry Andric   switch (SpillMode) {
10696122f3e6SDimitry Andric   case SM_Partition:
10706122f3e6SDimitry Andric     // Leave all back-copies as is.
10716122f3e6SDimitry Andric     break;
10726122f3e6SDimitry Andric   case SM_Size:
10736122f3e6SDimitry Andric     hoistCopiesForSize();
10746122f3e6SDimitry Andric     break;
10756122f3e6SDimitry Andric   case SM_Speed:
10766122f3e6SDimitry Andric     llvm_unreachable("Spill mode 'speed' not implemented yet");
10772754fe60SDimitry Andric   }
10782754fe60SDimitry Andric 
10793b0f4066SDimitry Andric   // Transfer the simply mapped values, check if any are skipped.
10803b0f4066SDimitry Andric   bool Skipped = transferValues();
10813b0f4066SDimitry Andric   if (Skipped)
10823b0f4066SDimitry Andric     extendPHIKillRanges();
10832754fe60SDimitry Andric   else
10843b0f4066SDimitry Andric     ++NumSimple;
10852754fe60SDimitry Andric 
10863b0f4066SDimitry Andric   // Rewrite virtual registers, possibly extending ranges.
10873b0f4066SDimitry Andric   rewriteAssigned(Skipped);
10882754fe60SDimitry Andric 
10893b0f4066SDimitry Andric   // Delete defs that were rematted everywhere.
10903b0f4066SDimitry Andric   if (Skipped)
10913b0f4066SDimitry Andric     deleteRematVictims();
10922754fe60SDimitry Andric 
10932754fe60SDimitry Andric   // Get rid of unused values and set phi-kill flags.
1094f785676fSDimitry Andric   for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I) {
1095f785676fSDimitry Andric     LiveInterval &LI = LIS.getInterval(*I);
1096f785676fSDimitry Andric     LI.RenumberValues();
1097f785676fSDimitry Andric   }
10982754fe60SDimitry Andric 
10993b0f4066SDimitry Andric   // Provide a reverse mapping from original indices to Edit ranges.
11003b0f4066SDimitry Andric   if (LRMap) {
11013b0f4066SDimitry Andric     LRMap->clear();
11023b0f4066SDimitry Andric     for (unsigned i = 0, e = Edit->size(); i != e; ++i)
11033b0f4066SDimitry Andric       LRMap->push_back(i);
11043b0f4066SDimitry Andric   }
11053b0f4066SDimitry Andric 
11062754fe60SDimitry Andric   // Now check if any registers were separated into multiple components.
11072754fe60SDimitry Andric   ConnectedVNInfoEqClasses ConEQ(LIS);
11083b0f4066SDimitry Andric   for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
11092754fe60SDimitry Andric     // Don't use iterators, they are invalidated by create() below.
1110f785676fSDimitry Andric     LiveInterval *li = &LIS.getInterval(Edit->get(i));
11112754fe60SDimitry Andric     unsigned NumComp = ConEQ.Classify(li);
11122754fe60SDimitry Andric     if (NumComp <= 1)
11132754fe60SDimitry Andric       continue;
11142754fe60SDimitry Andric     DEBUG(dbgs() << "  " << NumComp << " components: " << *li << '\n');
11152754fe60SDimitry Andric     SmallVector<LiveInterval*, 8> dups;
11162754fe60SDimitry Andric     dups.push_back(li);
11173b0f4066SDimitry Andric     for (unsigned j = 1; j != NumComp; ++j)
1118f785676fSDimitry Andric       dups.push_back(&Edit->createEmptyInterval());
11193b0f4066SDimitry Andric     ConEQ.Distribute(&dups[0], MRI);
11203b0f4066SDimitry Andric     // The new intervals all map back to i.
11213b0f4066SDimitry Andric     if (LRMap)
11223b0f4066SDimitry Andric       LRMap->resize(Edit->size(), i);
11232754fe60SDimitry Andric   }
11242754fe60SDimitry Andric 
1125e580952dSDimitry Andric   // Calculate spill weight and allocation hints for new intervals.
1126f785676fSDimitry Andric   Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI);
11273b0f4066SDimitry Andric 
11283b0f4066SDimitry Andric   assert(!LRMap || LRMap->size() == Edit->size());
1129e580952dSDimitry Andric }
1130e580952dSDimitry Andric 
1131e580952dSDimitry Andric 
1132e580952dSDimitry Andric //===----------------------------------------------------------------------===//
1133e580952dSDimitry Andric //                            Single Block Splitting
1134e580952dSDimitry Andric //===----------------------------------------------------------------------===//
1135e580952dSDimitry Andric 
11366122f3e6SDimitry Andric bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
11376122f3e6SDimitry Andric                                            bool SingleInstrs) const {
11386122f3e6SDimitry Andric   // Always split for multiple instructions.
11396122f3e6SDimitry Andric   if (!BI.isOneInstr())
11406122f3e6SDimitry Andric     return true;
11416122f3e6SDimitry Andric   // Don't split for single instructions unless explicitly requested.
11426122f3e6SDimitry Andric   if (!SingleInstrs)
11432754fe60SDimitry Andric     return false;
11446122f3e6SDimitry Andric   // Splitting a live-through range always makes progress.
11456122f3e6SDimitry Andric   if (BI.LiveIn && BI.LiveOut)
11466122f3e6SDimitry Andric     return true;
11476122f3e6SDimitry Andric   // No point in isolating a copy. It has no register class constraints.
11486122f3e6SDimitry Andric   if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
11496122f3e6SDimitry Andric     return false;
11506122f3e6SDimitry Andric   // Finally, don't isolate an end point that was created by earlier splits.
11516122f3e6SDimitry Andric   return isOriginalEndpoint(BI.FirstInstr);
1152e580952dSDimitry Andric }
1153e580952dSDimitry Andric 
11543b0f4066SDimitry Andric void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
11553b0f4066SDimitry Andric   openIntv();
11563b0f4066SDimitry Andric   SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber());
11576122f3e6SDimitry Andric   SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
11583b0f4066SDimitry Andric     LastSplitPoint));
11596122f3e6SDimitry Andric   if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
11606122f3e6SDimitry Andric     useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
11613b0f4066SDimitry Andric   } else {
11623b0f4066SDimitry Andric       // The last use is after the last valid split point.
11633b0f4066SDimitry Andric     SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
11643b0f4066SDimitry Andric     useIntv(SegStart, SegStop);
11656122f3e6SDimitry Andric     overlapIntv(SegStop, BI.LastInstr);
11663b0f4066SDimitry Andric   }
11673b0f4066SDimitry Andric }
11683b0f4066SDimitry Andric 
116917a519f9SDimitry Andric 
117017a519f9SDimitry Andric //===----------------------------------------------------------------------===//
117117a519f9SDimitry Andric //                    Global Live Range Splitting Support
117217a519f9SDimitry Andric //===----------------------------------------------------------------------===//
117317a519f9SDimitry Andric 
117417a519f9SDimitry Andric // These methods support a method of global live range splitting that uses a
117517a519f9SDimitry Andric // global algorithm to decide intervals for CFG edges. They will insert split
117617a519f9SDimitry Andric // points and color intervals in basic blocks while avoiding interference.
117717a519f9SDimitry Andric //
117817a519f9SDimitry Andric // Note that splitSingleBlock is also useful for blocks where both CFG edges
117917a519f9SDimitry Andric // are on the stack.
118017a519f9SDimitry Andric 
118117a519f9SDimitry Andric void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
118217a519f9SDimitry Andric                                         unsigned IntvIn, SlotIndex LeaveBefore,
118317a519f9SDimitry Andric                                         unsigned IntvOut, SlotIndex EnterAfter){
118417a519f9SDimitry Andric   SlotIndex Start, Stop;
118591bc56edSDimitry Andric   std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
118617a519f9SDimitry Andric 
118717a519f9SDimitry Andric   DEBUG(dbgs() << "BB#" << MBBNum << " [" << Start << ';' << Stop
118817a519f9SDimitry Andric                << ") intf " << LeaveBefore << '-' << EnterAfter
118917a519f9SDimitry Andric                << ", live-through " << IntvIn << " -> " << IntvOut);
119017a519f9SDimitry Andric 
119117a519f9SDimitry Andric   assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
119217a519f9SDimitry Andric 
11936122f3e6SDimitry Andric   assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
11946122f3e6SDimitry Andric   assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
11956122f3e6SDimitry Andric   assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
11966122f3e6SDimitry Andric 
11976122f3e6SDimitry Andric   MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
11986122f3e6SDimitry Andric 
119917a519f9SDimitry Andric   if (!IntvOut) {
120017a519f9SDimitry Andric     DEBUG(dbgs() << ", spill on entry.\n");
120117a519f9SDimitry Andric     //
120217a519f9SDimitry Andric     //        <<<<<<<<<    Possible LeaveBefore interference.
120317a519f9SDimitry Andric     //    |-----------|    Live through.
120417a519f9SDimitry Andric     //    -____________    Spill on entry.
120517a519f9SDimitry Andric     //
120617a519f9SDimitry Andric     selectIntv(IntvIn);
120717a519f9SDimitry Andric     SlotIndex Idx = leaveIntvAtTop(*MBB);
120817a519f9SDimitry Andric     assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
120917a519f9SDimitry Andric     (void)Idx;
121017a519f9SDimitry Andric     return;
121117a519f9SDimitry Andric   }
121217a519f9SDimitry Andric 
121317a519f9SDimitry Andric   if (!IntvIn) {
121417a519f9SDimitry Andric     DEBUG(dbgs() << ", reload on exit.\n");
121517a519f9SDimitry Andric     //
121617a519f9SDimitry Andric     //    >>>>>>>          Possible EnterAfter interference.
121717a519f9SDimitry Andric     //    |-----------|    Live through.
121817a519f9SDimitry Andric     //    ___________--    Reload on exit.
121917a519f9SDimitry Andric     //
122017a519f9SDimitry Andric     selectIntv(IntvOut);
122117a519f9SDimitry Andric     SlotIndex Idx = enterIntvAtEnd(*MBB);
122217a519f9SDimitry Andric     assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
122317a519f9SDimitry Andric     (void)Idx;
122417a519f9SDimitry Andric     return;
122517a519f9SDimitry Andric   }
122617a519f9SDimitry Andric 
122717a519f9SDimitry Andric   if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
122817a519f9SDimitry Andric     DEBUG(dbgs() << ", straight through.\n");
122917a519f9SDimitry Andric     //
123017a519f9SDimitry Andric     //    |-----------|    Live through.
123117a519f9SDimitry Andric     //    -------------    Straight through, same intv, no interference.
123217a519f9SDimitry Andric     //
123317a519f9SDimitry Andric     selectIntv(IntvOut);
123417a519f9SDimitry Andric     useIntv(Start, Stop);
123517a519f9SDimitry Andric     return;
123617a519f9SDimitry Andric   }
123717a519f9SDimitry Andric 
123817a519f9SDimitry Andric   // We cannot legally insert splits after LSP.
123917a519f9SDimitry Andric   SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
12406122f3e6SDimitry Andric   assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
124117a519f9SDimitry Andric 
124217a519f9SDimitry Andric   if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
124317a519f9SDimitry Andric                   LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
124417a519f9SDimitry Andric     DEBUG(dbgs() << ", switch avoiding interference.\n");
124517a519f9SDimitry Andric     //
124617a519f9SDimitry Andric     //    >>>>     <<<<    Non-overlapping EnterAfter/LeaveBefore interference.
124717a519f9SDimitry Andric     //    |-----------|    Live through.
124817a519f9SDimitry Andric     //    ------=======    Switch intervals between interference.
124917a519f9SDimitry Andric     //
125017a519f9SDimitry Andric     selectIntv(IntvOut);
12516122f3e6SDimitry Andric     SlotIndex Idx;
12526122f3e6SDimitry Andric     if (LeaveBefore && LeaveBefore < LSP) {
12536122f3e6SDimitry Andric       Idx = enterIntvBefore(LeaveBefore);
125417a519f9SDimitry Andric       useIntv(Idx, Stop);
12556122f3e6SDimitry Andric     } else {
12566122f3e6SDimitry Andric       Idx = enterIntvAtEnd(*MBB);
12576122f3e6SDimitry Andric     }
125817a519f9SDimitry Andric     selectIntv(IntvIn);
125917a519f9SDimitry Andric     useIntv(Start, Idx);
126017a519f9SDimitry Andric     assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
126117a519f9SDimitry Andric     assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
126217a519f9SDimitry Andric     return;
126317a519f9SDimitry Andric   }
126417a519f9SDimitry Andric 
126517a519f9SDimitry Andric   DEBUG(dbgs() << ", create local intv for interference.\n");
126617a519f9SDimitry Andric   //
126717a519f9SDimitry Andric   //    >>><><><><<<<    Overlapping EnterAfter/LeaveBefore interference.
126817a519f9SDimitry Andric   //    |-----------|    Live through.
126917a519f9SDimitry Andric   //    ==---------==    Switch intervals before/after interference.
127017a519f9SDimitry Andric   //
127117a519f9SDimitry Andric   assert(LeaveBefore <= EnterAfter && "Missed case");
127217a519f9SDimitry Andric 
127317a519f9SDimitry Andric   selectIntv(IntvOut);
127417a519f9SDimitry Andric   SlotIndex Idx = enterIntvAfter(EnterAfter);
127517a519f9SDimitry Andric   useIntv(Idx, Stop);
127617a519f9SDimitry Andric   assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
127717a519f9SDimitry Andric 
127817a519f9SDimitry Andric   selectIntv(IntvIn);
127917a519f9SDimitry Andric   Idx = leaveIntvBefore(LeaveBefore);
128017a519f9SDimitry Andric   useIntv(Start, Idx);
128117a519f9SDimitry Andric   assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
128217a519f9SDimitry Andric }
128317a519f9SDimitry Andric 
128417a519f9SDimitry Andric 
128517a519f9SDimitry Andric void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
128617a519f9SDimitry Andric                                   unsigned IntvIn, SlotIndex LeaveBefore) {
128717a519f9SDimitry Andric   SlotIndex Start, Stop;
128891bc56edSDimitry Andric   std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
128917a519f9SDimitry Andric 
129017a519f9SDimitry Andric   DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
12916122f3e6SDimitry Andric                << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
129217a519f9SDimitry Andric                << ", reg-in " << IntvIn << ", leave before " << LeaveBefore
129317a519f9SDimitry Andric                << (BI.LiveOut ? ", stack-out" : ", killed in block"));
129417a519f9SDimitry Andric 
129517a519f9SDimitry Andric   assert(IntvIn && "Must have register in");
129617a519f9SDimitry Andric   assert(BI.LiveIn && "Must be live-in");
129717a519f9SDimitry Andric   assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
129817a519f9SDimitry Andric 
12996122f3e6SDimitry Andric   if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
130017a519f9SDimitry Andric     DEBUG(dbgs() << " before interference.\n");
130117a519f9SDimitry Andric     //
130217a519f9SDimitry Andric     //               <<<    Interference after kill.
130317a519f9SDimitry Andric     //     |---o---x   |    Killed in block.
130417a519f9SDimitry Andric     //     =========        Use IntvIn everywhere.
130517a519f9SDimitry Andric     //
130617a519f9SDimitry Andric     selectIntv(IntvIn);
13076122f3e6SDimitry Andric     useIntv(Start, BI.LastInstr);
130817a519f9SDimitry Andric     return;
130917a519f9SDimitry Andric   }
131017a519f9SDimitry Andric 
131117a519f9SDimitry Andric   SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
131217a519f9SDimitry Andric 
13136122f3e6SDimitry Andric   if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
131417a519f9SDimitry Andric     //
131517a519f9SDimitry Andric     //               <<<    Possible interference after last use.
131617a519f9SDimitry Andric     //     |---o---o---|    Live-out on stack.
131717a519f9SDimitry Andric     //     =========____    Leave IntvIn after last use.
131817a519f9SDimitry Andric     //
131917a519f9SDimitry Andric     //                 <    Interference after last use.
132017a519f9SDimitry Andric     //     |---o---o--o|    Live-out on stack, late last use.
132117a519f9SDimitry Andric     //     ============     Copy to stack after LSP, overlap IntvIn.
132217a519f9SDimitry Andric     //            \_____    Stack interval is live-out.
132317a519f9SDimitry Andric     //
13246122f3e6SDimitry Andric     if (BI.LastInstr < LSP) {
132517a519f9SDimitry Andric       DEBUG(dbgs() << ", spill after last use before interference.\n");
132617a519f9SDimitry Andric       selectIntv(IntvIn);
13276122f3e6SDimitry Andric       SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
132817a519f9SDimitry Andric       useIntv(Start, Idx);
132917a519f9SDimitry Andric       assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
133017a519f9SDimitry Andric     } else {
133117a519f9SDimitry Andric       DEBUG(dbgs() << ", spill before last split point.\n");
133217a519f9SDimitry Andric       selectIntv(IntvIn);
133317a519f9SDimitry Andric       SlotIndex Idx = leaveIntvBefore(LSP);
13346122f3e6SDimitry Andric       overlapIntv(Idx, BI.LastInstr);
133517a519f9SDimitry Andric       useIntv(Start, Idx);
133617a519f9SDimitry Andric       assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
133717a519f9SDimitry Andric     }
133817a519f9SDimitry Andric     return;
133917a519f9SDimitry Andric   }
134017a519f9SDimitry Andric 
134117a519f9SDimitry Andric   // The interference is overlapping somewhere we wanted to use IntvIn. That
134217a519f9SDimitry Andric   // means we need to create a local interval that can be allocated a
134317a519f9SDimitry Andric   // different register.
134417a519f9SDimitry Andric   unsigned LocalIntv = openIntv();
134517a519f9SDimitry Andric   (void)LocalIntv;
134617a519f9SDimitry Andric   DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
134717a519f9SDimitry Andric 
13486122f3e6SDimitry Andric   if (!BI.LiveOut || BI.LastInstr < LSP) {
134917a519f9SDimitry Andric     //
135017a519f9SDimitry Andric     //           <<<<<<<    Interference overlapping uses.
135117a519f9SDimitry Andric     //     |---o---o---|    Live-out on stack.
135217a519f9SDimitry Andric     //     =====----____    Leave IntvIn before interference, then spill.
135317a519f9SDimitry Andric     //
13546122f3e6SDimitry Andric     SlotIndex To = leaveIntvAfter(BI.LastInstr);
135517a519f9SDimitry Andric     SlotIndex From = enterIntvBefore(LeaveBefore);
135617a519f9SDimitry Andric     useIntv(From, To);
135717a519f9SDimitry Andric     selectIntv(IntvIn);
135817a519f9SDimitry Andric     useIntv(Start, From);
135917a519f9SDimitry Andric     assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
136017a519f9SDimitry Andric     return;
136117a519f9SDimitry Andric   }
136217a519f9SDimitry Andric 
136317a519f9SDimitry Andric   //           <<<<<<<    Interference overlapping uses.
136417a519f9SDimitry Andric   //     |---o---o--o|    Live-out on stack, late last use.
136517a519f9SDimitry Andric   //     =====-------     Copy to stack before LSP, overlap LocalIntv.
136617a519f9SDimitry Andric   //            \_____    Stack interval is live-out.
136717a519f9SDimitry Andric   //
136817a519f9SDimitry Andric   SlotIndex To = leaveIntvBefore(LSP);
13696122f3e6SDimitry Andric   overlapIntv(To, BI.LastInstr);
137017a519f9SDimitry Andric   SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
137117a519f9SDimitry Andric   useIntv(From, To);
137217a519f9SDimitry Andric   selectIntv(IntvIn);
137317a519f9SDimitry Andric   useIntv(Start, From);
137417a519f9SDimitry Andric   assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
137517a519f9SDimitry Andric }
137617a519f9SDimitry Andric 
137717a519f9SDimitry Andric void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
137817a519f9SDimitry Andric                                    unsigned IntvOut, SlotIndex EnterAfter) {
137917a519f9SDimitry Andric   SlotIndex Start, Stop;
138091bc56edSDimitry Andric   std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
138117a519f9SDimitry Andric 
138217a519f9SDimitry Andric   DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
13836122f3e6SDimitry Andric                << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
138417a519f9SDimitry Andric                << ", reg-out " << IntvOut << ", enter after " << EnterAfter
138517a519f9SDimitry Andric                << (BI.LiveIn ? ", stack-in" : ", defined in block"));
138617a519f9SDimitry Andric 
138717a519f9SDimitry Andric   SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
138817a519f9SDimitry Andric 
138917a519f9SDimitry Andric   assert(IntvOut && "Must have register out");
139017a519f9SDimitry Andric   assert(BI.LiveOut && "Must be live-out");
139117a519f9SDimitry Andric   assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
139217a519f9SDimitry Andric 
13936122f3e6SDimitry Andric   if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
139417a519f9SDimitry Andric     DEBUG(dbgs() << " after interference.\n");
139517a519f9SDimitry Andric     //
139617a519f9SDimitry Andric     //    >>>>             Interference before def.
139717a519f9SDimitry Andric     //    |   o---o---|    Defined in block.
139817a519f9SDimitry Andric     //        =========    Use IntvOut everywhere.
139917a519f9SDimitry Andric     //
140017a519f9SDimitry Andric     selectIntv(IntvOut);
14016122f3e6SDimitry Andric     useIntv(BI.FirstInstr, Stop);
140217a519f9SDimitry Andric     return;
140317a519f9SDimitry Andric   }
140417a519f9SDimitry Andric 
14056122f3e6SDimitry Andric   if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
140617a519f9SDimitry Andric     DEBUG(dbgs() << ", reload after interference.\n");
140717a519f9SDimitry Andric     //
140817a519f9SDimitry Andric     //    >>>>             Interference before def.
140917a519f9SDimitry Andric     //    |---o---o---|    Live-through, stack-in.
141017a519f9SDimitry Andric     //    ____=========    Enter IntvOut before first use.
141117a519f9SDimitry Andric     //
141217a519f9SDimitry Andric     selectIntv(IntvOut);
14136122f3e6SDimitry Andric     SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
141417a519f9SDimitry Andric     useIntv(Idx, Stop);
141517a519f9SDimitry Andric     assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
141617a519f9SDimitry Andric     return;
141717a519f9SDimitry Andric   }
141817a519f9SDimitry Andric 
141917a519f9SDimitry Andric   // The interference is overlapping somewhere we wanted to use IntvOut. That
142017a519f9SDimitry Andric   // means we need to create a local interval that can be allocated a
142117a519f9SDimitry Andric   // different register.
142217a519f9SDimitry Andric   DEBUG(dbgs() << ", interference overlaps uses.\n");
142317a519f9SDimitry Andric   //
142417a519f9SDimitry Andric   //    >>>>>>>          Interference overlapping uses.
142517a519f9SDimitry Andric   //    |---o---o---|    Live-through, stack-in.
142617a519f9SDimitry Andric   //    ____---======    Create local interval for interference range.
142717a519f9SDimitry Andric   //
142817a519f9SDimitry Andric   selectIntv(IntvOut);
142917a519f9SDimitry Andric   SlotIndex Idx = enterIntvAfter(EnterAfter);
143017a519f9SDimitry Andric   useIntv(Idx, Stop);
143117a519f9SDimitry Andric   assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
143217a519f9SDimitry Andric 
143317a519f9SDimitry Andric   openIntv();
14346122f3e6SDimitry Andric   SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
143517a519f9SDimitry Andric   useIntv(From, Idx);
143617a519f9SDimitry Andric }
1437