xref: /src/contrib/llvm-project/llvm/lib/CodeGen/SplitKit.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1044eb2f6SDimitry Andric //===- SplitKit.cpp - Toolkit for splitting live ranges -------------------===//
2d39c594dSDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d39c594dSDimitry Andric //
7d39c594dSDimitry Andric //===----------------------------------------------------------------------===//
8d39c594dSDimitry Andric //
9d39c594dSDimitry Andric // This file contains the SplitAnalysis class as well as mutator functions for
10d39c594dSDimitry Andric // live range splitting.
11d39c594dSDimitry Andric //
12d39c594dSDimitry Andric //===----------------------------------------------------------------------===//
13d39c594dSDimitry Andric 
14d39c594dSDimitry Andric #include "SplitKit.h"
15044eb2f6SDimitry Andric #include "llvm/ADT/STLExtras.h"
166b943ff3SDimitry Andric #include "llvm/ADT/Statistic.h"
17cfca06d7SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
1863faed5bSDimitry Andric #include "llvm/CodeGen/LiveRangeEdit.h"
1901095a5dSDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
20cf099d11SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
21044eb2f6SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
22d39c594dSDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
2330815c53SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h"
24044eb2f6SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
25d39c594dSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
26044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
27044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h"
28044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
29044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
304a16efa3SDimitry Andric #include "llvm/CodeGen/VirtRegMap.h"
31eb11fae6SDimitry Andric #include "llvm/Config/llvm-config.h"
32044eb2f6SDimitry Andric #include "llvm/IR/DebugLoc.h"
33044eb2f6SDimitry Andric #include "llvm/Support/Allocator.h"
34044eb2f6SDimitry Andric #include "llvm/Support/BlockFrequency.h"
35d39c594dSDimitry Andric #include "llvm/Support/Debug.h"
36044eb2f6SDimitry Andric #include "llvm/Support/ErrorHandling.h"
37d39c594dSDimitry Andric #include "llvm/Support/raw_ostream.h"
38044eb2f6SDimitry Andric #include <algorithm>
39044eb2f6SDimitry Andric #include <cassert>
40044eb2f6SDimitry Andric #include <iterator>
41044eb2f6SDimitry Andric #include <limits>
42044eb2f6SDimitry Andric #include <tuple>
43d39c594dSDimitry Andric 
44d39c594dSDimitry Andric using namespace llvm;
45d39c594dSDimitry Andric 
465ca98fd9SDimitry Andric #define DEBUG_TYPE "regalloc"
475ca98fd9SDimitry Andric 
48b1c73532SDimitry Andric static cl::opt<bool>
49b1c73532SDimitry Andric     EnableLoopIVHeuristic("enable-split-loopiv-heuristic",
50b1c73532SDimitry Andric                           cl::desc("Enable loop iv regalloc heuristic"),
51b1c73532SDimitry Andric                           cl::init(true));
52b1c73532SDimitry Andric 
536b943ff3SDimitry Andric STATISTIC(NumFinished, "Number of splits finished");
546b943ff3SDimitry Andric STATISTIC(NumSimple,   "Number of splits that were simple");
5556fe8f14SDimitry Andric STATISTIC(NumCopies,   "Number of copies inserted for splitting");
5656fe8f14SDimitry Andric STATISTIC(NumRemats,   "Number of rematerialized defs for splitting");
57d39c594dSDimitry Andric 
58d39c594dSDimitry Andric //===----------------------------------------------------------------------===//
5901095a5dSDimitry Andric //                     Last Insert Point Analysis
6001095a5dSDimitry Andric //===----------------------------------------------------------------------===//
6101095a5dSDimitry Andric 
InsertPointAnalysis(const LiveIntervals & lis,unsigned BBNum)6201095a5dSDimitry Andric InsertPointAnalysis::InsertPointAnalysis(const LiveIntervals &lis,
6301095a5dSDimitry Andric                                          unsigned BBNum)
6401095a5dSDimitry Andric     : LIS(lis), LastInsertPoint(BBNum) {}
6501095a5dSDimitry Andric 
6601095a5dSDimitry Andric SlotIndex
computeLastInsertPoint(const LiveInterval & CurLI,const MachineBasicBlock & MBB)6701095a5dSDimitry Andric InsertPointAnalysis::computeLastInsertPoint(const LiveInterval &CurLI,
6801095a5dSDimitry Andric                                             const MachineBasicBlock &MBB) {
6901095a5dSDimitry Andric   unsigned Num = MBB.getNumber();
7001095a5dSDimitry Andric   std::pair<SlotIndex, SlotIndex> &LIP = LastInsertPoint[Num];
7101095a5dSDimitry Andric   SlotIndex MBBEnd = LIS.getMBBEndIdx(&MBB);
7201095a5dSDimitry Andric 
73cfca06d7SDimitry Andric   SmallVector<const MachineBasicBlock *, 1> ExceptionalSuccessors;
74cfca06d7SDimitry Andric   bool EHPadSuccessor = false;
75cfca06d7SDimitry Andric   for (const MachineBasicBlock *SMBB : MBB.successors()) {
76cfca06d7SDimitry Andric     if (SMBB->isEHPad()) {
77cfca06d7SDimitry Andric       ExceptionalSuccessors.push_back(SMBB);
78cfca06d7SDimitry Andric       EHPadSuccessor = true;
79cfca06d7SDimitry Andric     } else if (SMBB->isInlineAsmBrIndirectTarget())
80cfca06d7SDimitry Andric       ExceptionalSuccessors.push_back(SMBB);
81cfca06d7SDimitry Andric   }
8201095a5dSDimitry Andric 
8301095a5dSDimitry Andric   // Compute insert points on the first call. The pair is independent of the
8401095a5dSDimitry Andric   // current live interval.
8501095a5dSDimitry Andric   if (!LIP.first.isValid()) {
8601095a5dSDimitry Andric     MachineBasicBlock::const_iterator FirstTerm = MBB.getFirstTerminator();
8701095a5dSDimitry Andric     if (FirstTerm == MBB.end())
8801095a5dSDimitry Andric       LIP.first = MBBEnd;
8901095a5dSDimitry Andric     else
9001095a5dSDimitry Andric       LIP.first = LIS.getInstructionIndex(*FirstTerm);
9101095a5dSDimitry Andric 
92cfca06d7SDimitry Andric     // If there is a landing pad or inlineasm_br successor, also find the
93cfca06d7SDimitry Andric     // instruction. If there is no such instruction, we don't need to do
94cfca06d7SDimitry Andric     // anything special.  We assume there cannot be multiple instructions that
95cfca06d7SDimitry Andric     // are Calls with EHPad successors or INLINEASM_BR in a block. Further, we
96cfca06d7SDimitry Andric     // assume that if there are any, they will be after any other call
97cfca06d7SDimitry Andric     // instructions in the block.
98cfca06d7SDimitry Andric     if (ExceptionalSuccessors.empty())
9901095a5dSDimitry Andric       return LIP.first;
100344a3780SDimitry Andric     for (const MachineInstr &MI : llvm::reverse(MBB)) {
101344a3780SDimitry Andric       if ((EHPadSuccessor && MI.isCall()) ||
102344a3780SDimitry Andric           MI.getOpcode() == TargetOpcode::INLINEASM_BR) {
103344a3780SDimitry Andric         LIP.second = LIS.getInstructionIndex(MI);
10401095a5dSDimitry Andric         break;
10501095a5dSDimitry Andric       }
10601095a5dSDimitry Andric     }
10701095a5dSDimitry Andric   }
10801095a5dSDimitry Andric 
10901095a5dSDimitry Andric   // If CurLI is live into a landing pad successor, move the last insert point
11001095a5dSDimitry Andric   // back to the call that may throw.
11101095a5dSDimitry Andric   if (!LIP.second)
11201095a5dSDimitry Andric     return LIP.first;
11301095a5dSDimitry Andric 
114cfca06d7SDimitry Andric   if (none_of(ExceptionalSuccessors, [&](const MachineBasicBlock *EHPad) {
11501095a5dSDimitry Andric         return LIS.isLiveInToMBB(CurLI, EHPad);
11601095a5dSDimitry Andric       }))
11701095a5dSDimitry Andric     return LIP.first;
11801095a5dSDimitry Andric 
11901095a5dSDimitry Andric   // Find the value leaving MBB.
12001095a5dSDimitry Andric   const VNInfo *VNI = CurLI.getVNInfoBefore(MBBEnd);
12101095a5dSDimitry Andric   if (!VNI)
12201095a5dSDimitry Andric     return LIP.first;
12301095a5dSDimitry Andric 
124344a3780SDimitry Andric   // The def of statepoint instruction is a gc relocation and it should be alive
125344a3780SDimitry Andric   // in landing pad. So we cannot split interval after statepoint instruction.
126344a3780SDimitry Andric   if (SlotIndex::isSameInstr(VNI->def, LIP.second))
127344a3780SDimitry Andric     if (auto *I = LIS.getInstructionFromIndex(LIP.second))
128344a3780SDimitry Andric       if (I->getOpcode() == TargetOpcode::STATEPOINT)
129344a3780SDimitry Andric         return LIP.second;
130344a3780SDimitry Andric 
13101095a5dSDimitry Andric   // If the value leaving MBB was defined after the call in MBB, it can't
13201095a5dSDimitry Andric   // really be live-in to the landing pad.  This can happen if the landing pad
13301095a5dSDimitry Andric   // has a PHI, and this register is undef on the exceptional edge.
13401095a5dSDimitry Andric   if (!SlotIndex::isEarlierInstr(VNI->def, LIP.second) && VNI->def < MBBEnd)
13501095a5dSDimitry Andric     return LIP.first;
13601095a5dSDimitry Andric 
13701095a5dSDimitry Andric   // Value is properly live-in to the landing pad.
13801095a5dSDimitry Andric   // Only allow inserts before the call.
13901095a5dSDimitry Andric   return LIP.second;
14001095a5dSDimitry Andric }
14101095a5dSDimitry Andric 
14201095a5dSDimitry Andric MachineBasicBlock::iterator
getLastInsertPointIter(const LiveInterval & CurLI,MachineBasicBlock & MBB)14301095a5dSDimitry Andric InsertPointAnalysis::getLastInsertPointIter(const LiveInterval &CurLI,
14401095a5dSDimitry Andric                                             MachineBasicBlock &MBB) {
14501095a5dSDimitry Andric   SlotIndex LIP = getLastInsertPoint(CurLI, MBB);
14601095a5dSDimitry Andric   if (LIP == LIS.getMBBEndIdx(&MBB))
14701095a5dSDimitry Andric     return MBB.end();
14801095a5dSDimitry Andric   return LIS.getInstructionFromIndex(LIP);
14901095a5dSDimitry Andric }
15001095a5dSDimitry Andric 
15101095a5dSDimitry Andric //===----------------------------------------------------------------------===//
152d39c594dSDimitry Andric //                                 Split Analysis
153d39c594dSDimitry Andric //===----------------------------------------------------------------------===//
154d39c594dSDimitry Andric 
SplitAnalysis(const VirtRegMap & vrm,const LiveIntervals & lis,const MachineLoopInfo & mli)15567c32a98SDimitry Andric SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis,
156d39c594dSDimitry Andric                              const MachineLoopInfo &mli)
15767c32a98SDimitry Andric     : MF(vrm.getMachineFunction()), VRM(vrm), LIS(lis), Loops(mli),
158044eb2f6SDimitry Andric       TII(*MF.getSubtarget().getInstrInfo()), IPA(lis, MF.getNumBlockIDs()) {}
159d39c594dSDimitry Andric 
clear()160d39c594dSDimitry Andric void SplitAnalysis::clear() {
161cf099d11SDimitry Andric   UseSlots.clear();
1626b943ff3SDimitry Andric   UseBlocks.clear();
1636b943ff3SDimitry Andric   ThroughBlocks.clear();
1645ca98fd9SDimitry Andric   CurLI = nullptr;
165d39c594dSDimitry Andric }
166d39c594dSDimitry Andric 
167cf099d11SDimitry Andric /// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
analyzeUses()168d39c594dSDimitry Andric void SplitAnalysis::analyzeUses() {
1696b943ff3SDimitry Andric   assert(UseSlots.empty() && "Call clear first");
1706b943ff3SDimitry Andric 
1716b943ff3SDimitry Andric   // First get all the defs from the interval values. This provides the correct
1726b943ff3SDimitry Andric   // slots for early clobbers.
17367c32a98SDimitry Andric   for (const VNInfo *VNI : CurLI->valnos)
17467c32a98SDimitry Andric     if (!VNI->isPHIDef() && !VNI->isUnused())
17567c32a98SDimitry Andric       UseSlots.push_back(VNI->def);
1766b943ff3SDimitry Andric 
1776b943ff3SDimitry Andric   // Get use slots form the use-def chain.
178cf099d11SDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
179b60736ecSDimitry Andric   for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg()))
1805ca98fd9SDimitry Andric     if (!MO.isUndef())
18101095a5dSDimitry Andric       UseSlots.push_back(LIS.getInstructionIndex(*MO.getParent()).getRegSlot());
1826b943ff3SDimitry Andric 
183cf099d11SDimitry Andric   array_pod_sort(UseSlots.begin(), UseSlots.end());
1846b943ff3SDimitry Andric 
1856b943ff3SDimitry Andric   // Remove duplicates, keeping the smaller slot for each instruction.
1866b943ff3SDimitry Andric   // That is what we want for early clobbers.
187ac9a064cSDimitry Andric   UseSlots.erase(llvm::unique(UseSlots, SlotIndex::isSameInstr),
1886b943ff3SDimitry Andric                  UseSlots.end());
1896b943ff3SDimitry Andric 
1906b943ff3SDimitry Andric   // Compute per-live block info.
191c0981da4SDimitry Andric   calcLiveBlockInfo();
1926b943ff3SDimitry Andric 
193eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "Analyze counted " << UseSlots.size() << " instrs in "
1946b943ff3SDimitry Andric                     << UseBlocks.size() << " blocks, through "
1956b943ff3SDimitry Andric                     << NumThroughBlocks << " blocks.\n");
196d39c594dSDimitry Andric }
197d39c594dSDimitry Andric 
198cf099d11SDimitry Andric /// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
199cf099d11SDimitry Andric /// where CurLI is live.
calcLiveBlockInfo()200c0981da4SDimitry Andric void SplitAnalysis::calcLiveBlockInfo() {
2016b943ff3SDimitry Andric   ThroughBlocks.resize(MF.getNumBlockIDs());
20256fe8f14SDimitry Andric   NumThroughBlocks = NumGapBlocks = 0;
203cf099d11SDimitry Andric   if (CurLI->empty())
204c0981da4SDimitry Andric     return;
205d39c594dSDimitry Andric 
206cf099d11SDimitry Andric   LiveInterval::const_iterator LVI = CurLI->begin();
207cf099d11SDimitry Andric   LiveInterval::const_iterator LVE = CurLI->end();
208d39c594dSDimitry Andric 
209cf099d11SDimitry Andric   SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
210cf099d11SDimitry Andric   UseI = UseSlots.begin();
211cf099d11SDimitry Andric   UseE = UseSlots.end();
212cf099d11SDimitry Andric 
213cf099d11SDimitry Andric   // Loop over basic blocks where CurLI is live.
214dd58ef01SDimitry Andric   MachineFunction::iterator MFI =
215dd58ef01SDimitry Andric       LIS.getMBBFromIndex(LVI->start)->getIterator();
216044eb2f6SDimitry Andric   while (true) {
217cf099d11SDimitry Andric     BlockInfo BI;
218dd58ef01SDimitry Andric     BI.MBB = &*MFI;
219cf099d11SDimitry Andric     SlotIndex Start, Stop;
2205ca98fd9SDimitry Andric     std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
221cf099d11SDimitry Andric 
22256fe8f14SDimitry Andric     // If the block contains no uses, the range must be live through. At one
223411bd29eSDimitry Andric     // point, RegisterCoalescer could create dangling ranges that ended
22456fe8f14SDimitry Andric     // mid-block.
22556fe8f14SDimitry Andric     if (UseI == UseE || *UseI >= Stop) {
22656fe8f14SDimitry Andric       ++NumThroughBlocks;
22756fe8f14SDimitry Andric       ThroughBlocks.set(BI.MBB->getNumber());
22856fe8f14SDimitry Andric       // The range shouldn't end mid-block if there are no uses. This shouldn't
22956fe8f14SDimitry Andric       // happen.
230c0981da4SDimitry Andric       assert(LVI->end >= Stop && "range ends mid block with no uses");
23156fe8f14SDimitry Andric     } else {
23256fe8f14SDimitry Andric       // This block has uses. Find the first and last uses in the block.
23330815c53SDimitry Andric       BI.FirstInstr = *UseI;
23430815c53SDimitry Andric       assert(BI.FirstInstr >= Start);
235cf099d11SDimitry Andric       do ++UseI;
236cf099d11SDimitry Andric       while (UseI != UseE && *UseI < Stop);
23730815c53SDimitry Andric       BI.LastInstr = UseI[-1];
23830815c53SDimitry Andric       assert(BI.LastInstr < Stop);
23956fe8f14SDimitry Andric 
24056fe8f14SDimitry Andric       // LVI is the first live segment overlapping MBB.
24156fe8f14SDimitry Andric       BI.LiveIn = LVI->start <= Start;
242d39c594dSDimitry Andric 
24330815c53SDimitry Andric       // When not live in, the first use should be a def.
24430815c53SDimitry Andric       if (!BI.LiveIn) {
245f8af5cf6SDimitry Andric         assert(LVI->start == LVI->valno->def && "Dangling Segment start");
24630815c53SDimitry Andric         assert(LVI->start == BI.FirstInstr && "First instr should be a def");
24730815c53SDimitry Andric         BI.FirstDef = BI.FirstInstr;
24830815c53SDimitry Andric       }
24930815c53SDimitry Andric 
250cf099d11SDimitry Andric       // Look for gaps in the live range.
251cf099d11SDimitry Andric       BI.LiveOut = true;
252cf099d11SDimitry Andric       while (LVI->end < Stop) {
253cf099d11SDimitry Andric         SlotIndex LastStop = LVI->end;
254cf099d11SDimitry Andric         if (++LVI == LVE || LVI->start >= Stop) {
255cf099d11SDimitry Andric           BI.LiveOut = false;
25630815c53SDimitry Andric           BI.LastInstr = LastStop;
257d39c594dSDimitry Andric           break;
258d39c594dSDimitry Andric         }
25930815c53SDimitry Andric 
260cf099d11SDimitry Andric         if (LastStop < LVI->start) {
26156fe8f14SDimitry Andric           // There is a gap in the live range. Create duplicate entries for the
26256fe8f14SDimitry Andric           // live-in snippet and the live-out snippet.
26356fe8f14SDimitry Andric           ++NumGapBlocks;
26456fe8f14SDimitry Andric 
26556fe8f14SDimitry Andric           // Push the Live-in part.
26656fe8f14SDimitry Andric           BI.LiveOut = false;
26756fe8f14SDimitry Andric           UseBlocks.push_back(BI);
26830815c53SDimitry Andric           UseBlocks.back().LastInstr = LastStop;
26956fe8f14SDimitry Andric 
27056fe8f14SDimitry Andric           // Set up BI for the live-out part.
27156fe8f14SDimitry Andric           BI.LiveIn = false;
27256fe8f14SDimitry Andric           BI.LiveOut = true;
27330815c53SDimitry Andric           BI.FirstInstr = BI.FirstDef = LVI->start;
274d39c594dSDimitry Andric         }
275d39c594dSDimitry Andric 
276f8af5cf6SDimitry Andric         // A Segment that starts in the middle of the block must be a def.
277f8af5cf6SDimitry Andric         assert(LVI->start == LVI->valno->def && "Dangling Segment start");
27830815c53SDimitry Andric         if (!BI.FirstDef)
27930815c53SDimitry Andric           BI.FirstDef = LVI->start;
28030815c53SDimitry Andric       }
28130815c53SDimitry Andric 
2826b943ff3SDimitry Andric       UseBlocks.push_back(BI);
283d39c594dSDimitry Andric 
284cf099d11SDimitry Andric       // LVI is now at LVE or LVI->end >= Stop.
285cf099d11SDimitry Andric       if (LVI == LVE)
286cf099d11SDimitry Andric         break;
28756fe8f14SDimitry Andric     }
288cf099d11SDimitry Andric 
289cf099d11SDimitry Andric     // Live segment ends exactly at Stop. Move to the next segment.
290cf099d11SDimitry Andric     if (LVI->end == Stop && ++LVI == LVE)
291cf099d11SDimitry Andric       break;
292cf099d11SDimitry Andric 
293cf099d11SDimitry Andric     // Pick the next basic block.
294cf099d11SDimitry Andric     if (LVI->start < Stop)
295cf099d11SDimitry Andric       ++MFI;
296cf099d11SDimitry Andric     else
297dd58ef01SDimitry Andric       MFI = LIS.getMBBFromIndex(LVI->start)->getIterator();
298cf099d11SDimitry Andric   }
29956fe8f14SDimitry Andric 
300b1c73532SDimitry Andric   LooksLikeLoopIV = EnableLoopIVHeuristic && UseBlocks.size() == 2 &&
301b1c73532SDimitry Andric                     any_of(UseBlocks, [this](BlockInfo &BI) {
302b1c73532SDimitry Andric                       MachineLoop *L = Loops.getLoopFor(BI.MBB);
303b1c73532SDimitry Andric                       return BI.LiveIn && BI.LiveOut && BI.FirstDef && L &&
304b1c73532SDimitry Andric                              L->isLoopLatch(BI.MBB);
305b1c73532SDimitry Andric                     });
306b1c73532SDimitry Andric 
30756fe8f14SDimitry Andric   assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
3086b943ff3SDimitry Andric }
3096b943ff3SDimitry Andric 
countLiveBlocks(const LiveInterval * cli) const3106b943ff3SDimitry Andric unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
3116b943ff3SDimitry Andric   if (cli->empty())
3126b943ff3SDimitry Andric     return 0;
3136b943ff3SDimitry Andric   LiveInterval *li = const_cast<LiveInterval*>(cli);
3146b943ff3SDimitry Andric   LiveInterval::iterator LVI = li->begin();
3156b943ff3SDimitry Andric   LiveInterval::iterator LVE = li->end();
3166b943ff3SDimitry Andric   unsigned Count = 0;
3176b943ff3SDimitry Andric 
3186b943ff3SDimitry Andric   // Loop over basic blocks where li is live.
319dd58ef01SDimitry Andric   MachineFunction::const_iterator MFI =
320dd58ef01SDimitry Andric       LIS.getMBBFromIndex(LVI->start)->getIterator();
321dd58ef01SDimitry Andric   SlotIndex Stop = LIS.getMBBEndIdx(&*MFI);
322044eb2f6SDimitry Andric   while (true) {
3236b943ff3SDimitry Andric     ++Count;
3246b943ff3SDimitry Andric     LVI = li->advanceTo(LVI, Stop);
3256b943ff3SDimitry Andric     if (LVI == LVE)
3266b943ff3SDimitry Andric       return Count;
3276b943ff3SDimitry Andric     do {
3286b943ff3SDimitry Andric       ++MFI;
329dd58ef01SDimitry Andric       Stop = LIS.getMBBEndIdx(&*MFI);
3306b943ff3SDimitry Andric     } while (Stop <= LVI->start);
3316b943ff3SDimitry Andric   }
332d39c594dSDimitry Andric }
333d39c594dSDimitry Andric 
isOriginalEndpoint(SlotIndex Idx) const334d0e4e96dSDimitry Andric bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
335e3b55780SDimitry Andric   Register OrigReg = VRM.getOriginal(CurLI->reg());
336d0e4e96dSDimitry Andric   const LiveInterval &Orig = LIS.getInterval(OrigReg);
337d0e4e96dSDimitry Andric   assert(!Orig.empty() && "Splitting empty interval?");
338d0e4e96dSDimitry Andric   LiveInterval::const_iterator I = Orig.find(Idx);
339d0e4e96dSDimitry Andric 
340d0e4e96dSDimitry Andric   // Range containing Idx should begin at Idx.
341d0e4e96dSDimitry Andric   if (I != Orig.end() && I->start <= Idx)
342d0e4e96dSDimitry Andric     return I->start == Idx;
343d0e4e96dSDimitry Andric 
344d0e4e96dSDimitry Andric   // Range does not contain Idx, previous must end at Idx.
345d0e4e96dSDimitry Andric   return I != Orig.begin() && (--I)->end == Idx;
346d0e4e96dSDimitry Andric }
347d0e4e96dSDimitry Andric 
analyze(const LiveInterval * li)348d39c594dSDimitry Andric void SplitAnalysis::analyze(const LiveInterval *li) {
349d39c594dSDimitry Andric   clear();
350cf099d11SDimitry Andric   CurLI = li;
351d39c594dSDimitry Andric   analyzeUses();
352d39c594dSDimitry Andric }
353d39c594dSDimitry Andric 
354d39c594dSDimitry Andric //===----------------------------------------------------------------------===//
3556b943ff3SDimitry Andric //                               Split Editor
356d39c594dSDimitry Andric //===----------------------------------------------------------------------===//
357d39c594dSDimitry Andric 
3586b943ff3SDimitry Andric /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
SplitEditor(SplitAnalysis & SA,LiveIntervals & LIS,VirtRegMap & VRM,MachineDominatorTree & MDT,MachineBlockFrequencyInfo & MBFI,VirtRegAuxInfo & VRAI)3594b4fe385SDimitry Andric SplitEditor::SplitEditor(SplitAnalysis &SA, LiveIntervals &LIS, VirtRegMap &VRM,
360344a3780SDimitry Andric                          MachineDominatorTree &MDT,
361344a3780SDimitry Andric                          MachineBlockFrequencyInfo &MBFI, VirtRegAuxInfo &VRAI)
3624b4fe385SDimitry Andric     : SA(SA), LIS(LIS), VRM(VRM), MRI(VRM.getMachineFunction().getRegInfo()),
3634b4fe385SDimitry Andric       MDT(MDT), TII(*VRM.getMachineFunction().getSubtarget().getInstrInfo()),
364344a3780SDimitry Andric       TRI(*VRM.getMachineFunction().getSubtarget().getRegisterInfo()),
365344a3780SDimitry Andric       MBFI(MBFI), VRAI(VRAI), RegAssign(Allocator) {}
366d39c594dSDimitry Andric 
reset(LiveRangeEdit & LRE,ComplementSpillMode SM)36730815c53SDimitry Andric void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
36830815c53SDimitry Andric   Edit = &LRE;
36930815c53SDimitry Andric   SpillMode = SM;
3706b943ff3SDimitry Andric   OpenIdx = 0;
3716b943ff3SDimitry Andric   RegAssign.clear();
372cf099d11SDimitry Andric   Values.clear();
3736b943ff3SDimitry Andric 
374cfca06d7SDimitry Andric   // Reset the LiveIntervalCalc instances needed for this spill mode.
375cfca06d7SDimitry Andric   LICalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
37658b69754SDimitry Andric                   &LIS.getVNInfoAllocator());
37730815c53SDimitry Andric   if (SpillMode)
378cfca06d7SDimitry Andric     LICalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
37958b69754SDimitry Andric                     &LIS.getVNInfoAllocator());
3806b943ff3SDimitry Andric 
3814b4fe385SDimitry Andric   Edit->anyRematerializable();
382cf099d11SDimitry Andric }
383cf099d11SDimitry Andric 
384522600a2SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const38501095a5dSDimitry Andric LLVM_DUMP_METHOD void SplitEditor::dump() const {
3866b943ff3SDimitry Andric   if (RegAssign.empty()) {
3876b943ff3SDimitry Andric     dbgs() << " empty\n";
3886b943ff3SDimitry Andric     return;
389cf099d11SDimitry Andric   }
390cf099d11SDimitry Andric 
3916b943ff3SDimitry Andric   for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
3926b943ff3SDimitry Andric     dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
3936b943ff3SDimitry Andric   dbgs() << '\n';
3946b943ff3SDimitry Andric }
395522600a2SDimitry Andric #endif
3966b943ff3SDimitry Andric 
397145449b1SDimitry Andric /// Find a subrange corresponding to the exact lane mask @p LM in the live
398145449b1SDimitry Andric /// interval @p LI. The interval @p LI is assumed to contain such a subrange.
399145449b1SDimitry Andric /// This function is used to find corresponding subranges between the
400145449b1SDimitry Andric /// original interval and the new intervals.
getSubrangeImpl(LaneBitmask LM,T & LI)401145449b1SDimitry Andric template <typename T> auto &getSubrangeImpl(LaneBitmask LM, T &LI) {
402145449b1SDimitry Andric   for (auto &S : LI.subranges())
403b915e9e0SDimitry Andric     if (S.LaneMask == LM)
404b915e9e0SDimitry Andric       return S;
405b915e9e0SDimitry Andric   llvm_unreachable("SubRange for this mask not found");
406b915e9e0SDimitry Andric }
407b915e9e0SDimitry Andric 
getSubRangeForMaskExact(LaneBitmask LM,LiveInterval & LI)408145449b1SDimitry Andric LiveInterval::SubRange &getSubRangeForMaskExact(LaneBitmask LM,
409b60736ecSDimitry Andric                                                 LiveInterval &LI) {
410145449b1SDimitry Andric   return getSubrangeImpl(LM, LI);
411145449b1SDimitry Andric }
412145449b1SDimitry Andric 
getSubRangeForMaskExact(LaneBitmask LM,const LiveInterval & LI)413145449b1SDimitry Andric const LiveInterval::SubRange &getSubRangeForMaskExact(LaneBitmask LM,
414145449b1SDimitry Andric                                                       const LiveInterval &LI) {
415145449b1SDimitry Andric   return getSubrangeImpl(LM, LI);
416145449b1SDimitry Andric }
417145449b1SDimitry Andric 
418145449b1SDimitry Andric /// Find a subrange corresponding to the lane mask @p LM, or a superset of it,
419145449b1SDimitry Andric /// in the live interval @p LI. The interval @p LI is assumed to contain such
420145449b1SDimitry Andric /// a subrange.  This function is used to find corresponding subranges between
421145449b1SDimitry Andric /// the original interval and the new intervals.
getSubRangeForMask(LaneBitmask LM,const LiveInterval & LI)422145449b1SDimitry Andric const LiveInterval::SubRange &getSubRangeForMask(LaneBitmask LM,
423145449b1SDimitry Andric                                                  const LiveInterval &LI) {
424145449b1SDimitry Andric   for (const LiveInterval::SubRange &S : LI.subranges())
425b60736ecSDimitry Andric     if ((S.LaneMask & LM) == LM)
426b60736ecSDimitry Andric       return S;
427b60736ecSDimitry Andric   llvm_unreachable("SubRange for this mask not found");
428b60736ecSDimitry Andric }
429b60736ecSDimitry Andric 
addDeadDef(LiveInterval & LI,VNInfo * VNI,bool Original)430b915e9e0SDimitry Andric void SplitEditor::addDeadDef(LiveInterval &LI, VNInfo *VNI, bool Original) {
431b915e9e0SDimitry Andric   if (!LI.hasSubRanges()) {
432b915e9e0SDimitry Andric     LI.createDeadDef(VNI);
433b915e9e0SDimitry Andric     return;
434b915e9e0SDimitry Andric   }
435b915e9e0SDimitry Andric 
436b915e9e0SDimitry Andric   SlotIndex Def = VNI->def;
437b915e9e0SDimitry Andric   if (Original) {
438b915e9e0SDimitry Andric     // If we are transferring a def from the original interval, make sure
439b915e9e0SDimitry Andric     // to only update the subranges for which the original subranges had
440b915e9e0SDimitry Andric     // a def at this location.
441b915e9e0SDimitry Andric     for (LiveInterval::SubRange &S : LI.subranges()) {
442b915e9e0SDimitry Andric       auto &PS = getSubRangeForMask(S.LaneMask, Edit->getParent());
443b915e9e0SDimitry Andric       VNInfo *PV = PS.getVNInfoAt(Def);
444b915e9e0SDimitry Andric       if (PV != nullptr && PV->def == Def)
445b915e9e0SDimitry Andric         S.createDeadDef(Def, LIS.getVNInfoAllocator());
446b915e9e0SDimitry Andric     }
447b915e9e0SDimitry Andric   } else {
448b915e9e0SDimitry Andric     // This is a new def: either from rematerialization, or from an inserted
449b915e9e0SDimitry Andric     // copy. Since rematerialization can regenerate a definition of a sub-
450b915e9e0SDimitry Andric     // register, we need to check which subranges need to be updated.
451b915e9e0SDimitry Andric     const MachineInstr *DefMI = LIS.getInstructionFromIndex(Def);
452b915e9e0SDimitry Andric     assert(DefMI != nullptr);
453b915e9e0SDimitry Andric     LaneBitmask LM;
454b915e9e0SDimitry Andric     for (const MachineOperand &DefOp : DefMI->defs()) {
4551d5ae102SDimitry Andric       Register R = DefOp.getReg();
456b60736ecSDimitry Andric       if (R != LI.reg())
457b915e9e0SDimitry Andric         continue;
458b915e9e0SDimitry Andric       if (unsigned SR = DefOp.getSubReg())
459b915e9e0SDimitry Andric         LM |= TRI.getSubRegIndexLaneMask(SR);
460b915e9e0SDimitry Andric       else {
461b915e9e0SDimitry Andric         LM = MRI.getMaxLaneMaskForVReg(R);
462b915e9e0SDimitry Andric         break;
463b915e9e0SDimitry Andric       }
464b915e9e0SDimitry Andric     }
465b915e9e0SDimitry Andric     for (LiveInterval::SubRange &S : LI.subranges())
466b915e9e0SDimitry Andric       if ((S.LaneMask & LM).any())
467b915e9e0SDimitry Andric         S.createDeadDef(Def, LIS.getVNInfoAllocator());
468b915e9e0SDimitry Andric   }
469b915e9e0SDimitry Andric }
470b915e9e0SDimitry Andric 
defValue(unsigned RegIdx,const VNInfo * ParentVNI,SlotIndex Idx,bool Original)4716b943ff3SDimitry Andric VNInfo *SplitEditor::defValue(unsigned RegIdx,
4726b943ff3SDimitry Andric                               const VNInfo *ParentVNI,
473b915e9e0SDimitry Andric                               SlotIndex Idx,
474b915e9e0SDimitry Andric                               bool Original) {
475d39c594dSDimitry Andric   assert(ParentVNI && "Mapping  NULL value");
476d39c594dSDimitry Andric   assert(Idx.isValid() && "Invalid SlotIndex");
4776b943ff3SDimitry Andric   assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
478f8af5cf6SDimitry Andric   LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
479cf099d11SDimitry Andric 
480cf099d11SDimitry Andric   // Create a new value.
48163faed5bSDimitry Andric   VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
482cf099d11SDimitry Andric 
483b915e9e0SDimitry Andric   bool Force = LI->hasSubRanges();
484b915e9e0SDimitry Andric   ValueForcePair FP(Force ? nullptr : VNI, Force);
485d39c594dSDimitry Andric   // Use insert for lookup, so we can add missing values with a second lookup.
486d39c594dSDimitry Andric   std::pair<ValueMap::iterator, bool> InsP =
487b915e9e0SDimitry Andric     Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), FP));
488cf099d11SDimitry Andric 
489b915e9e0SDimitry Andric   // This was the first time (RegIdx, ParentVNI) was mapped, and it is not
490b915e9e0SDimitry Andric   // forced. Keep it as a simple def without any liveness.
491b915e9e0SDimitry Andric   if (!Force && InsP.second)
4926b943ff3SDimitry Andric     return VNI;
4936b943ff3SDimitry Andric 
4946b943ff3SDimitry Andric   // If the previous value was a simple mapping, add liveness for it now.
49530815c53SDimitry Andric   if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
496b915e9e0SDimitry Andric     addDeadDef(*LI, OldVNI, Original);
497b915e9e0SDimitry Andric 
498b915e9e0SDimitry Andric     // No longer a simple mapping.  Switch to a complex mapping. If the
499b915e9e0SDimitry Andric     // interval has subranges, make it a forced mapping.
500b915e9e0SDimitry Andric     InsP.first->second = ValueForcePair(nullptr, Force);
5016b943ff3SDimitry Andric   }
5026b943ff3SDimitry Andric 
5036b943ff3SDimitry Andric   // This is a complex mapping, add liveness for VNI
504b915e9e0SDimitry Andric   addDeadDef(*LI, VNI, Original);
505cf099d11SDimitry Andric   return VNI;
506cf099d11SDimitry Andric }
507cf099d11SDimitry Andric 
forceRecompute(unsigned RegIdx,const VNInfo & ParentVNI)508eb11fae6SDimitry Andric void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo &ParentVNI) {
509eb11fae6SDimitry Andric   ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI.id)];
51030815c53SDimitry Andric   VNInfo *VNI = VFP.getPointer();
5116b943ff3SDimitry Andric 
51230815c53SDimitry Andric   // ParentVNI was either unmapped or already complex mapped. Either way, just
51330815c53SDimitry Andric   // set the force bit.
51430815c53SDimitry Andric   if (!VNI) {
51530815c53SDimitry Andric     VFP.setInt(true);
5166b943ff3SDimitry Andric     return;
51730815c53SDimitry Andric   }
5186b943ff3SDimitry Andric 
5196b943ff3SDimitry Andric   // This was previously a single mapping. Make sure the old def is represented
5206b943ff3SDimitry Andric   // by a trivial live range.
521b915e9e0SDimitry Andric   addDeadDef(LIS.getInterval(Edit->get(RegIdx)), VNI, false);
522b915e9e0SDimitry Andric 
52330815c53SDimitry Andric   // Mark as complex mapped, forced.
5245ca98fd9SDimitry Andric   VFP = ValueForcePair(nullptr, true);
525d39c594dSDimitry Andric }
526d39c594dSDimitry Andric 
buildSingleSubRegCopy(Register FromReg,Register ToReg,MachineBasicBlock & MBB,MachineBasicBlock::iterator InsertBefore,unsigned SubIdx,LiveInterval & DestLI,bool Late,SlotIndex Def,const MCInstrDesc & Desc)5277fa27ce4SDimitry Andric SlotIndex SplitEditor::buildSingleSubRegCopy(
5287fa27ce4SDimitry Andric     Register FromReg, Register ToReg, MachineBasicBlock &MBB,
5297fa27ce4SDimitry Andric     MachineBasicBlock::iterator InsertBefore, unsigned SubIdx,
5307fa27ce4SDimitry Andric     LiveInterval &DestLI, bool Late, SlotIndex Def, const MCInstrDesc &Desc) {
53171d5a254SDimitry Andric   bool FirstCopy = !Def.isValid();
53271d5a254SDimitry Andric   MachineInstr *CopyMI = BuildMI(MBB, InsertBefore, DebugLoc(), Desc)
53371d5a254SDimitry Andric       .addReg(ToReg, RegState::Define | getUndefRegState(FirstCopy)
53471d5a254SDimitry Andric               | getInternalReadRegState(!FirstCopy), SubIdx)
53571d5a254SDimitry Andric       .addReg(FromReg, 0, SubIdx);
53671d5a254SDimitry Andric 
53771d5a254SDimitry Andric   SlotIndexes &Indexes = *LIS.getSlotIndexes();
538e6d15924SDimitry Andric   if (FirstCopy) {
53971d5a254SDimitry Andric     Def = Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot();
54071d5a254SDimitry Andric   } else {
54171d5a254SDimitry Andric     CopyMI->bundleWithPred();
54271d5a254SDimitry Andric   }
54371d5a254SDimitry Andric   return Def;
54471d5a254SDimitry Andric }
54571d5a254SDimitry Andric 
buildCopy(Register FromReg,Register ToReg,LaneBitmask LaneMask,MachineBasicBlock & MBB,MachineBasicBlock::iterator InsertBefore,bool Late,unsigned RegIdx)546b60736ecSDimitry Andric SlotIndex SplitEditor::buildCopy(Register FromReg, Register ToReg,
54771d5a254SDimitry Andric     LaneBitmask LaneMask, MachineBasicBlock &MBB,
54871d5a254SDimitry Andric     MachineBasicBlock::iterator InsertBefore, bool Late, unsigned RegIdx) {
5497fa27ce4SDimitry Andric   const MCInstrDesc &Desc =
5507fa27ce4SDimitry Andric       TII.get(TII.getLiveRangeSplitOpcode(FromReg, *MBB.getParent()));
551c0981da4SDimitry Andric   SlotIndexes &Indexes = *LIS.getSlotIndexes();
55271d5a254SDimitry Andric   if (LaneMask.all() || LaneMask == MRI.getMaxLaneMaskForVReg(FromReg)) {
55371d5a254SDimitry Andric     // The full vreg is copied.
55471d5a254SDimitry Andric     MachineInstr *CopyMI =
55571d5a254SDimitry Andric         BuildMI(MBB, InsertBefore, DebugLoc(), Desc, ToReg).addReg(FromReg);
55671d5a254SDimitry Andric     return Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot();
55771d5a254SDimitry Andric   }
55871d5a254SDimitry Andric 
55971d5a254SDimitry Andric   // Only a subset of lanes needs to be copied. The following is a simple
56071d5a254SDimitry Andric   // heuristic to construct a sequence of COPYs. We could add a target
56171d5a254SDimitry Andric   // specific callback if this turns out to be suboptimal.
56271d5a254SDimitry Andric   LiveInterval &DestLI = LIS.getInterval(Edit->get(RegIdx));
56371d5a254SDimitry Andric 
56471d5a254SDimitry Andric   // First pass: Try to find a perfectly matching subregister index. If none
56571d5a254SDimitry Andric   // exists find the one covering the most lanemask bits.
56671d5a254SDimitry Andric   const TargetRegisterClass *RC = MRI.getRegClass(FromReg);
56771d5a254SDimitry Andric   assert(RC == MRI.getRegClass(ToReg) && "Should have same reg class");
56871d5a254SDimitry Andric 
569c0981da4SDimitry Andric   SmallVector<unsigned, 8> SubIndexes;
57071d5a254SDimitry Andric 
57171d5a254SDimitry Andric   // Abort if we cannot possibly implement the COPY with the given indexes.
572c0981da4SDimitry Andric   if (!TRI.getCoveringSubRegIndexes(MRI, RC, LaneMask, SubIndexes))
57371d5a254SDimitry Andric     report_fatal_error("Impossible to implement partial COPY");
57471d5a254SDimitry Andric 
575344a3780SDimitry Andric   SlotIndex Def;
576c0981da4SDimitry Andric   for (unsigned BestIdx : SubIndexes) {
577344a3780SDimitry Andric     Def = buildSingleSubRegCopy(FromReg, ToReg, MBB, InsertBefore, BestIdx,
5787fa27ce4SDimitry Andric                                 DestLI, Late, Def, Desc);
57971d5a254SDimitry Andric   }
58071d5a254SDimitry Andric 
581c0981da4SDimitry Andric   BumpPtrAllocator &Allocator = LIS.getVNInfoAllocator();
582c0981da4SDimitry Andric   DestLI.refineSubRanges(
583c0981da4SDimitry Andric       Allocator, LaneMask,
584c0981da4SDimitry Andric       [Def, &Allocator](LiveInterval::SubRange &SR) {
585c0981da4SDimitry Andric         SR.createDeadDef(Def, Allocator);
586c0981da4SDimitry Andric       },
587c0981da4SDimitry Andric       Indexes, TRI);
588c0981da4SDimitry Andric 
58971d5a254SDimitry Andric   return Def;
59071d5a254SDimitry Andric }
59171d5a254SDimitry Andric 
defFromParent(unsigned RegIdx,const VNInfo * ParentVNI,SlotIndex UseIdx,MachineBasicBlock & MBB,MachineBasicBlock::iterator I)592145449b1SDimitry Andric VNInfo *SplitEditor::defFromParent(unsigned RegIdx, const VNInfo *ParentVNI,
593145449b1SDimitry Andric                                    SlotIndex UseIdx, MachineBasicBlock &MBB,
594d39c594dSDimitry Andric                                    MachineBasicBlock::iterator I) {
595cf099d11SDimitry Andric   SlotIndex Def;
596f8af5cf6SDimitry Andric   LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
5976b943ff3SDimitry Andric 
5986b943ff3SDimitry Andric   // We may be trying to avoid interference that ends at a deleted instruction,
5996b943ff3SDimitry Andric   // so always begin RegIdx 0 early and all others late.
6006b943ff3SDimitry Andric   bool Late = RegIdx != 0;
601cf099d11SDimitry Andric 
602cf099d11SDimitry Andric   // Attempt cheap-as-a-copy rematerialization.
603e3b55780SDimitry Andric   Register Original = VRM.getOriginal(Edit->get(RegIdx));
60401095a5dSDimitry Andric   LiveInterval &OrigLI = LIS.getInterval(Original);
60501095a5dSDimitry Andric   VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx);
606b915e9e0SDimitry Andric 
607b60736ecSDimitry Andric   Register Reg = LI->reg();
608b915e9e0SDimitry Andric   bool DidRemat = false;
609b915e9e0SDimitry Andric   if (OrigVNI) {
610cf099d11SDimitry Andric     LiveRangeEdit::Remat RM(ParentVNI);
61101095a5dSDimitry Andric     RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def);
61201095a5dSDimitry Andric     if (Edit->canRematerializeAt(RM, OrigVNI, UseIdx, true)) {
61371d5a254SDimitry Andric       Def = Edit->rematerializeAt(MBB, I, Reg, RM, TRI, Late);
61456fe8f14SDimitry Andric       ++NumRemats;
615b915e9e0SDimitry Andric       DidRemat = true;
616b915e9e0SDimitry Andric     }
617b915e9e0SDimitry Andric   }
618b915e9e0SDimitry Andric   if (!DidRemat) {
61971d5a254SDimitry Andric     LaneBitmask LaneMask;
620b60736ecSDimitry Andric     if (OrigLI.hasSubRanges()) {
62171d5a254SDimitry Andric       LaneMask = LaneBitmask::getNone();
622b60736ecSDimitry Andric       for (LiveInterval::SubRange &S : OrigLI.subranges()) {
623b60736ecSDimitry Andric         if (S.liveAt(UseIdx))
62471d5a254SDimitry Andric           LaneMask |= S.LaneMask;
625b60736ecSDimitry Andric       }
62671d5a254SDimitry Andric     } else {
62771d5a254SDimitry Andric       LaneMask = LaneBitmask::getAll();
62871d5a254SDimitry Andric     }
62971d5a254SDimitry Andric 
630b60736ecSDimitry Andric     if (LaneMask.none()) {
631b60736ecSDimitry Andric       const MCInstrDesc &Desc = TII.get(TargetOpcode::IMPLICIT_DEF);
632b60736ecSDimitry Andric       MachineInstr *ImplicitDef = BuildMI(MBB, I, DebugLoc(), Desc, Reg);
633b60736ecSDimitry Andric       SlotIndexes &Indexes = *LIS.getSlotIndexes();
634b60736ecSDimitry Andric       Def = Indexes.insertMachineInstrInMaps(*ImplicitDef, Late).getRegSlot();
635b60736ecSDimitry Andric     } else {
63656fe8f14SDimitry Andric       ++NumCopies;
63771d5a254SDimitry Andric       Def = buildCopy(Edit->getReg(), Reg, LaneMask, MBB, I, Late, RegIdx);
638cf099d11SDimitry Andric     }
639b60736ecSDimitry Andric   }
640cf099d11SDimitry Andric 
641cf099d11SDimitry Andric   // Define the value in Reg.
642b915e9e0SDimitry Andric   return defValue(RegIdx, ParentVNI, Def, false);
643d39c594dSDimitry Andric }
644d39c594dSDimitry Andric 
645d39c594dSDimitry Andric /// Create a new virtual register and live interval.
openIntv()6466b943ff3SDimitry Andric unsigned SplitEditor::openIntv() {
647cf099d11SDimitry Andric   // Create the complement as index 0.
6486b943ff3SDimitry Andric   if (Edit->empty())
649f8af5cf6SDimitry Andric     Edit->createEmptyInterval();
650d39c594dSDimitry Andric 
651cf099d11SDimitry Andric   // Create the open interval.
6526b943ff3SDimitry Andric   OpenIdx = Edit->size();
653f8af5cf6SDimitry Andric   Edit->createEmptyInterval();
6546b943ff3SDimitry Andric   return OpenIdx;
6556b943ff3SDimitry Andric }
6566b943ff3SDimitry Andric 
selectIntv(unsigned Idx)6576b943ff3SDimitry Andric void SplitEditor::selectIntv(unsigned Idx) {
6586b943ff3SDimitry Andric   assert(Idx != 0 && "Cannot select the complement interval");
6596b943ff3SDimitry Andric   assert(Idx < Edit->size() && "Can only select previously opened interval");
660eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "    selectIntv " << OpenIdx << " -> " << Idx << '\n');
6616b943ff3SDimitry Andric   OpenIdx = Idx;
662cf099d11SDimitry Andric }
663d39c594dSDimitry Andric 
enterIntvBefore(SlotIndex Idx)664cf099d11SDimitry Andric SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
665cf099d11SDimitry Andric   assert(OpenIdx && "openIntv not called before enterIntvBefore");
666eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "    enterIntvBefore " << Idx);
667cf099d11SDimitry Andric   Idx = Idx.getBaseIndex();
6686b943ff3SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
669cf099d11SDimitry Andric   if (!ParentVNI) {
670eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << ": not live\n");
671cf099d11SDimitry Andric     return Idx;
672cf099d11SDimitry Andric   }
673eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
674cf099d11SDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
675d39c594dSDimitry Andric   assert(MI && "enterIntvBefore called with invalid index");
676d39c594dSDimitry Andric 
677cf099d11SDimitry Andric   VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
678cf099d11SDimitry Andric   return VNI->def;
679d39c594dSDimitry Andric }
680d39c594dSDimitry Andric 
enterIntvAfter(SlotIndex Idx)681411bd29eSDimitry Andric SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
682411bd29eSDimitry Andric   assert(OpenIdx && "openIntv not called before enterIntvAfter");
683eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "    enterIntvAfter " << Idx);
684411bd29eSDimitry Andric   Idx = Idx.getBoundaryIndex();
685411bd29eSDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
686411bd29eSDimitry Andric   if (!ParentVNI) {
687eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << ": not live\n");
688411bd29eSDimitry Andric     return Idx;
689411bd29eSDimitry Andric   }
690eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
691411bd29eSDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
692411bd29eSDimitry Andric   assert(MI && "enterIntvAfter called with invalid index");
693411bd29eSDimitry Andric 
694411bd29eSDimitry Andric   VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
6955ca98fd9SDimitry Andric                               std::next(MachineBasicBlock::iterator(MI)));
696411bd29eSDimitry Andric   return VNI->def;
697411bd29eSDimitry Andric }
698411bd29eSDimitry Andric 
enterIntvAtEnd(MachineBasicBlock & MBB)699cf099d11SDimitry Andric SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
700cf099d11SDimitry Andric   assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
701cf099d11SDimitry Andric   SlotIndex End = LIS.getMBBEndIdx(&MBB);
702cf099d11SDimitry Andric   SlotIndex Last = End.getPrevSlot();
703eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "    enterIntvAtEnd " << printMBBReference(MBB) << ", "
704044eb2f6SDimitry Andric                     << Last);
7056b943ff3SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
706cf099d11SDimitry Andric   if (!ParentVNI) {
707eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << ": not live\n");
708cf099d11SDimitry Andric     return End;
709cf099d11SDimitry Andric   }
710344a3780SDimitry Andric   SlotIndex LSP = SA.getLastSplitPoint(&MBB);
711344a3780SDimitry Andric   if (LSP < Last) {
712344a3780SDimitry Andric     // It could be that the use after LSP is a def, and thus the ParentVNI
713344a3780SDimitry Andric     // just selected starts at that def.  For this case to exist, the def
714344a3780SDimitry Andric     // must be part of a tied def/use pair (as otherwise we'd have split
715344a3780SDimitry Andric     // distinct live ranges into individual live intervals), and thus we
716344a3780SDimitry Andric     // can insert the def into the VNI of the use and the tied def/use
717344a3780SDimitry Andric     // pair can live in the resulting interval.
718344a3780SDimitry Andric     Last = LSP;
719344a3780SDimitry Andric     ParentVNI = Edit->getParent().getVNInfoAt(Last);
720344a3780SDimitry Andric     if (!ParentVNI) {
721344a3780SDimitry Andric       // undef use --> undef tied def
722344a3780SDimitry Andric       LLVM_DEBUG(dbgs() << ": tied use not live\n");
723344a3780SDimitry Andric       return End;
724344a3780SDimitry Andric     }
725344a3780SDimitry Andric   }
726344a3780SDimitry Andric 
727eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id);
728cf099d11SDimitry Andric   VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
72963faed5bSDimitry Andric                               SA.getLastSplitPointIter(&MBB));
730cf099d11SDimitry Andric   RegAssign.insert(VNI->def, End, OpenIdx);
731eb11fae6SDimitry Andric   LLVM_DEBUG(dump());
732cf099d11SDimitry Andric   return VNI->def;
733d39c594dSDimitry Andric }
734d39c594dSDimitry Andric 
735cf099d11SDimitry Andric /// useIntv - indicate that all instructions in MBB should use OpenLI.
useIntv(const MachineBasicBlock & MBB)736d39c594dSDimitry Andric void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
737cf099d11SDimitry Andric   useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
738d39c594dSDimitry Andric }
739d39c594dSDimitry Andric 
useIntv(SlotIndex Start,SlotIndex End)740d39c594dSDimitry Andric void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
741cf099d11SDimitry Andric   assert(OpenIdx && "openIntv not called before useIntv");
742eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "    useIntv [" << Start << ';' << End << "):");
743cf099d11SDimitry Andric   RegAssign.insert(Start, End, OpenIdx);
744eb11fae6SDimitry Andric   LLVM_DEBUG(dump());
745d39c594dSDimitry Andric }
746d39c594dSDimitry Andric 
leaveIntvAfter(SlotIndex Idx)747cf099d11SDimitry Andric SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
748cf099d11SDimitry Andric   assert(OpenIdx && "openIntv not called before leaveIntvAfter");
749eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "    leaveIntvAfter " << Idx);
750cf099d11SDimitry Andric 
751cf099d11SDimitry Andric   // The interval must be live beyond the instruction at Idx.
75230815c53SDimitry Andric   SlotIndex Boundary = Idx.getBoundaryIndex();
75330815c53SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
754cf099d11SDimitry Andric   if (!ParentVNI) {
755eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << ": not live\n");
75630815c53SDimitry Andric     return Boundary.getNextSlot();
757cf099d11SDimitry Andric   }
758eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
75930815c53SDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
760cf099d11SDimitry Andric   assert(MI && "No instruction at index");
76130815c53SDimitry Andric 
76230815c53SDimitry Andric   // In spill mode, make live ranges as short as possible by inserting the copy
76330815c53SDimitry Andric   // before MI.  This is only possible if that instruction doesn't redefine the
76430815c53SDimitry Andric   // value.  The inserted COPY is not a kill, and we don't need to recompute
76530815c53SDimitry Andric   // the source live range.  The spiller also won't try to hoist this copy.
76630815c53SDimitry Andric   if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
76730815c53SDimitry Andric       MI->readsVirtualRegister(Edit->getReg())) {
768eb11fae6SDimitry Andric     forceRecompute(0, *ParentVNI);
76930815c53SDimitry Andric     defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
77030815c53SDimitry Andric     return Idx;
77130815c53SDimitry Andric   }
77230815c53SDimitry Andric 
77330815c53SDimitry Andric   VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
7745ca98fd9SDimitry Andric                               std::next(MachineBasicBlock::iterator(MI)));
775cf099d11SDimitry Andric   return VNI->def;
776d39c594dSDimitry Andric }
777d39c594dSDimitry Andric 
leaveIntvBefore(SlotIndex Idx)778cf099d11SDimitry Andric SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
779cf099d11SDimitry Andric   assert(OpenIdx && "openIntv not called before leaveIntvBefore");
780eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "    leaveIntvBefore " << Idx);
781d39c594dSDimitry Andric 
782cf099d11SDimitry Andric   // The interval must be live into the instruction at Idx.
78330815c53SDimitry Andric   Idx = Idx.getBaseIndex();
7846b943ff3SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
785cf099d11SDimitry Andric   if (!ParentVNI) {
786eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << ": not live\n");
787cf099d11SDimitry Andric     return Idx.getNextSlot();
788cf099d11SDimitry Andric   }
789eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
790cf099d11SDimitry Andric 
791cf099d11SDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
792cf099d11SDimitry Andric   assert(MI && "No instruction at index");
793cf099d11SDimitry Andric   VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
794cf099d11SDimitry Andric   return VNI->def;
795d39c594dSDimitry Andric }
796d39c594dSDimitry Andric 
leaveIntvAtTop(MachineBasicBlock & MBB)797cf099d11SDimitry Andric SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
798cf099d11SDimitry Andric   assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
799cf099d11SDimitry Andric   SlotIndex Start = LIS.getMBBStartIdx(&MBB);
800eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "    leaveIntvAtTop " << printMBBReference(MBB) << ", "
801044eb2f6SDimitry Andric                     << Start);
802cf099d11SDimitry Andric 
8036b943ff3SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
804cf099d11SDimitry Andric   if (!ParentVNI) {
805eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << ": not live\n");
806cf099d11SDimitry Andric     return Start;
807d39c594dSDimitry Andric   }
808d39c594dSDimitry Andric 
809b1c73532SDimitry Andric   unsigned RegIdx = 0;
810b1c73532SDimitry Andric   Register Reg = LIS.getInterval(Edit->get(RegIdx)).reg();
811b1c73532SDimitry Andric   VNInfo *VNI = defFromParent(RegIdx, ParentVNI, Start, MBB,
812b1c73532SDimitry Andric                               MBB.SkipPHIsLabelsAndDebug(MBB.begin(), Reg));
813cf099d11SDimitry Andric   RegAssign.insert(Start, VNI->def, OpenIdx);
814eb11fae6SDimitry Andric   LLVM_DEBUG(dump());
815cf099d11SDimitry Andric   return VNI->def;
816d39c594dSDimitry Andric }
817d39c594dSDimitry Andric 
hasTiedUseOf(MachineInstr & MI,unsigned Reg)818344a3780SDimitry Andric static bool hasTiedUseOf(MachineInstr &MI, unsigned Reg) {
819344a3780SDimitry Andric   return any_of(MI.defs(), [Reg](const MachineOperand &MO) {
820344a3780SDimitry Andric     return MO.isReg() && MO.isTied() && MO.getReg() == Reg;
821344a3780SDimitry Andric   });
822344a3780SDimitry Andric }
823344a3780SDimitry Andric 
overlapIntv(SlotIndex Start,SlotIndex End)824cf099d11SDimitry Andric void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
825cf099d11SDimitry Andric   assert(OpenIdx && "openIntv not called before overlapIntv");
8266b943ff3SDimitry Andric   const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
82763faed5bSDimitry Andric   assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
828cf099d11SDimitry Andric          "Parent changes value in extended range");
829cf099d11SDimitry Andric   assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
830cf099d11SDimitry Andric          "Range cannot span basic blocks");
831d39c594dSDimitry Andric 
832cfca06d7SDimitry Andric   // The complement interval will be extended as needed by LICalc.extend().
8336b943ff3SDimitry Andric   if (ParentVNI)
834eb11fae6SDimitry Andric     forceRecompute(0, *ParentVNI);
835344a3780SDimitry Andric 
836344a3780SDimitry Andric   // If the last use is tied to a def, we can't mark it as live for the
837344a3780SDimitry Andric   // interval which includes only the use.  That would cause the tied pair
838344a3780SDimitry Andric   // to end up in two different intervals.
839344a3780SDimitry Andric   if (auto *MI = LIS.getInstructionFromIndex(End))
840344a3780SDimitry Andric     if (hasTiedUseOf(*MI, Edit->getReg())) {
841344a3780SDimitry Andric       LLVM_DEBUG(dbgs() << "skip overlap due to tied def at end\n");
842344a3780SDimitry Andric       return;
843344a3780SDimitry Andric     }
844344a3780SDimitry Andric 
845eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "    overlapIntv [" << Start << ';' << End << "):");
846cf099d11SDimitry Andric   RegAssign.insert(Start, End, OpenIdx);
847eb11fae6SDimitry Andric   LLVM_DEBUG(dump());
848d39c594dSDimitry Andric }
849d39c594dSDimitry Andric 
85030815c53SDimitry Andric //===----------------------------------------------------------------------===//
85130815c53SDimitry Andric //                                  Spill modes
85230815c53SDimitry Andric //===----------------------------------------------------------------------===//
85330815c53SDimitry Andric 
removeBackCopies(SmallVectorImpl<VNInfo * > & Copies)85430815c53SDimitry Andric void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
855f8af5cf6SDimitry Andric   LiveInterval *LI = &LIS.getInterval(Edit->get(0));
856eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
85730815c53SDimitry Andric   RegAssignMap::iterator AssignI;
85830815c53SDimitry Andric   AssignI.setMap(RegAssign);
85930815c53SDimitry Andric 
860344a3780SDimitry Andric   for (const VNInfo *C : Copies) {
861344a3780SDimitry Andric     SlotIndex Def = C->def;
86230815c53SDimitry Andric     MachineInstr *MI = LIS.getInstructionFromIndex(Def);
86330815c53SDimitry Andric     assert(MI && "No instruction for back-copy");
86430815c53SDimitry Andric 
86530815c53SDimitry Andric     MachineBasicBlock *MBB = MI->getParent();
86630815c53SDimitry Andric     MachineBasicBlock::iterator MBBI(MI);
86730815c53SDimitry Andric     bool AtBegin;
86830815c53SDimitry Andric     do AtBegin = MBBI == MBB->begin();
869344a3780SDimitry Andric     while (!AtBegin && (--MBBI)->isDebugOrPseudoInstr());
87030815c53SDimitry Andric 
871eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
8725a5ac124SDimitry Andric     LIS.removeVRegDefAt(*LI, Def);
87301095a5dSDimitry Andric     LIS.RemoveMachineInstrFromMaps(*MI);
87430815c53SDimitry Andric     MI->eraseFromParent();
87530815c53SDimitry Andric 
8765a5ac124SDimitry Andric     // Adjust RegAssign if a register assignment is killed at Def. We want to
8775a5ac124SDimitry Andric     // avoid calculating the live range of the source register if possible.
87858b69754SDimitry Andric     AssignI.find(Def.getPrevSlot());
87930815c53SDimitry Andric     if (!AssignI.valid() || AssignI.start() >= Def)
88030815c53SDimitry Andric       continue;
88130815c53SDimitry Andric     // If MI doesn't kill the assigned register, just leave it.
88230815c53SDimitry Andric     if (AssignI.stop() != Def)
88330815c53SDimitry Andric       continue;
88430815c53SDimitry Andric     unsigned RegIdx = AssignI.value();
885344a3780SDimitry Andric     // We could hoist back-copy right after another back-copy. As a result
886344a3780SDimitry Andric     // MMBI points to copy instruction which is actually dead now.
887344a3780SDimitry Andric     // We cannot set its stop to MBBI which will be the same as start and
888344a3780SDimitry Andric     // interval does not support that.
889344a3780SDimitry Andric     SlotIndex Kill =
890344a3780SDimitry Andric         AtBegin ? SlotIndex() : LIS.getInstructionIndex(*MBBI).getRegSlot();
891344a3780SDimitry Andric     if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg()) ||
892344a3780SDimitry Andric         Kill <= AssignI.start()) {
893eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << "  cannot find simple kill of RegIdx " << RegIdx
894eb11fae6SDimitry Andric                         << '\n');
895eb11fae6SDimitry Andric       forceRecompute(RegIdx, *Edit->getParent().getVNInfoAt(Def));
89630815c53SDimitry Andric     } else {
897eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << "  move kill to " << Kill << '\t' << *MBBI);
89830815c53SDimitry Andric       AssignI.setStop(Kill);
89930815c53SDimitry Andric     }
90030815c53SDimitry Andric   }
90130815c53SDimitry Andric }
90230815c53SDimitry Andric 
90330815c53SDimitry Andric MachineBasicBlock*
findShallowDominator(MachineBasicBlock * MBB,MachineBasicBlock * DefMBB)90430815c53SDimitry Andric SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
90530815c53SDimitry Andric                                   MachineBasicBlock *DefMBB) {
90630815c53SDimitry Andric   if (MBB == DefMBB)
90730815c53SDimitry Andric     return MBB;
90830815c53SDimitry Andric   assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
90930815c53SDimitry Andric 
91030815c53SDimitry Andric   const MachineLoopInfo &Loops = SA.Loops;
91130815c53SDimitry Andric   const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
91230815c53SDimitry Andric   MachineDomTreeNode *DefDomNode = MDT[DefMBB];
91330815c53SDimitry Andric 
91430815c53SDimitry Andric   // Best candidate so far.
91530815c53SDimitry Andric   MachineBasicBlock *BestMBB = MBB;
916044eb2f6SDimitry Andric   unsigned BestDepth = std::numeric_limits<unsigned>::max();
91730815c53SDimitry Andric 
918044eb2f6SDimitry Andric   while (true) {
91930815c53SDimitry Andric     const MachineLoop *Loop = Loops.getLoopFor(MBB);
92030815c53SDimitry Andric 
92130815c53SDimitry Andric     // MBB isn't in a loop, it doesn't get any better.  All dominators have a
92230815c53SDimitry Andric     // higher frequency by definition.
92330815c53SDimitry Andric     if (!Loop) {
924eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
925eb11fae6SDimitry Andric                         << " dominates " << printMBBReference(*MBB)
926eb11fae6SDimitry Andric                         << " at depth 0\n");
92730815c53SDimitry Andric       return MBB;
92830815c53SDimitry Andric     }
92930815c53SDimitry Andric 
93030815c53SDimitry Andric     // We'll never be able to exit the DefLoop.
93130815c53SDimitry Andric     if (Loop == DefLoop) {
932eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
933eb11fae6SDimitry Andric                         << " dominates " << printMBBReference(*MBB)
934eb11fae6SDimitry Andric                         << " in the same loop\n");
93530815c53SDimitry Andric       return MBB;
93630815c53SDimitry Andric     }
93730815c53SDimitry Andric 
93830815c53SDimitry Andric     // Least busy dominator seen so far.
93930815c53SDimitry Andric     unsigned Depth = Loop->getLoopDepth();
94030815c53SDimitry Andric     if (Depth < BestDepth) {
94130815c53SDimitry Andric       BestMBB = MBB;
94230815c53SDimitry Andric       BestDepth = Depth;
943eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
944eb11fae6SDimitry Andric                         << " dominates " << printMBBReference(*MBB)
945eb11fae6SDimitry Andric                         << " at depth " << Depth << '\n');
94630815c53SDimitry Andric     }
94730815c53SDimitry Andric 
94830815c53SDimitry Andric     // Leave loop by going to the immediate dominator of the loop header.
94930815c53SDimitry Andric     // This is a bigger stride than simply walking up the dominator tree.
95030815c53SDimitry Andric     MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
95130815c53SDimitry Andric 
95230815c53SDimitry Andric     // Too far up the dominator tree?
95330815c53SDimitry Andric     if (!IDom || !MDT.dominates(DefDomNode, IDom))
95430815c53SDimitry Andric       return BestMBB;
95530815c53SDimitry Andric 
95630815c53SDimitry Andric     MBB = IDom->getBlock();
95730815c53SDimitry Andric   }
95830815c53SDimitry Andric }
95930815c53SDimitry Andric 
computeRedundantBackCopies(DenseSet<unsigned> & NotToHoistSet,SmallVectorImpl<VNInfo * > & BackCopies)96001095a5dSDimitry Andric void SplitEditor::computeRedundantBackCopies(
96101095a5dSDimitry Andric     DenseSet<unsigned> &NotToHoistSet, SmallVectorImpl<VNInfo *> &BackCopies) {
96201095a5dSDimitry Andric   LiveInterval *LI = &LIS.getInterval(Edit->get(0));
963145449b1SDimitry Andric   const LiveInterval *Parent = &Edit->getParent();
96401095a5dSDimitry Andric   SmallVector<SmallPtrSet<VNInfo *, 8>, 8> EqualVNs(Parent->getNumValNums());
96501095a5dSDimitry Andric   SmallPtrSet<VNInfo *, 8> DominatedVNIs;
96601095a5dSDimitry Andric 
96701095a5dSDimitry Andric   // Aggregate VNIs having the same value as ParentVNI.
96801095a5dSDimitry Andric   for (VNInfo *VNI : LI->valnos) {
96901095a5dSDimitry Andric     if (VNI->isUnused())
97001095a5dSDimitry Andric       continue;
97101095a5dSDimitry Andric     VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
97201095a5dSDimitry Andric     EqualVNs[ParentVNI->id].insert(VNI);
97301095a5dSDimitry Andric   }
97401095a5dSDimitry Andric 
97501095a5dSDimitry Andric   // For VNI aggregation of each ParentVNI, collect dominated, i.e.,
97601095a5dSDimitry Andric   // redundant VNIs to BackCopies.
97701095a5dSDimitry Andric   for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
978145449b1SDimitry Andric     const VNInfo *ParentVNI = Parent->getValNumInfo(i);
97901095a5dSDimitry Andric     if (!NotToHoistSet.count(ParentVNI->id))
98001095a5dSDimitry Andric       continue;
98101095a5dSDimitry Andric     SmallPtrSetIterator<VNInfo *> It1 = EqualVNs[ParentVNI->id].begin();
98201095a5dSDimitry Andric     SmallPtrSetIterator<VNInfo *> It2 = It1;
98301095a5dSDimitry Andric     for (; It1 != EqualVNs[ParentVNI->id].end(); ++It1) {
98401095a5dSDimitry Andric       It2 = It1;
98501095a5dSDimitry Andric       for (++It2; It2 != EqualVNs[ParentVNI->id].end(); ++It2) {
98601095a5dSDimitry Andric         if (DominatedVNIs.count(*It1) || DominatedVNIs.count(*It2))
98701095a5dSDimitry Andric           continue;
98801095a5dSDimitry Andric 
98901095a5dSDimitry Andric         MachineBasicBlock *MBB1 = LIS.getMBBFromIndex((*It1)->def);
99001095a5dSDimitry Andric         MachineBasicBlock *MBB2 = LIS.getMBBFromIndex((*It2)->def);
99101095a5dSDimitry Andric         if (MBB1 == MBB2) {
99201095a5dSDimitry Andric           DominatedVNIs.insert((*It1)->def < (*It2)->def ? (*It2) : (*It1));
99301095a5dSDimitry Andric         } else if (MDT.dominates(MBB1, MBB2)) {
99401095a5dSDimitry Andric           DominatedVNIs.insert(*It2);
99501095a5dSDimitry Andric         } else if (MDT.dominates(MBB2, MBB1)) {
99601095a5dSDimitry Andric           DominatedVNIs.insert(*It1);
99701095a5dSDimitry Andric         }
99801095a5dSDimitry Andric       }
99901095a5dSDimitry Andric     }
100001095a5dSDimitry Andric     if (!DominatedVNIs.empty()) {
1001eb11fae6SDimitry Andric       forceRecompute(0, *ParentVNI);
1002b60736ecSDimitry Andric       append_range(BackCopies, DominatedVNIs);
100301095a5dSDimitry Andric       DominatedVNIs.clear();
100401095a5dSDimitry Andric     }
100501095a5dSDimitry Andric   }
100601095a5dSDimitry Andric }
100701095a5dSDimitry Andric 
100801095a5dSDimitry Andric /// For SM_Size mode, find a common dominator for all the back-copies for
100901095a5dSDimitry Andric /// the same ParentVNI and hoist the backcopies to the dominator BB.
101001095a5dSDimitry Andric /// For SM_Speed mode, if the common dominator is hot and it is not beneficial
101101095a5dSDimitry Andric /// to do the hoisting, simply remove the dominated backcopies for the same
101201095a5dSDimitry Andric /// ParentVNI.
hoistCopies()101301095a5dSDimitry Andric void SplitEditor::hoistCopies() {
101430815c53SDimitry Andric   // Get the complement interval, always RegIdx 0.
1015f8af5cf6SDimitry Andric   LiveInterval *LI = &LIS.getInterval(Edit->get(0));
1016145449b1SDimitry Andric   const LiveInterval *Parent = &Edit->getParent();
101730815c53SDimitry Andric 
101830815c53SDimitry Andric   // Track the nearest common dominator for all back-copies for each ParentVNI,
101930815c53SDimitry Andric   // indexed by ParentVNI->id.
1020044eb2f6SDimitry Andric   using DomPair = std::pair<MachineBasicBlock *, SlotIndex>;
102130815c53SDimitry Andric   SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
102201095a5dSDimitry Andric   // The total cost of all the back-copies for each ParentVNI.
102301095a5dSDimitry Andric   SmallVector<BlockFrequency, 8> Costs(Parent->getNumValNums());
102401095a5dSDimitry Andric   // The ParentVNI->id set for which hoisting back-copies are not beneficial
102501095a5dSDimitry Andric   // for Speed.
102601095a5dSDimitry Andric   DenseSet<unsigned> NotToHoistSet;
102730815c53SDimitry Andric 
102830815c53SDimitry Andric   // Find the nearest common dominator for parent values with multiple
102930815c53SDimitry Andric   // back-copies.  If a single back-copy dominates, put it in DomPair.second.
103067c32a98SDimitry Andric   for (VNInfo *VNI : LI->valnos) {
103158b69754SDimitry Andric     if (VNI->isUnused())
103258b69754SDimitry Andric       continue;
103330815c53SDimitry Andric     VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
103430815c53SDimitry Andric     assert(ParentVNI && "Parent not live at complement def");
103530815c53SDimitry Andric 
103630815c53SDimitry Andric     // Don't hoist remats.  The complement is probably going to disappear
103730815c53SDimitry Andric     // completely anyway.
103830815c53SDimitry Andric     if (Edit->didRematerialize(ParentVNI))
103930815c53SDimitry Andric       continue;
104030815c53SDimitry Andric 
104130815c53SDimitry Andric     MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
104201095a5dSDimitry Andric 
104330815c53SDimitry Andric     DomPair &Dom = NearestDom[ParentVNI->id];
104430815c53SDimitry Andric 
104530815c53SDimitry Andric     // Keep directly defined parent values.  This is either a PHI or an
104630815c53SDimitry Andric     // instruction in the complement range.  All other copies of ParentVNI
104730815c53SDimitry Andric     // should be eliminated.
104830815c53SDimitry Andric     if (VNI->def == ParentVNI->def) {
1049eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
105030815c53SDimitry Andric       Dom = DomPair(ValMBB, VNI->def);
105130815c53SDimitry Andric       continue;
105230815c53SDimitry Andric     }
105330815c53SDimitry Andric     // Skip the singly mapped values.  There is nothing to gain from hoisting a
105430815c53SDimitry Andric     // single back-copy.
105530815c53SDimitry Andric     if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
1056eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
105730815c53SDimitry Andric       continue;
105830815c53SDimitry Andric     }
105930815c53SDimitry Andric 
106030815c53SDimitry Andric     if (!Dom.first) {
106130815c53SDimitry Andric       // First time we see ParentVNI.  VNI dominates itself.
106230815c53SDimitry Andric       Dom = DomPair(ValMBB, VNI->def);
106330815c53SDimitry Andric     } else if (Dom.first == ValMBB) {
106430815c53SDimitry Andric       // Two defs in the same block.  Pick the earlier def.
106530815c53SDimitry Andric       if (!Dom.second.isValid() || VNI->def < Dom.second)
106630815c53SDimitry Andric         Dom.second = VNI->def;
106730815c53SDimitry Andric     } else {
106830815c53SDimitry Andric       // Different basic blocks. Check if one dominates.
106930815c53SDimitry Andric       MachineBasicBlock *Near =
107030815c53SDimitry Andric         MDT.findNearestCommonDominator(Dom.first, ValMBB);
107130815c53SDimitry Andric       if (Near == ValMBB)
107230815c53SDimitry Andric         // Def ValMBB dominates.
107330815c53SDimitry Andric         Dom = DomPair(ValMBB, VNI->def);
107430815c53SDimitry Andric       else if (Near != Dom.first)
107530815c53SDimitry Andric         // None dominate. Hoist to common dominator, need new def.
107630815c53SDimitry Andric         Dom = DomPair(Near, SlotIndex());
107701095a5dSDimitry Andric       Costs[ParentVNI->id] += MBFI.getBlockFreq(ValMBB);
107830815c53SDimitry Andric     }
107930815c53SDimitry Andric 
1080eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@'
1081eb11fae6SDimitry Andric                       << VNI->def << " for parent " << ParentVNI->id << '@'
1082eb11fae6SDimitry Andric                       << ParentVNI->def << " hoist to "
1083eb11fae6SDimitry Andric                       << printMBBReference(*Dom.first) << ' ' << Dom.second
1084eb11fae6SDimitry Andric                       << '\n');
108530815c53SDimitry Andric   }
108630815c53SDimitry Andric 
108730815c53SDimitry Andric   // Insert the hoisted copies.
108830815c53SDimitry Andric   for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
108930815c53SDimitry Andric     DomPair &Dom = NearestDom[i];
109030815c53SDimitry Andric     if (!Dom.first || Dom.second.isValid())
109130815c53SDimitry Andric       continue;
109230815c53SDimitry Andric     // This value needs a hoisted copy inserted at the end of Dom.first.
1093145449b1SDimitry Andric     const VNInfo *ParentVNI = Parent->getValNumInfo(i);
109430815c53SDimitry Andric     MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
109530815c53SDimitry Andric     // Get a less loopy dominator than Dom.first.
109630815c53SDimitry Andric     Dom.first = findShallowDominator(Dom.first, DefMBB);
109701095a5dSDimitry Andric     if (SpillMode == SM_Speed &&
109801095a5dSDimitry Andric         MBFI.getBlockFreq(Dom.first) > Costs[ParentVNI->id]) {
109901095a5dSDimitry Andric       NotToHoistSet.insert(ParentVNI->id);
110001095a5dSDimitry Andric       continue;
110101095a5dSDimitry Andric     }
1102344a3780SDimitry Andric     SlotIndex LSP = SA.getLastSplitPoint(Dom.first);
1103344a3780SDimitry Andric     if (LSP <= ParentVNI->def) {
1104344a3780SDimitry Andric       NotToHoistSet.insert(ParentVNI->id);
1105344a3780SDimitry Andric       continue;
1106344a3780SDimitry Andric     }
1107344a3780SDimitry Andric     Dom.second = defFromParent(0, ParentVNI, LSP, *Dom.first,
110863faed5bSDimitry Andric                                SA.getLastSplitPointIter(Dom.first))->def;
110930815c53SDimitry Andric   }
111030815c53SDimitry Andric 
111130815c53SDimitry Andric   // Remove redundant back-copies that are now known to be dominated by another
111230815c53SDimitry Andric   // def with the same value.
111330815c53SDimitry Andric   SmallVector<VNInfo*, 8> BackCopies;
111467c32a98SDimitry Andric   for (VNInfo *VNI : LI->valnos) {
111558b69754SDimitry Andric     if (VNI->isUnused())
111658b69754SDimitry Andric       continue;
111730815c53SDimitry Andric     VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
111830815c53SDimitry Andric     const DomPair &Dom = NearestDom[ParentVNI->id];
111901095a5dSDimitry Andric     if (!Dom.first || Dom.second == VNI->def ||
112001095a5dSDimitry Andric         NotToHoistSet.count(ParentVNI->id))
112130815c53SDimitry Andric       continue;
112230815c53SDimitry Andric     BackCopies.push_back(VNI);
1123eb11fae6SDimitry Andric     forceRecompute(0, *ParentVNI);
112430815c53SDimitry Andric   }
112501095a5dSDimitry Andric 
112601095a5dSDimitry Andric   // If it is not beneficial to hoist all the BackCopies, simply remove
112701095a5dSDimitry Andric   // redundant BackCopies in speed mode.
112801095a5dSDimitry Andric   if (SpillMode == SM_Speed && !NotToHoistSet.empty())
112901095a5dSDimitry Andric     computeRedundantBackCopies(NotToHoistSet, BackCopies);
113001095a5dSDimitry Andric 
113130815c53SDimitry Andric   removeBackCopies(BackCopies);
113230815c53SDimitry Andric }
113330815c53SDimitry Andric 
11346b943ff3SDimitry Andric /// transferValues - Transfer all possible values to the new live ranges.
1135cfca06d7SDimitry Andric /// Values that were rematerialized are left alone, they need LICalc.extend().
transferValues()11366b943ff3SDimitry Andric bool SplitEditor::transferValues() {
11376b943ff3SDimitry Andric   bool Skipped = false;
11386b943ff3SDimitry Andric   RegAssignMap::const_iterator AssignI = RegAssign.begin();
113967c32a98SDimitry Andric   for (const LiveRange::Segment &S : Edit->getParent()) {
1140eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << "  blit " << S << ':');
114167c32a98SDimitry Andric     VNInfo *ParentVNI = S.valno;
11426b943ff3SDimitry Andric     // RegAssign has holes where RegIdx 0 should be used.
114367c32a98SDimitry Andric     SlotIndex Start = S.start;
11446b943ff3SDimitry Andric     AssignI.advanceTo(Start);
11456b943ff3SDimitry Andric     do {
11466b943ff3SDimitry Andric       unsigned RegIdx;
114767c32a98SDimitry Andric       SlotIndex End = S.end;
11486b943ff3SDimitry Andric       if (!AssignI.valid()) {
11496b943ff3SDimitry Andric         RegIdx = 0;
11506b943ff3SDimitry Andric       } else if (AssignI.start() <= Start) {
11516b943ff3SDimitry Andric         RegIdx = AssignI.value();
11526b943ff3SDimitry Andric         if (AssignI.stop() < End) {
11536b943ff3SDimitry Andric           End = AssignI.stop();
11546b943ff3SDimitry Andric           ++AssignI;
11556b943ff3SDimitry Andric         }
11566b943ff3SDimitry Andric       } else {
11576b943ff3SDimitry Andric         RegIdx = 0;
11586b943ff3SDimitry Andric         End = std::min(End, AssignI.start());
1159d39c594dSDimitry Andric       }
1160d39c594dSDimitry Andric 
11616b943ff3SDimitry Andric       // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
1162eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx << '('
1163eb11fae6SDimitry Andric                         << printReg(Edit->get(RegIdx)) << ')');
1164b915e9e0SDimitry Andric       LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
11656b943ff3SDimitry Andric 
11666b943ff3SDimitry Andric       // Check for a simply defined value that can be blitted directly.
116730815c53SDimitry Andric       ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
116830815c53SDimitry Andric       if (VNInfo *VNI = VFP.getPointer()) {
1169eb11fae6SDimitry Andric         LLVM_DEBUG(dbgs() << ':' << VNI->id);
1170b915e9e0SDimitry Andric         LI.addSegment(LiveInterval::Segment(Start, End, VNI));
11716b943ff3SDimitry Andric         Start = End;
11726b943ff3SDimitry Andric         continue;
11736b943ff3SDimitry Andric       }
11746b943ff3SDimitry Andric 
117530815c53SDimitry Andric       // Skip values with forced recomputation.
117630815c53SDimitry Andric       if (VFP.getInt()) {
1177eb11fae6SDimitry Andric         LLVM_DEBUG(dbgs() << "(recalc)");
11786b943ff3SDimitry Andric         Skipped = true;
11796b943ff3SDimitry Andric         Start = End;
11806b943ff3SDimitry Andric         continue;
11816b943ff3SDimitry Andric       }
11826b943ff3SDimitry Andric 
1183cfca06d7SDimitry Andric       LiveIntervalCalc &LIC = getLICalc(RegIdx);
11846b943ff3SDimitry Andric 
11856b943ff3SDimitry Andric       // This value has multiple defs in RegIdx, but it wasn't rematerialized,
11866b943ff3SDimitry Andric       // so the live range is accurate. Add live-in blocks in [Start;End) to the
11876b943ff3SDimitry Andric       // LiveInBlocks.
1188dd58ef01SDimitry Andric       MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
11896b943ff3SDimitry Andric       SlotIndex BlockStart, BlockEnd;
1190dd58ef01SDimitry Andric       std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(&*MBB);
11916b943ff3SDimitry Andric 
11926b943ff3SDimitry Andric       // The first block may be live-in, or it may have its own def.
11936b943ff3SDimitry Andric       if (Start != BlockStart) {
1194b915e9e0SDimitry Andric         VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
11956b943ff3SDimitry Andric         assert(VNI && "Missing def for complex mapped value");
1196eb11fae6SDimitry Andric         LLVM_DEBUG(dbgs() << ':' << VNI->id << "*" << printMBBReference(*MBB));
11976b943ff3SDimitry Andric         // MBB has its own def. Is it also live-out?
119830815c53SDimitry Andric         if (BlockEnd <= End)
1199cfca06d7SDimitry Andric           LIC.setLiveOutValue(&*MBB, VNI);
120030815c53SDimitry Andric 
12016b943ff3SDimitry Andric         // Skip to the next block for live-in.
12026b943ff3SDimitry Andric         ++MBB;
12036b943ff3SDimitry Andric         BlockStart = BlockEnd;
12046b943ff3SDimitry Andric       }
12056b943ff3SDimitry Andric 
12066b943ff3SDimitry Andric       // Handle the live-in blocks covered by [Start;End).
12076b943ff3SDimitry Andric       assert(Start <= BlockStart && "Expected live-in block");
12086b943ff3SDimitry Andric       while (BlockStart < End) {
1209eb11fae6SDimitry Andric         LLVM_DEBUG(dbgs() << ">" << printMBBReference(*MBB));
1210dd58ef01SDimitry Andric         BlockEnd = LIS.getMBBEndIdx(&*MBB);
12116b943ff3SDimitry Andric         if (BlockStart == ParentVNI->def) {
12126b943ff3SDimitry Andric           // This block has the def of a parent PHI, so it isn't live-in.
12136b943ff3SDimitry Andric           assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
1214b915e9e0SDimitry Andric           VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
12156b943ff3SDimitry Andric           assert(VNI && "Missing def for complex mapped parent PHI");
121630815c53SDimitry Andric           if (End >= BlockEnd)
1217cfca06d7SDimitry Andric             LIC.setLiveOutValue(&*MBB, VNI); // Live-out as well.
12186b943ff3SDimitry Andric         } else {
121930815c53SDimitry Andric           // This block needs a live-in value.  The last block covered may not
122030815c53SDimitry Andric           // be live-out.
12216b943ff3SDimitry Andric           if (End < BlockEnd)
1222cfca06d7SDimitry Andric             LIC.addLiveInBlock(LI, MDT[&*MBB], End);
12236b943ff3SDimitry Andric           else {
122430815c53SDimitry Andric             // Live-through, and we don't know the value.
1225cfca06d7SDimitry Andric             LIC.addLiveInBlock(LI, MDT[&*MBB]);
1226cfca06d7SDimitry Andric             LIC.setLiveOutValue(&*MBB, nullptr);
12276b943ff3SDimitry Andric           }
12286b943ff3SDimitry Andric         }
12296b943ff3SDimitry Andric         BlockStart = BlockEnd;
12306b943ff3SDimitry Andric         ++MBB;
12316b943ff3SDimitry Andric       }
12326b943ff3SDimitry Andric       Start = End;
123367c32a98SDimitry Andric     } while (Start != S.end);
1234eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << '\n');
12356b943ff3SDimitry Andric   }
12366b943ff3SDimitry Andric 
1237cfca06d7SDimitry Andric   LICalc[0].calculateValues();
123830815c53SDimitry Andric   if (SpillMode)
1239cfca06d7SDimitry Andric     LICalc[1].calculateValues();
12406b943ff3SDimitry Andric 
12416b943ff3SDimitry Andric   return Skipped;
12426b943ff3SDimitry Andric }
12436b943ff3SDimitry Andric 
removeDeadSegment(SlotIndex Def,LiveRange & LR)1244b915e9e0SDimitry Andric static bool removeDeadSegment(SlotIndex Def, LiveRange &LR) {
1245b915e9e0SDimitry Andric   const LiveRange::Segment *Seg = LR.getSegmentContaining(Def);
1246b915e9e0SDimitry Andric   if (Seg == nullptr)
1247b915e9e0SDimitry Andric     return true;
1248b915e9e0SDimitry Andric   if (Seg->end != Def.getDeadSlot())
1249b915e9e0SDimitry Andric     return false;
125001095a5dSDimitry Andric   // This is a dead PHI. Remove it.
1251b915e9e0SDimitry Andric   LR.removeSegment(*Seg, true);
1252b915e9e0SDimitry Andric   return true;
125301095a5dSDimitry Andric }
125401095a5dSDimitry Andric 
extendPHIRange(MachineBasicBlock & B,LiveIntervalCalc & LIC,LiveRange & LR,LaneBitmask LM,ArrayRef<SlotIndex> Undefs)1255cfca06d7SDimitry Andric void SplitEditor::extendPHIRange(MachineBasicBlock &B, LiveIntervalCalc &LIC,
1256b915e9e0SDimitry Andric                                  LiveRange &LR, LaneBitmask LM,
1257b915e9e0SDimitry Andric                                  ArrayRef<SlotIndex> Undefs) {
1258b915e9e0SDimitry Andric   for (MachineBasicBlock *P : B.predecessors()) {
1259b915e9e0SDimitry Andric     SlotIndex End = LIS.getMBBEndIdx(P);
126030815c53SDimitry Andric     SlotIndex LastUse = End.getPrevSlot();
12616b943ff3SDimitry Andric     // The predecessor may not have a live-out value. That is OK, like an
12626b943ff3SDimitry Andric     // undef PHI operand.
1263145449b1SDimitry Andric     const LiveInterval &PLI = Edit->getParent();
1264b915e9e0SDimitry Andric     // Need the cast because the inputs to ?: would otherwise be deemed
1265b915e9e0SDimitry Andric     // "incompatible": SubRange vs LiveInterval.
1266145449b1SDimitry Andric     const LiveRange &PSR = !LM.all() ? getSubRangeForMaskExact(LM, PLI)
1267145449b1SDimitry Andric                                      : static_cast<const LiveRange &>(PLI);
1268b915e9e0SDimitry Andric     if (PSR.liveAt(LastUse))
1269cfca06d7SDimitry Andric       LIC.extend(LR, End, /*PhysReg=*/0, Undefs);
12706b943ff3SDimitry Andric   }
12716b943ff3SDimitry Andric }
1272b915e9e0SDimitry Andric 
extendPHIKillRanges()1273b915e9e0SDimitry Andric void SplitEditor::extendPHIKillRanges() {
1274b915e9e0SDimitry Andric   // Extend live ranges to be live-out for successor PHI values.
1275b915e9e0SDimitry Andric 
1276b915e9e0SDimitry Andric   // Visit each PHI def slot in the parent live interval. If the def is dead,
1277b915e9e0SDimitry Andric   // remove it. Otherwise, extend the live interval to reach the end indexes
1278b915e9e0SDimitry Andric   // of all predecessor blocks.
1279b915e9e0SDimitry Andric 
1280145449b1SDimitry Andric   const LiveInterval &ParentLI = Edit->getParent();
1281b915e9e0SDimitry Andric   for (const VNInfo *V : ParentLI.valnos) {
1282b915e9e0SDimitry Andric     if (V->isUnused() || !V->isPHIDef())
1283b915e9e0SDimitry Andric       continue;
1284b915e9e0SDimitry Andric 
1285b915e9e0SDimitry Andric     unsigned RegIdx = RegAssign.lookup(V->def);
1286b915e9e0SDimitry Andric     LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1287cfca06d7SDimitry Andric     LiveIntervalCalc &LIC = getLICalc(RegIdx);
1288b915e9e0SDimitry Andric     MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
1289b915e9e0SDimitry Andric     if (!removeDeadSegment(V->def, LI))
1290cfca06d7SDimitry Andric       extendPHIRange(B, LIC, LI, LaneBitmask::getAll(), /*Undefs=*/{});
1291b915e9e0SDimitry Andric   }
1292b915e9e0SDimitry Andric 
1293b915e9e0SDimitry Andric   SmallVector<SlotIndex, 4> Undefs;
1294cfca06d7SDimitry Andric   LiveIntervalCalc SubLIC;
1295b915e9e0SDimitry Andric 
1296145449b1SDimitry Andric   for (const LiveInterval::SubRange &PS : ParentLI.subranges()) {
1297b915e9e0SDimitry Andric     for (const VNInfo *V : PS.valnos) {
1298b915e9e0SDimitry Andric       if (V->isUnused() || !V->isPHIDef())
1299b915e9e0SDimitry Andric         continue;
1300b915e9e0SDimitry Andric       unsigned RegIdx = RegAssign.lookup(V->def);
1301b915e9e0SDimitry Andric       LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1302b60736ecSDimitry Andric       LiveInterval::SubRange &S = getSubRangeForMaskExact(PS.LaneMask, LI);
1303b915e9e0SDimitry Andric       if (removeDeadSegment(V->def, S))
1304b915e9e0SDimitry Andric         continue;
1305b915e9e0SDimitry Andric 
1306b915e9e0SDimitry Andric       MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
1307cfca06d7SDimitry Andric       SubLIC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
1308b915e9e0SDimitry Andric                    &LIS.getVNInfoAllocator());
1309b915e9e0SDimitry Andric       Undefs.clear();
1310b915e9e0SDimitry Andric       LI.computeSubRangeUndefs(Undefs, PS.LaneMask, MRI, *LIS.getSlotIndexes());
1311cfca06d7SDimitry Andric       extendPHIRange(B, SubLIC, S, PS.LaneMask, Undefs);
1312b915e9e0SDimitry Andric     }
13136b943ff3SDimitry Andric   }
13146b943ff3SDimitry Andric }
13156b943ff3SDimitry Andric 
13166b943ff3SDimitry Andric /// rewriteAssigned - Rewrite all uses of Edit->getReg().
rewriteAssigned(bool ExtendRanges)13176b943ff3SDimitry Andric void SplitEditor::rewriteAssigned(bool ExtendRanges) {
1318b915e9e0SDimitry Andric   struct ExtPoint {
1319b915e9e0SDimitry Andric     ExtPoint(const MachineOperand &O, unsigned R, SlotIndex N)
1320b915e9e0SDimitry Andric       : MO(O), RegIdx(R), Next(N) {}
1321044eb2f6SDimitry Andric 
1322b915e9e0SDimitry Andric     MachineOperand MO;
1323b915e9e0SDimitry Andric     unsigned RegIdx;
1324b915e9e0SDimitry Andric     SlotIndex Next;
1325b915e9e0SDimitry Andric   };
1326b915e9e0SDimitry Andric 
1327b915e9e0SDimitry Andric   SmallVector<ExtPoint,4> ExtPoints;
1328b915e9e0SDimitry Andric 
1329344a3780SDimitry Andric   for (MachineOperand &MO :
1330344a3780SDimitry Andric        llvm::make_early_inc_range(MRI.reg_operands(Edit->getReg()))) {
1331d39c594dSDimitry Andric     MachineInstr *MI = MO.getParent();
1332cf099d11SDimitry Andric     // LiveDebugVariables should have handled all DBG_VALUE instructions.
1333d39c594dSDimitry Andric     if (MI->isDebugValue()) {
1334eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << "Zapping " << *MI);
1335d39c594dSDimitry Andric       MO.setReg(0);
1336d39c594dSDimitry Andric       continue;
1337d39c594dSDimitry Andric     }
1338cf099d11SDimitry Andric 
133930815c53SDimitry Andric     // <undef> operands don't really read the register, so it doesn't matter
134030815c53SDimitry Andric     // which register we choose.  When the use operand is tied to a def, we must
134130815c53SDimitry Andric     // use the same register as the def, so just do that always.
134201095a5dSDimitry Andric     SlotIndex Idx = LIS.getInstructionIndex(*MI);
134330815c53SDimitry Andric     if (MO.isDef() || MO.isUndef())
134463faed5bSDimitry Andric       Idx = Idx.getRegSlot(MO.isEarlyClobber());
1345cf099d11SDimitry Andric 
1346cf099d11SDimitry Andric     // Rewrite to the mapped register at Idx.
1347cf099d11SDimitry Andric     unsigned RegIdx = RegAssign.lookup(Idx);
1348b915e9e0SDimitry Andric     LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1349b60736ecSDimitry Andric     MO.setReg(LI.reg());
1350eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << "  rewr " << printMBBReference(*MI->getParent())
1351eb11fae6SDimitry Andric                       << '\t' << Idx << ':' << RegIdx << '\t' << *MI);
1352cf099d11SDimitry Andric 
13536b943ff3SDimitry Andric     // Extend liveness to Idx if the instruction reads reg.
135430815c53SDimitry Andric     if (!ExtendRanges || MO.isUndef())
1355cf099d11SDimitry Andric       continue;
13566b943ff3SDimitry Andric 
13576b943ff3SDimitry Andric     // Skip instructions that don't read Reg.
13586b943ff3SDimitry Andric     if (MO.isDef()) {
13596b943ff3SDimitry Andric       if (!MO.getSubReg() && !MO.isEarlyClobber())
13606b943ff3SDimitry Andric         continue;
1361b915e9e0SDimitry Andric       // We may want to extend a live range for a partial redef, or for a use
13626b943ff3SDimitry Andric       // tied to an early clobber.
1363145449b1SDimitry Andric       if (!Edit->getParent().liveAt(Idx.getPrevSlot()))
13646b943ff3SDimitry Andric         continue;
1365145449b1SDimitry Andric     } else {
1366145449b1SDimitry Andric       assert(MO.isUse());
1367145449b1SDimitry Andric       bool IsEarlyClobber = false;
1368145449b1SDimitry Andric       if (MO.isTied()) {
1369145449b1SDimitry Andric         // We want to extend a live range into `e` slot rather than `r` slot if
1370145449b1SDimitry Andric         // tied-def is early clobber, because the `e` slot already contained
1371145449b1SDimitry Andric         // in the live range of early-clobber tied-def operand, give an example
1372145449b1SDimitry Andric         // here:
1373145449b1SDimitry Andric         //  0  %0 = ...
1374145449b1SDimitry Andric         // 16  early-clobber %0 = Op %0 (tied-def 0), ...
1375145449b1SDimitry Andric         // 32  ... = Op %0
1376145449b1SDimitry Andric         // Before extend:
1377145449b1SDimitry Andric         //   %0 = [0r, 0d) [16e, 32d)
1378145449b1SDimitry Andric         // The point we want to extend is 0d to 16e not 16r in this case, but if
1379145449b1SDimitry Andric         // we use 16r here we will extend nothing because that already contained
1380145449b1SDimitry Andric         // in [16e, 32d).
13817fa27ce4SDimitry Andric         unsigned OpIdx = MO.getOperandNo();
1382145449b1SDimitry Andric         unsigned DefOpIdx = MI->findTiedOperandIdx(OpIdx);
1383145449b1SDimitry Andric         const MachineOperand &DefOp = MI->getOperand(DefOpIdx);
1384145449b1SDimitry Andric         IsEarlyClobber = DefOp.isEarlyClobber();
1385145449b1SDimitry Andric       }
13866b943ff3SDimitry Andric 
1387145449b1SDimitry Andric       Idx = Idx.getRegSlot(IsEarlyClobber);
1388145449b1SDimitry Andric     }
1389145449b1SDimitry Andric 
1390145449b1SDimitry Andric     SlotIndex Next = Idx;
1391b915e9e0SDimitry Andric     if (LI.hasSubRanges()) {
1392b915e9e0SDimitry Andric       // We have to delay extending subranges until we have seen all operands
1393b915e9e0SDimitry Andric       // defining the register. This is because a <def,read-undef> operand
1394b915e9e0SDimitry Andric       // will create an "undef" point, and we cannot extend any subranges
1395b915e9e0SDimitry Andric       // until all of them have been accounted for.
1396b915e9e0SDimitry Andric       if (MO.isUse())
1397b915e9e0SDimitry Andric         ExtPoints.push_back(ExtPoint(MO, RegIdx, Next));
1398b915e9e0SDimitry Andric     } else {
1399cfca06d7SDimitry Andric       LiveIntervalCalc &LIC = getLICalc(RegIdx);
1400cfca06d7SDimitry Andric       LIC.extend(LI, Next, 0, ArrayRef<SlotIndex>());
1401b915e9e0SDimitry Andric     }
1402b915e9e0SDimitry Andric   }
1403b915e9e0SDimitry Andric 
1404b915e9e0SDimitry Andric   for (ExtPoint &EP : ExtPoints) {
1405b915e9e0SDimitry Andric     LiveInterval &LI = LIS.getInterval(Edit->get(EP.RegIdx));
1406b915e9e0SDimitry Andric     assert(LI.hasSubRanges());
1407b915e9e0SDimitry Andric 
1408cfca06d7SDimitry Andric     LiveIntervalCalc SubLIC;
14091d5ae102SDimitry Andric     Register Reg = EP.MO.getReg(), Sub = EP.MO.getSubReg();
1410b915e9e0SDimitry Andric     LaneBitmask LM = Sub != 0 ? TRI.getSubRegIndexLaneMask(Sub)
1411b915e9e0SDimitry Andric                               : MRI.getMaxLaneMaskForVReg(Reg);
1412b915e9e0SDimitry Andric     for (LiveInterval::SubRange &S : LI.subranges()) {
1413b915e9e0SDimitry Andric       if ((S.LaneMask & LM).none())
1414b915e9e0SDimitry Andric         continue;
1415b915e9e0SDimitry Andric       // The problem here can be that the new register may have been created
1416b915e9e0SDimitry Andric       // for a partially defined original register. For example:
1417044eb2f6SDimitry Andric       //   %0:subreg_hireg<def,read-undef> = ...
1418b915e9e0SDimitry Andric       //   ...
1419044eb2f6SDimitry Andric       //   %1 = COPY %0
1420b915e9e0SDimitry Andric       if (S.empty())
1421b915e9e0SDimitry Andric         continue;
1422cfca06d7SDimitry Andric       SubLIC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
1423b915e9e0SDimitry Andric                    &LIS.getVNInfoAllocator());
1424b915e9e0SDimitry Andric       SmallVector<SlotIndex, 4> Undefs;
1425b915e9e0SDimitry Andric       LI.computeSubRangeUndefs(Undefs, S.LaneMask, MRI, *LIS.getSlotIndexes());
1426cfca06d7SDimitry Andric       SubLIC.extend(S, EP.Next, 0, Undefs);
1427b915e9e0SDimitry Andric     }
1428b915e9e0SDimitry Andric   }
1429b915e9e0SDimitry Andric 
1430b60736ecSDimitry Andric   for (Register R : *Edit) {
1431b915e9e0SDimitry Andric     LiveInterval &LI = LIS.getInterval(R);
1432b915e9e0SDimitry Andric     if (!LI.hasSubRanges())
1433b915e9e0SDimitry Andric       continue;
1434b915e9e0SDimitry Andric     LI.clear();
1435b915e9e0SDimitry Andric     LI.removeEmptySubRanges();
1436b915e9e0SDimitry Andric     LIS.constructMainRangeFromSubranges(LI);
1437d39c594dSDimitry Andric   }
1438d39c594dSDimitry Andric }
1439d39c594dSDimitry Andric 
deleteRematVictims()14406b943ff3SDimitry Andric void SplitEditor::deleteRematVictims() {
14416b943ff3SDimitry Andric   SmallVector<MachineInstr*, 8> Dead;
1442344a3780SDimitry Andric   for (const Register &R : *Edit) {
1443344a3780SDimitry Andric     LiveInterval *LI = &LIS.getInterval(R);
144467c32a98SDimitry Andric     for (const LiveRange::Segment &S : LI->segments) {
144563faed5bSDimitry Andric       // Dead defs end at the dead slot.
144667c32a98SDimitry Andric       if (S.end != S.valno->def.getDeadSlot())
14476b943ff3SDimitry Andric         continue;
144801095a5dSDimitry Andric       if (S.valno->isPHIDef())
144901095a5dSDimitry Andric         continue;
145067c32a98SDimitry Andric       MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def);
14516b943ff3SDimitry Andric       assert(MI && "Missing instruction for dead def");
1452b60736ecSDimitry Andric       MI->addRegisterDead(LI->reg(), &TRI);
14536b943ff3SDimitry Andric 
14546b943ff3SDimitry Andric       if (!MI->allDefsAreDead())
14556b943ff3SDimitry Andric         continue;
14566b943ff3SDimitry Andric 
1457eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << "All defs dead: " << *MI);
14586b943ff3SDimitry Andric       Dead.push_back(MI);
14596b943ff3SDimitry Andric     }
14606b943ff3SDimitry Andric   }
14616b943ff3SDimitry Andric 
14626b943ff3SDimitry Andric   if (Dead.empty())
14636b943ff3SDimitry Andric     return;
14646b943ff3SDimitry Andric 
1465e3b55780SDimitry Andric   Edit->eliminateDeadDefs(Dead, std::nullopt);
14666b943ff3SDimitry Andric }
14676b943ff3SDimitry Andric 
forceRecomputeVNI(const VNInfo & ParentVNI)1468eb11fae6SDimitry Andric void SplitEditor::forceRecomputeVNI(const VNInfo &ParentVNI) {
1469eb11fae6SDimitry Andric   // Fast-path for common case.
1470eb11fae6SDimitry Andric   if (!ParentVNI.isPHIDef()) {
1471eb11fae6SDimitry Andric     for (unsigned I = 0, E = Edit->size(); I != E; ++I)
1472eb11fae6SDimitry Andric       forceRecompute(I, ParentVNI);
1473eb11fae6SDimitry Andric     return;
1474eb11fae6SDimitry Andric   }
1475eb11fae6SDimitry Andric 
1476eb11fae6SDimitry Andric   // Trace value through phis.
1477eb11fae6SDimitry Andric   SmallPtrSet<const VNInfo *, 8> Visited; ///< whether VNI was/is in worklist.
1478eb11fae6SDimitry Andric   SmallVector<const VNInfo *, 4> WorkList;
1479eb11fae6SDimitry Andric   Visited.insert(&ParentVNI);
1480eb11fae6SDimitry Andric   WorkList.push_back(&ParentVNI);
1481eb11fae6SDimitry Andric 
1482eb11fae6SDimitry Andric   const LiveInterval &ParentLI = Edit->getParent();
1483eb11fae6SDimitry Andric   const SlotIndexes &Indexes = *LIS.getSlotIndexes();
1484eb11fae6SDimitry Andric   do {
1485eb11fae6SDimitry Andric     const VNInfo &VNI = *WorkList.back();
1486eb11fae6SDimitry Andric     WorkList.pop_back();
1487eb11fae6SDimitry Andric     for (unsigned I = 0, E = Edit->size(); I != E; ++I)
1488eb11fae6SDimitry Andric       forceRecompute(I, VNI);
1489eb11fae6SDimitry Andric     if (!VNI.isPHIDef())
1490eb11fae6SDimitry Andric       continue;
1491eb11fae6SDimitry Andric 
1492eb11fae6SDimitry Andric     MachineBasicBlock &MBB = *Indexes.getMBBFromIndex(VNI.def);
1493eb11fae6SDimitry Andric     for (const MachineBasicBlock *Pred : MBB.predecessors()) {
1494eb11fae6SDimitry Andric       SlotIndex PredEnd = Indexes.getMBBEndIdx(Pred);
1495eb11fae6SDimitry Andric       VNInfo *PredVNI = ParentLI.getVNInfoBefore(PredEnd);
1496eb11fae6SDimitry Andric       assert(PredVNI && "Value available in PhiVNI predecessor");
1497eb11fae6SDimitry Andric       if (Visited.insert(PredVNI).second)
1498eb11fae6SDimitry Andric         WorkList.push_back(PredVNI);
1499eb11fae6SDimitry Andric     }
1500eb11fae6SDimitry Andric   } while(!WorkList.empty());
1501eb11fae6SDimitry Andric }
1502eb11fae6SDimitry Andric 
finish(SmallVectorImpl<unsigned> * LRMap)15036b943ff3SDimitry Andric void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
15046b943ff3SDimitry Andric   ++NumFinished;
1505cf099d11SDimitry Andric 
1506cf099d11SDimitry Andric   // At this point, the live intervals in Edit contain VNInfos corresponding to
1507cf099d11SDimitry Andric   // the inserted copies.
1508cf099d11SDimitry Andric 
1509cf099d11SDimitry Andric   // Add the original defs from the parent interval.
151067c32a98SDimitry Andric   for (const VNInfo *ParentVNI : Edit->getParent().valnos) {
1511cf099d11SDimitry Andric     if (ParentVNI->isUnused())
1512cf099d11SDimitry Andric       continue;
15136b943ff3SDimitry Andric     unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
1514b915e9e0SDimitry Andric     defValue(RegIdx, ParentVNI, ParentVNI->def, true);
15156b943ff3SDimitry Andric 
151630815c53SDimitry Andric     // Force rematted values to be recomputed everywhere.
15176b943ff3SDimitry Andric     // The new live ranges may be truncated.
15186b943ff3SDimitry Andric     if (Edit->didRematerialize(ParentVNI))
1519eb11fae6SDimitry Andric       forceRecomputeVNI(*ParentVNI);
152030815c53SDimitry Andric   }
152130815c53SDimitry Andric 
152230815c53SDimitry Andric   // Hoist back-copies to the complement interval when in spill mode.
152330815c53SDimitry Andric   switch (SpillMode) {
152430815c53SDimitry Andric   case SM_Partition:
152530815c53SDimitry Andric     // Leave all back-copies as is.
152630815c53SDimitry Andric     break;
152730815c53SDimitry Andric   case SM_Size:
152830815c53SDimitry Andric   case SM_Speed:
152901095a5dSDimitry Andric     // hoistCopies will behave differently between size and speed.
153001095a5dSDimitry Andric     hoistCopies();
1531cf099d11SDimitry Andric   }
1532cf099d11SDimitry Andric 
15336b943ff3SDimitry Andric   // Transfer the simply mapped values, check if any are skipped.
15346b943ff3SDimitry Andric   bool Skipped = transferValues();
153501095a5dSDimitry Andric 
153601095a5dSDimitry Andric   // Rewrite virtual registers, possibly extending ranges.
153701095a5dSDimitry Andric   rewriteAssigned(Skipped);
153801095a5dSDimitry Andric 
15396b943ff3SDimitry Andric   if (Skipped)
15406b943ff3SDimitry Andric     extendPHIKillRanges();
1541cf099d11SDimitry Andric   else
15426b943ff3SDimitry Andric     ++NumSimple;
1543cf099d11SDimitry Andric 
15446b943ff3SDimitry Andric   // Delete defs that were rematted everywhere.
15456b943ff3SDimitry Andric   if (Skipped)
15466b943ff3SDimitry Andric     deleteRematVictims();
1547cf099d11SDimitry Andric 
1548cf099d11SDimitry Andric   // Get rid of unused values and set phi-kill flags.
1549b60736ecSDimitry Andric   for (Register Reg : *Edit) {
1550b915e9e0SDimitry Andric     LiveInterval &LI = LIS.getInterval(Reg);
1551b915e9e0SDimitry Andric     LI.removeEmptySubRanges();
1552f8af5cf6SDimitry Andric     LI.RenumberValues();
1553f8af5cf6SDimitry Andric   }
1554cf099d11SDimitry Andric 
15556b943ff3SDimitry Andric   // Provide a reverse mapping from original indices to Edit ranges.
15566b943ff3SDimitry Andric   if (LRMap) {
1557145449b1SDimitry Andric     auto Seq = llvm::seq<unsigned>(0, Edit->size());
1558145449b1SDimitry Andric     LRMap->assign(Seq.begin(), Seq.end());
15596b943ff3SDimitry Andric   }
15606b943ff3SDimitry Andric 
1561cf099d11SDimitry Andric   // Now check if any registers were separated into multiple components.
1562cf099d11SDimitry Andric   ConnectedVNInfoEqClasses ConEQ(LIS);
15636b943ff3SDimitry Andric   for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
1564cf099d11SDimitry Andric     // Don't use iterators, they are invalidated by create() below.
1565b60736ecSDimitry Andric     Register VReg = Edit->get(i);
1566dd58ef01SDimitry Andric     LiveInterval &LI = LIS.getInterval(VReg);
1567dd58ef01SDimitry Andric     SmallVector<LiveInterval*, 8> SplitLIs;
1568dd58ef01SDimitry Andric     LIS.splitSeparateComponents(LI, SplitLIs);
1569b60736ecSDimitry Andric     Register Original = VRM.getOriginal(VReg);
1570dd58ef01SDimitry Andric     for (LiveInterval *SplitLI : SplitLIs)
1571b60736ecSDimitry Andric       VRM.setIsSplitFromReg(SplitLI->reg(), Original);
1572dd58ef01SDimitry Andric 
15736b943ff3SDimitry Andric     // The new intervals all map back to i.
15746b943ff3SDimitry Andric     if (LRMap)
15756b943ff3SDimitry Andric       LRMap->resize(Edit->size(), i);
1576cf099d11SDimitry Andric   }
1577cf099d11SDimitry Andric 
1578d39c594dSDimitry Andric   // Calculate spill weight and allocation hints for new intervals.
1579344a3780SDimitry Andric   Edit->calculateRegClassAndHint(VRM.getMachineFunction(), VRAI);
15806b943ff3SDimitry Andric 
15816b943ff3SDimitry Andric   assert(!LRMap || LRMap->size() == Edit->size());
1582d39c594dSDimitry Andric }
1583d39c594dSDimitry Andric 
1584d39c594dSDimitry Andric //===----------------------------------------------------------------------===//
1585d39c594dSDimitry Andric //                            Single Block Splitting
1586d39c594dSDimitry Andric //===----------------------------------------------------------------------===//
1587d39c594dSDimitry Andric 
shouldSplitSingleBlock(const BlockInfo & BI,bool SingleInstrs) const158830815c53SDimitry Andric bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
158930815c53SDimitry Andric                                            bool SingleInstrs) const {
159030815c53SDimitry Andric   // Always split for multiple instructions.
159130815c53SDimitry Andric   if (!BI.isOneInstr())
159230815c53SDimitry Andric     return true;
159330815c53SDimitry Andric   // Don't split for single instructions unless explicitly requested.
159430815c53SDimitry Andric   if (!SingleInstrs)
1595cf099d11SDimitry Andric     return false;
159630815c53SDimitry Andric   // Splitting a live-through range always makes progress.
159730815c53SDimitry Andric   if (BI.LiveIn && BI.LiveOut)
159830815c53SDimitry Andric     return true;
159930815c53SDimitry Andric   // No point in isolating a copy. It has no register class constraints.
16007fa27ce4SDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(BI.FirstInstr);
16017fa27ce4SDimitry Andric   bool copyLike = TII.isCopyInstr(*MI) || MI->isSubregToReg();
16027fa27ce4SDimitry Andric   if (copyLike)
160330815c53SDimitry Andric     return false;
160430815c53SDimitry Andric   // Finally, don't isolate an end point that was created by earlier splits.
160530815c53SDimitry Andric   return isOriginalEndpoint(BI.FirstInstr);
1606d39c594dSDimitry Andric }
1607d39c594dSDimitry Andric 
splitSingleBlock(const SplitAnalysis::BlockInfo & BI)16086b943ff3SDimitry Andric void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
16096b943ff3SDimitry Andric   openIntv();
1610344a3780SDimitry Andric   SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB);
161130815c53SDimitry Andric   SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
16126b943ff3SDimitry Andric     LastSplitPoint));
161330815c53SDimitry Andric   if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
161430815c53SDimitry Andric     useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
16156b943ff3SDimitry Andric   } else {
16166b943ff3SDimitry Andric       // The last use is after the last valid split point.
16176b943ff3SDimitry Andric     SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
16186b943ff3SDimitry Andric     useIntv(SegStart, SegStop);
161930815c53SDimitry Andric     overlapIntv(SegStop, BI.LastInstr);
16206b943ff3SDimitry Andric   }
16216b943ff3SDimitry Andric }
16226b943ff3SDimitry Andric 
1623411bd29eSDimitry Andric //===----------------------------------------------------------------------===//
1624411bd29eSDimitry Andric //                    Global Live Range Splitting Support
1625411bd29eSDimitry Andric //===----------------------------------------------------------------------===//
1626411bd29eSDimitry Andric 
1627411bd29eSDimitry Andric // These methods support a method of global live range splitting that uses a
1628411bd29eSDimitry Andric // global algorithm to decide intervals for CFG edges. They will insert split
1629411bd29eSDimitry Andric // points and color intervals in basic blocks while avoiding interference.
1630411bd29eSDimitry Andric //
1631411bd29eSDimitry Andric // Note that splitSingleBlock is also useful for blocks where both CFG edges
1632411bd29eSDimitry Andric // are on the stack.
1633411bd29eSDimitry Andric 
splitLiveThroughBlock(unsigned MBBNum,unsigned IntvIn,SlotIndex LeaveBefore,unsigned IntvOut,SlotIndex EnterAfter)1634411bd29eSDimitry Andric void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
1635411bd29eSDimitry Andric                                         unsigned IntvIn, SlotIndex LeaveBefore,
1636411bd29eSDimitry Andric                                         unsigned IntvOut, SlotIndex EnterAfter){
1637411bd29eSDimitry Andric   SlotIndex Start, Stop;
16385ca98fd9SDimitry Andric   std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
1639411bd29eSDimitry Andric 
1640eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "%bb." << MBBNum << " [" << Start << ';' << Stop
1641eb11fae6SDimitry Andric                     << ") intf " << LeaveBefore << '-' << EnterAfter
1642eb11fae6SDimitry Andric                     << ", live-through " << IntvIn << " -> " << IntvOut);
1643411bd29eSDimitry Andric 
1644411bd29eSDimitry Andric   assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
1645411bd29eSDimitry Andric 
164630815c53SDimitry Andric   assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
164730815c53SDimitry Andric   assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
164830815c53SDimitry Andric   assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
164930815c53SDimitry Andric 
165030815c53SDimitry Andric   MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
165130815c53SDimitry Andric 
1652411bd29eSDimitry Andric   if (!IntvOut) {
1653eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << ", spill on entry.\n");
1654411bd29eSDimitry Andric     //
1655411bd29eSDimitry Andric     //        <<<<<<<<<    Possible LeaveBefore interference.
1656411bd29eSDimitry Andric     //    |-----------|    Live through.
1657411bd29eSDimitry Andric     //    -____________    Spill on entry.
1658411bd29eSDimitry Andric     //
1659411bd29eSDimitry Andric     selectIntv(IntvIn);
1660411bd29eSDimitry Andric     SlotIndex Idx = leaveIntvAtTop(*MBB);
1661411bd29eSDimitry Andric     assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1662411bd29eSDimitry Andric     (void)Idx;
1663411bd29eSDimitry Andric     return;
1664411bd29eSDimitry Andric   }
1665411bd29eSDimitry Andric 
1666411bd29eSDimitry Andric   if (!IntvIn) {
1667eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << ", reload on exit.\n");
1668411bd29eSDimitry Andric     //
1669411bd29eSDimitry Andric     //    >>>>>>>          Possible EnterAfter interference.
1670411bd29eSDimitry Andric     //    |-----------|    Live through.
1671411bd29eSDimitry Andric     //    ___________--    Reload on exit.
1672411bd29eSDimitry Andric     //
1673411bd29eSDimitry Andric     selectIntv(IntvOut);
1674411bd29eSDimitry Andric     SlotIndex Idx = enterIntvAtEnd(*MBB);
1675411bd29eSDimitry Andric     assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1676411bd29eSDimitry Andric     (void)Idx;
1677411bd29eSDimitry Andric     return;
1678411bd29eSDimitry Andric   }
1679411bd29eSDimitry Andric 
1680411bd29eSDimitry Andric   if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
1681eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << ", straight through.\n");
1682411bd29eSDimitry Andric     //
1683411bd29eSDimitry Andric     //    |-----------|    Live through.
1684411bd29eSDimitry Andric     //    -------------    Straight through, same intv, no interference.
1685411bd29eSDimitry Andric     //
1686411bd29eSDimitry Andric     selectIntv(IntvOut);
1687411bd29eSDimitry Andric     useIntv(Start, Stop);
1688411bd29eSDimitry Andric     return;
1689411bd29eSDimitry Andric   }
1690411bd29eSDimitry Andric 
1691411bd29eSDimitry Andric   // We cannot legally insert splits after LSP.
1692411bd29eSDimitry Andric   SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
169330815c53SDimitry Andric   assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
1694411bd29eSDimitry Andric 
1695411bd29eSDimitry Andric   if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
1696411bd29eSDimitry Andric                   LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
1697eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << ", switch avoiding interference.\n");
1698411bd29eSDimitry Andric     //
1699411bd29eSDimitry Andric     //    >>>>     <<<<    Non-overlapping EnterAfter/LeaveBefore interference.
1700411bd29eSDimitry Andric     //    |-----------|    Live through.
1701411bd29eSDimitry Andric     //    ------=======    Switch intervals between interference.
1702411bd29eSDimitry Andric     //
1703411bd29eSDimitry Andric     selectIntv(IntvOut);
170430815c53SDimitry Andric     SlotIndex Idx;
170530815c53SDimitry Andric     if (LeaveBefore && LeaveBefore < LSP) {
170630815c53SDimitry Andric       Idx = enterIntvBefore(LeaveBefore);
1707411bd29eSDimitry Andric       useIntv(Idx, Stop);
170830815c53SDimitry Andric     } else {
170930815c53SDimitry Andric       Idx = enterIntvAtEnd(*MBB);
171030815c53SDimitry Andric     }
1711411bd29eSDimitry Andric     selectIntv(IntvIn);
1712411bd29eSDimitry Andric     useIntv(Start, Idx);
1713411bd29eSDimitry Andric     assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1714411bd29eSDimitry Andric     assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1715411bd29eSDimitry Andric     return;
1716411bd29eSDimitry Andric   }
1717411bd29eSDimitry Andric 
1718eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << ", create local intv for interference.\n");
1719411bd29eSDimitry Andric   //
1720411bd29eSDimitry Andric   //    >>><><><><<<<    Overlapping EnterAfter/LeaveBefore interference.
1721411bd29eSDimitry Andric   //    |-----------|    Live through.
1722411bd29eSDimitry Andric   //    ==---------==    Switch intervals before/after interference.
1723411bd29eSDimitry Andric   //
1724411bd29eSDimitry Andric   assert(LeaveBefore <= EnterAfter && "Missed case");
1725411bd29eSDimitry Andric 
1726411bd29eSDimitry Andric   selectIntv(IntvOut);
1727411bd29eSDimitry Andric   SlotIndex Idx = enterIntvAfter(EnterAfter);
1728411bd29eSDimitry Andric   useIntv(Idx, Stop);
1729411bd29eSDimitry Andric   assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1730411bd29eSDimitry Andric 
1731411bd29eSDimitry Andric   selectIntv(IntvIn);
1732411bd29eSDimitry Andric   Idx = leaveIntvBefore(LeaveBefore);
1733411bd29eSDimitry Andric   useIntv(Start, Idx);
1734411bd29eSDimitry Andric   assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1735411bd29eSDimitry Andric }
1736411bd29eSDimitry Andric 
splitRegInBlock(const SplitAnalysis::BlockInfo & BI,unsigned IntvIn,SlotIndex LeaveBefore)1737411bd29eSDimitry Andric void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
1738411bd29eSDimitry Andric                                   unsigned IntvIn, SlotIndex LeaveBefore) {
1739411bd29eSDimitry Andric   SlotIndex Start, Stop;
17405ca98fd9SDimitry Andric   std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
1741411bd29eSDimitry Andric 
1742eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << printMBBReference(*BI.MBB) << " [" << Start << ';'
1743eb11fae6SDimitry Andric                     << Stop << "), uses " << BI.FirstInstr << '-'
1744eb11fae6SDimitry Andric                     << BI.LastInstr << ", reg-in " << IntvIn
1745eb11fae6SDimitry Andric                     << ", leave before " << LeaveBefore
1746411bd29eSDimitry Andric                     << (BI.LiveOut ? ", stack-out" : ", killed in block"));
1747411bd29eSDimitry Andric 
1748411bd29eSDimitry Andric   assert(IntvIn && "Must have register in");
1749411bd29eSDimitry Andric   assert(BI.LiveIn && "Must be live-in");
1750411bd29eSDimitry Andric   assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
1751411bd29eSDimitry Andric 
175230815c53SDimitry Andric   if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
1753eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << " before interference.\n");
1754411bd29eSDimitry Andric     //
1755411bd29eSDimitry Andric     //               <<<    Interference after kill.
1756411bd29eSDimitry Andric     //     |---o---x   |    Killed in block.
1757411bd29eSDimitry Andric     //     =========        Use IntvIn everywhere.
1758411bd29eSDimitry Andric     //
1759411bd29eSDimitry Andric     selectIntv(IntvIn);
176030815c53SDimitry Andric     useIntv(Start, BI.LastInstr);
1761411bd29eSDimitry Andric     return;
1762411bd29eSDimitry Andric   }
1763411bd29eSDimitry Andric 
1764344a3780SDimitry Andric   SlotIndex LSP = SA.getLastSplitPoint(BI.MBB);
1765411bd29eSDimitry Andric 
176630815c53SDimitry Andric   if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
1767411bd29eSDimitry Andric     //
1768411bd29eSDimitry Andric     //               <<<    Possible interference after last use.
1769411bd29eSDimitry Andric     //     |---o---o---|    Live-out on stack.
1770411bd29eSDimitry Andric     //     =========____    Leave IntvIn after last use.
1771411bd29eSDimitry Andric     //
1772411bd29eSDimitry Andric     //                 <    Interference after last use.
1773411bd29eSDimitry Andric     //     |---o---o--o|    Live-out on stack, late last use.
1774411bd29eSDimitry Andric     //     ============     Copy to stack after LSP, overlap IntvIn.
1775411bd29eSDimitry Andric     //            \_____    Stack interval is live-out.
1776411bd29eSDimitry Andric     //
177730815c53SDimitry Andric     if (BI.LastInstr < LSP) {
1778eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << ", spill after last use before interference.\n");
1779411bd29eSDimitry Andric       selectIntv(IntvIn);
178030815c53SDimitry Andric       SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
1781411bd29eSDimitry Andric       useIntv(Start, Idx);
1782411bd29eSDimitry Andric       assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1783411bd29eSDimitry Andric     } else {
1784eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << ", spill before last split point.\n");
1785411bd29eSDimitry Andric       selectIntv(IntvIn);
1786411bd29eSDimitry Andric       SlotIndex Idx = leaveIntvBefore(LSP);
178730815c53SDimitry Andric       overlapIntv(Idx, BI.LastInstr);
1788411bd29eSDimitry Andric       useIntv(Start, Idx);
1789411bd29eSDimitry Andric       assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1790411bd29eSDimitry Andric     }
1791411bd29eSDimitry Andric     return;
1792411bd29eSDimitry Andric   }
1793411bd29eSDimitry Andric 
1794411bd29eSDimitry Andric   // The interference is overlapping somewhere we wanted to use IntvIn. That
1795411bd29eSDimitry Andric   // means we need to create a local interval that can be allocated a
1796411bd29eSDimitry Andric   // different register.
1797411bd29eSDimitry Andric   unsigned LocalIntv = openIntv();
1798411bd29eSDimitry Andric   (void)LocalIntv;
1799eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
1800411bd29eSDimitry Andric 
180130815c53SDimitry Andric   if (!BI.LiveOut || BI.LastInstr < LSP) {
1802411bd29eSDimitry Andric     //
1803411bd29eSDimitry Andric     //           <<<<<<<    Interference overlapping uses.
1804411bd29eSDimitry Andric     //     |---o---o---|    Live-out on stack.
1805411bd29eSDimitry Andric     //     =====----____    Leave IntvIn before interference, then spill.
1806411bd29eSDimitry Andric     //
180730815c53SDimitry Andric     SlotIndex To = leaveIntvAfter(BI.LastInstr);
1808411bd29eSDimitry Andric     SlotIndex From = enterIntvBefore(LeaveBefore);
1809411bd29eSDimitry Andric     useIntv(From, To);
1810411bd29eSDimitry Andric     selectIntv(IntvIn);
1811411bd29eSDimitry Andric     useIntv(Start, From);
1812411bd29eSDimitry Andric     assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1813411bd29eSDimitry Andric     return;
1814411bd29eSDimitry Andric   }
1815411bd29eSDimitry Andric 
1816411bd29eSDimitry Andric   //           <<<<<<<    Interference overlapping uses.
1817411bd29eSDimitry Andric   //     |---o---o--o|    Live-out on stack, late last use.
1818411bd29eSDimitry Andric   //     =====-------     Copy to stack before LSP, overlap LocalIntv.
1819411bd29eSDimitry Andric   //            \_____    Stack interval is live-out.
1820411bd29eSDimitry Andric   //
1821411bd29eSDimitry Andric   SlotIndex To = leaveIntvBefore(LSP);
182230815c53SDimitry Andric   overlapIntv(To, BI.LastInstr);
1823411bd29eSDimitry Andric   SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
1824411bd29eSDimitry Andric   useIntv(From, To);
1825411bd29eSDimitry Andric   selectIntv(IntvIn);
1826411bd29eSDimitry Andric   useIntv(Start, From);
1827411bd29eSDimitry Andric   assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1828411bd29eSDimitry Andric }
1829411bd29eSDimitry Andric 
splitRegOutBlock(const SplitAnalysis::BlockInfo & BI,unsigned IntvOut,SlotIndex EnterAfter)1830411bd29eSDimitry Andric void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
1831411bd29eSDimitry Andric                                    unsigned IntvOut, SlotIndex EnterAfter) {
1832411bd29eSDimitry Andric   SlotIndex Start, Stop;
18335ca98fd9SDimitry Andric   std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
1834411bd29eSDimitry Andric 
1835eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << printMBBReference(*BI.MBB) << " [" << Start << ';'
1836eb11fae6SDimitry Andric                     << Stop << "), uses " << BI.FirstInstr << '-'
1837eb11fae6SDimitry Andric                     << BI.LastInstr << ", reg-out " << IntvOut
1838eb11fae6SDimitry Andric                     << ", enter after " << EnterAfter
1839411bd29eSDimitry Andric                     << (BI.LiveIn ? ", stack-in" : ", defined in block"));
1840411bd29eSDimitry Andric 
1841344a3780SDimitry Andric   SlotIndex LSP = SA.getLastSplitPoint(BI.MBB);
1842411bd29eSDimitry Andric 
1843411bd29eSDimitry Andric   assert(IntvOut && "Must have register out");
1844411bd29eSDimitry Andric   assert(BI.LiveOut && "Must be live-out");
1845411bd29eSDimitry Andric   assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
1846411bd29eSDimitry Andric 
184730815c53SDimitry Andric   if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
1848eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << " after interference.\n");
1849411bd29eSDimitry Andric     //
1850411bd29eSDimitry Andric     //    >>>>             Interference before def.
1851411bd29eSDimitry Andric     //    |   o---o---|    Defined in block.
1852411bd29eSDimitry Andric     //        =========    Use IntvOut everywhere.
1853411bd29eSDimitry Andric     //
1854411bd29eSDimitry Andric     selectIntv(IntvOut);
185530815c53SDimitry Andric     useIntv(BI.FirstInstr, Stop);
1856411bd29eSDimitry Andric     return;
1857411bd29eSDimitry Andric   }
1858411bd29eSDimitry Andric 
185930815c53SDimitry Andric   if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
1860eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << ", reload after interference.\n");
1861411bd29eSDimitry Andric     //
1862411bd29eSDimitry Andric     //    >>>>             Interference before def.
1863411bd29eSDimitry Andric     //    |---o---o---|    Live-through, stack-in.
1864411bd29eSDimitry Andric     //    ____=========    Enter IntvOut before first use.
1865411bd29eSDimitry Andric     //
1866411bd29eSDimitry Andric     selectIntv(IntvOut);
186730815c53SDimitry Andric     SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
1868411bd29eSDimitry Andric     useIntv(Idx, Stop);
1869411bd29eSDimitry Andric     assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1870411bd29eSDimitry Andric     return;
1871411bd29eSDimitry Andric   }
1872411bd29eSDimitry Andric 
1873411bd29eSDimitry Andric   // The interference is overlapping somewhere we wanted to use IntvOut. That
1874411bd29eSDimitry Andric   // means we need to create a local interval that can be allocated a
1875411bd29eSDimitry Andric   // different register.
1876eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << ", interference overlaps uses.\n");
1877411bd29eSDimitry Andric   //
1878411bd29eSDimitry Andric   //    >>>>>>>          Interference overlapping uses.
1879411bd29eSDimitry Andric   //    |---o---o---|    Live-through, stack-in.
1880411bd29eSDimitry Andric   //    ____---======    Create local interval for interference range.
1881411bd29eSDimitry Andric   //
1882411bd29eSDimitry Andric   selectIntv(IntvOut);
1883411bd29eSDimitry Andric   SlotIndex Idx = enterIntvAfter(EnterAfter);
1884411bd29eSDimitry Andric   useIntv(Idx, Stop);
1885411bd29eSDimitry Andric   assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1886411bd29eSDimitry Andric 
1887411bd29eSDimitry Andric   openIntv();
188830815c53SDimitry Andric   SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
1889411bd29eSDimitry Andric   useIntv(From, Idx);
1890411bd29eSDimitry Andric }
1891344a3780SDimitry Andric 
print(raw_ostream & OS) const1892344a3780SDimitry Andric void SplitAnalysis::BlockInfo::print(raw_ostream &OS) const {
1893344a3780SDimitry Andric   OS << "{" << printMBBReference(*MBB) << ", "
1894344a3780SDimitry Andric      << "uses " << FirstInstr << " to " << LastInstr << ", "
1895344a3780SDimitry Andric      << "1st def " << FirstDef << ", "
1896344a3780SDimitry Andric      << (LiveIn ? "live in" : "dead in") << ", "
1897344a3780SDimitry Andric      << (LiveOut ? "live out" : "dead out") << "}";
1898344a3780SDimitry Andric }
1899344a3780SDimitry Andric 
dump() const1900344a3780SDimitry Andric void SplitAnalysis::BlockInfo::dump() const {
1901344a3780SDimitry Andric   print(dbgs());
1902344a3780SDimitry Andric   dbgs() << "\n";
1903344a3780SDimitry Andric }
1904