10b57cec5SDimitry Andric //===- SplitKit.cpp - Toolkit for splitting live ranges -------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains the SplitAnalysis class as well as mutator functions for
100b57cec5SDimitry Andric // live range splitting.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "SplitKit.h"
150b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
160b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
175ffd83dbSDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/LiveRangeEdit.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/VirtRegMap.h"
310b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
320b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h"
330b57cec5SDimitry Andric #include "llvm/Support/Allocator.h"
340b57cec5SDimitry Andric #include "llvm/Support/BlockFrequency.h"
350b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
360b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
370b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
380b57cec5SDimitry Andric #include <algorithm>
390b57cec5SDimitry Andric #include <cassert>
400b57cec5SDimitry Andric #include <iterator>
410b57cec5SDimitry Andric #include <limits>
420b57cec5SDimitry Andric #include <tuple>
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric using namespace llvm;
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric #define DEBUG_TYPE "regalloc"
470b57cec5SDimitry Andric 
485f757f3fSDimitry Andric static cl::opt<bool>
495f757f3fSDimitry Andric     EnableLoopIVHeuristic("enable-split-loopiv-heuristic",
505f757f3fSDimitry Andric                           cl::desc("Enable loop iv regalloc heuristic"),
515f757f3fSDimitry Andric                           cl::init(true));
525f757f3fSDimitry Andric 
530b57cec5SDimitry Andric STATISTIC(NumFinished, "Number of splits finished");
540b57cec5SDimitry Andric STATISTIC(NumSimple,   "Number of splits that were simple");
550b57cec5SDimitry Andric STATISTIC(NumCopies,   "Number of copies inserted for splitting");
560b57cec5SDimitry Andric STATISTIC(NumRemats,   "Number of rematerialized defs for splitting");
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
590b57cec5SDimitry Andric //                     Last Insert Point Analysis
600b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
610b57cec5SDimitry Andric 
InsertPointAnalysis(const LiveIntervals & lis,unsigned BBNum)620b57cec5SDimitry Andric InsertPointAnalysis::InsertPointAnalysis(const LiveIntervals &lis,
630b57cec5SDimitry Andric                                          unsigned BBNum)
640b57cec5SDimitry Andric     : LIS(lis), LastInsertPoint(BBNum) {}
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric SlotIndex
computeLastInsertPoint(const LiveInterval & CurLI,const MachineBasicBlock & MBB)670b57cec5SDimitry Andric InsertPointAnalysis::computeLastInsertPoint(const LiveInterval &CurLI,
680b57cec5SDimitry Andric                                             const MachineBasicBlock &MBB) {
690b57cec5SDimitry Andric   unsigned Num = MBB.getNumber();
700b57cec5SDimitry Andric   std::pair<SlotIndex, SlotIndex> &LIP = LastInsertPoint[Num];
710b57cec5SDimitry Andric   SlotIndex MBBEnd = LIS.getMBBEndIdx(&MBB);
720b57cec5SDimitry Andric 
735ffd83dbSDimitry Andric   SmallVector<const MachineBasicBlock *, 1> ExceptionalSuccessors;
745ffd83dbSDimitry Andric   bool EHPadSuccessor = false;
755ffd83dbSDimitry Andric   for (const MachineBasicBlock *SMBB : MBB.successors()) {
765ffd83dbSDimitry Andric     if (SMBB->isEHPad()) {
775ffd83dbSDimitry Andric       ExceptionalSuccessors.push_back(SMBB);
785ffd83dbSDimitry Andric       EHPadSuccessor = true;
795ffd83dbSDimitry Andric     } else if (SMBB->isInlineAsmBrIndirectTarget())
805ffd83dbSDimitry Andric       ExceptionalSuccessors.push_back(SMBB);
815ffd83dbSDimitry Andric   }
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric   // Compute insert points on the first call. The pair is independent of the
840b57cec5SDimitry Andric   // current live interval.
850b57cec5SDimitry Andric   if (!LIP.first.isValid()) {
860b57cec5SDimitry Andric     MachineBasicBlock::const_iterator FirstTerm = MBB.getFirstTerminator();
870b57cec5SDimitry Andric     if (FirstTerm == MBB.end())
880b57cec5SDimitry Andric       LIP.first = MBBEnd;
890b57cec5SDimitry Andric     else
900b57cec5SDimitry Andric       LIP.first = LIS.getInstructionIndex(*FirstTerm);
910b57cec5SDimitry Andric 
925ffd83dbSDimitry Andric     // If there is a landing pad or inlineasm_br successor, also find the
935ffd83dbSDimitry Andric     // instruction. If there is no such instruction, we don't need to do
945ffd83dbSDimitry Andric     // anything special.  We assume there cannot be multiple instructions that
955ffd83dbSDimitry Andric     // are Calls with EHPad successors or INLINEASM_BR in a block. Further, we
965ffd83dbSDimitry Andric     // assume that if there are any, they will be after any other call
975ffd83dbSDimitry Andric     // instructions in the block.
985ffd83dbSDimitry Andric     if (ExceptionalSuccessors.empty())
990b57cec5SDimitry Andric       return LIP.first;
100fe6060f1SDimitry Andric     for (const MachineInstr &MI : llvm::reverse(MBB)) {
101fe6060f1SDimitry Andric       if ((EHPadSuccessor && MI.isCall()) ||
102fe6060f1SDimitry Andric           MI.getOpcode() == TargetOpcode::INLINEASM_BR) {
103fe6060f1SDimitry Andric         LIP.second = LIS.getInstructionIndex(MI);
1040b57cec5SDimitry Andric         break;
1050b57cec5SDimitry Andric       }
1060b57cec5SDimitry Andric     }
1070b57cec5SDimitry Andric   }
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric   // If CurLI is live into a landing pad successor, move the last insert point
1100b57cec5SDimitry Andric   // back to the call that may throw.
1110b57cec5SDimitry Andric   if (!LIP.second)
1120b57cec5SDimitry Andric     return LIP.first;
1130b57cec5SDimitry Andric 
1145ffd83dbSDimitry Andric   if (none_of(ExceptionalSuccessors, [&](const MachineBasicBlock *EHPad) {
1150b57cec5SDimitry Andric         return LIS.isLiveInToMBB(CurLI, EHPad);
1160b57cec5SDimitry Andric       }))
1170b57cec5SDimitry Andric     return LIP.first;
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   // Find the value leaving MBB.
1200b57cec5SDimitry Andric   const VNInfo *VNI = CurLI.getVNInfoBefore(MBBEnd);
1210b57cec5SDimitry Andric   if (!VNI)
1220b57cec5SDimitry Andric     return LIP.first;
1230b57cec5SDimitry Andric 
124fe6060f1SDimitry Andric   // The def of statepoint instruction is a gc relocation and it should be alive
125fe6060f1SDimitry Andric   // in landing pad. So we cannot split interval after statepoint instruction.
126fe6060f1SDimitry Andric   if (SlotIndex::isSameInstr(VNI->def, LIP.second))
127fe6060f1SDimitry Andric     if (auto *I = LIS.getInstructionFromIndex(LIP.second))
128fe6060f1SDimitry Andric       if (I->getOpcode() == TargetOpcode::STATEPOINT)
129fe6060f1SDimitry Andric         return LIP.second;
130fe6060f1SDimitry Andric 
1310b57cec5SDimitry Andric   // If the value leaving MBB was defined after the call in MBB, it can't
1320b57cec5SDimitry Andric   // really be live-in to the landing pad.  This can happen if the landing pad
1330b57cec5SDimitry Andric   // has a PHI, and this register is undef on the exceptional edge.
1340b57cec5SDimitry Andric   if (!SlotIndex::isEarlierInstr(VNI->def, LIP.second) && VNI->def < MBBEnd)
1350b57cec5SDimitry Andric     return LIP.first;
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric   // Value is properly live-in to the landing pad.
1380b57cec5SDimitry Andric   // Only allow inserts before the call.
1390b57cec5SDimitry Andric   return LIP.second;
1400b57cec5SDimitry Andric }
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric MachineBasicBlock::iterator
getLastInsertPointIter(const LiveInterval & CurLI,MachineBasicBlock & MBB)1430b57cec5SDimitry Andric InsertPointAnalysis::getLastInsertPointIter(const LiveInterval &CurLI,
1440b57cec5SDimitry Andric                                             MachineBasicBlock &MBB) {
1450b57cec5SDimitry Andric   SlotIndex LIP = getLastInsertPoint(CurLI, MBB);
1460b57cec5SDimitry Andric   if (LIP == LIS.getMBBEndIdx(&MBB))
1470b57cec5SDimitry Andric     return MBB.end();
1480b57cec5SDimitry Andric   return LIS.getInstructionFromIndex(LIP);
1490b57cec5SDimitry Andric }
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1520b57cec5SDimitry Andric //                                 Split Analysis
1530b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1540b57cec5SDimitry Andric 
SplitAnalysis(const VirtRegMap & vrm,const LiveIntervals & lis,const MachineLoopInfo & mli)1550b57cec5SDimitry Andric SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis,
1560b57cec5SDimitry Andric                              const MachineLoopInfo &mli)
1570b57cec5SDimitry Andric     : MF(vrm.getMachineFunction()), VRM(vrm), LIS(lis), Loops(mli),
1580b57cec5SDimitry Andric       TII(*MF.getSubtarget().getInstrInfo()), IPA(lis, MF.getNumBlockIDs()) {}
1590b57cec5SDimitry Andric 
clear()1600b57cec5SDimitry Andric void SplitAnalysis::clear() {
1610b57cec5SDimitry Andric   UseSlots.clear();
1620b57cec5SDimitry Andric   UseBlocks.clear();
1630b57cec5SDimitry Andric   ThroughBlocks.clear();
1640b57cec5SDimitry Andric   CurLI = nullptr;
1650b57cec5SDimitry Andric }
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric /// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
analyzeUses()1680b57cec5SDimitry Andric void SplitAnalysis::analyzeUses() {
1690b57cec5SDimitry Andric   assert(UseSlots.empty() && "Call clear first");
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric   // First get all the defs from the interval values. This provides the correct
1720b57cec5SDimitry Andric   // slots for early clobbers.
1730b57cec5SDimitry Andric   for (const VNInfo *VNI : CurLI->valnos)
1740b57cec5SDimitry Andric     if (!VNI->isPHIDef() && !VNI->isUnused())
1750b57cec5SDimitry Andric       UseSlots.push_back(VNI->def);
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric   // Get use slots form the use-def chain.
1780b57cec5SDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
179e8d8bef9SDimitry Andric   for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg()))
1800b57cec5SDimitry Andric     if (!MO.isUndef())
1810b57cec5SDimitry Andric       UseSlots.push_back(LIS.getInstructionIndex(*MO.getParent()).getRegSlot());
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric   array_pod_sort(UseSlots.begin(), UseSlots.end());
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric   // Remove duplicates, keeping the smaller slot for each instruction.
1860b57cec5SDimitry Andric   // That is what we want for early clobbers.
1870b57cec5SDimitry Andric   UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
1880b57cec5SDimitry Andric                              SlotIndex::isSameInstr),
1890b57cec5SDimitry Andric                  UseSlots.end());
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric   // Compute per-live block info.
192349cc55cSDimitry Andric   calcLiveBlockInfo();
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Analyze counted " << UseSlots.size() << " instrs in "
1950b57cec5SDimitry Andric                     << UseBlocks.size() << " blocks, through "
1960b57cec5SDimitry Andric                     << NumThroughBlocks << " blocks.\n");
1970b57cec5SDimitry Andric }
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric /// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
2000b57cec5SDimitry Andric /// where CurLI is live.
calcLiveBlockInfo()201349cc55cSDimitry Andric void SplitAnalysis::calcLiveBlockInfo() {
2020b57cec5SDimitry Andric   ThroughBlocks.resize(MF.getNumBlockIDs());
2030b57cec5SDimitry Andric   NumThroughBlocks = NumGapBlocks = 0;
2040b57cec5SDimitry Andric   if (CurLI->empty())
205349cc55cSDimitry Andric     return;
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric   LiveInterval::const_iterator LVI = CurLI->begin();
2080b57cec5SDimitry Andric   LiveInterval::const_iterator LVE = CurLI->end();
2090b57cec5SDimitry Andric 
2100b57cec5SDimitry Andric   SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
2110b57cec5SDimitry Andric   UseI = UseSlots.begin();
2120b57cec5SDimitry Andric   UseE = UseSlots.end();
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric   // Loop over basic blocks where CurLI is live.
2150b57cec5SDimitry Andric   MachineFunction::iterator MFI =
2160b57cec5SDimitry Andric       LIS.getMBBFromIndex(LVI->start)->getIterator();
2170b57cec5SDimitry Andric   while (true) {
2180b57cec5SDimitry Andric     BlockInfo BI;
2190b57cec5SDimitry Andric     BI.MBB = &*MFI;
2200b57cec5SDimitry Andric     SlotIndex Start, Stop;
2210b57cec5SDimitry Andric     std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric     // If the block contains no uses, the range must be live through. At one
2240b57cec5SDimitry Andric     // point, RegisterCoalescer could create dangling ranges that ended
2250b57cec5SDimitry Andric     // mid-block.
2260b57cec5SDimitry Andric     if (UseI == UseE || *UseI >= Stop) {
2270b57cec5SDimitry Andric       ++NumThroughBlocks;
2280b57cec5SDimitry Andric       ThroughBlocks.set(BI.MBB->getNumber());
2290b57cec5SDimitry Andric       // The range shouldn't end mid-block if there are no uses. This shouldn't
2300b57cec5SDimitry Andric       // happen.
231349cc55cSDimitry Andric       assert(LVI->end >= Stop && "range ends mid block with no uses");
2320b57cec5SDimitry Andric     } else {
2330b57cec5SDimitry Andric       // This block has uses. Find the first and last uses in the block.
2340b57cec5SDimitry Andric       BI.FirstInstr = *UseI;
2350b57cec5SDimitry Andric       assert(BI.FirstInstr >= Start);
2360b57cec5SDimitry Andric       do ++UseI;
2370b57cec5SDimitry Andric       while (UseI != UseE && *UseI < Stop);
2380b57cec5SDimitry Andric       BI.LastInstr = UseI[-1];
2390b57cec5SDimitry Andric       assert(BI.LastInstr < Stop);
2400b57cec5SDimitry Andric 
2410b57cec5SDimitry Andric       // LVI is the first live segment overlapping MBB.
2420b57cec5SDimitry Andric       BI.LiveIn = LVI->start <= Start;
2430b57cec5SDimitry Andric 
2440b57cec5SDimitry Andric       // When not live in, the first use should be a def.
2450b57cec5SDimitry Andric       if (!BI.LiveIn) {
2460b57cec5SDimitry Andric         assert(LVI->start == LVI->valno->def && "Dangling Segment start");
2470b57cec5SDimitry Andric         assert(LVI->start == BI.FirstInstr && "First instr should be a def");
2480b57cec5SDimitry Andric         BI.FirstDef = BI.FirstInstr;
2490b57cec5SDimitry Andric       }
2500b57cec5SDimitry Andric 
2510b57cec5SDimitry Andric       // Look for gaps in the live range.
2520b57cec5SDimitry Andric       BI.LiveOut = true;
2530b57cec5SDimitry Andric       while (LVI->end < Stop) {
2540b57cec5SDimitry Andric         SlotIndex LastStop = LVI->end;
2550b57cec5SDimitry Andric         if (++LVI == LVE || LVI->start >= Stop) {
2560b57cec5SDimitry Andric           BI.LiveOut = false;
2570b57cec5SDimitry Andric           BI.LastInstr = LastStop;
2580b57cec5SDimitry Andric           break;
2590b57cec5SDimitry Andric         }
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric         if (LastStop < LVI->start) {
2620b57cec5SDimitry Andric           // There is a gap in the live range. Create duplicate entries for the
2630b57cec5SDimitry Andric           // live-in snippet and the live-out snippet.
2640b57cec5SDimitry Andric           ++NumGapBlocks;
2650b57cec5SDimitry Andric 
2660b57cec5SDimitry Andric           // Push the Live-in part.
2670b57cec5SDimitry Andric           BI.LiveOut = false;
2680b57cec5SDimitry Andric           UseBlocks.push_back(BI);
2690b57cec5SDimitry Andric           UseBlocks.back().LastInstr = LastStop;
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric           // Set up BI for the live-out part.
2720b57cec5SDimitry Andric           BI.LiveIn = false;
2730b57cec5SDimitry Andric           BI.LiveOut = true;
2740b57cec5SDimitry Andric           BI.FirstInstr = BI.FirstDef = LVI->start;
2750b57cec5SDimitry Andric         }
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric         // A Segment that starts in the middle of the block must be a def.
2780b57cec5SDimitry Andric         assert(LVI->start == LVI->valno->def && "Dangling Segment start");
2790b57cec5SDimitry Andric         if (!BI.FirstDef)
2800b57cec5SDimitry Andric           BI.FirstDef = LVI->start;
2810b57cec5SDimitry Andric       }
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric       UseBlocks.push_back(BI);
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric       // LVI is now at LVE or LVI->end >= Stop.
2860b57cec5SDimitry Andric       if (LVI == LVE)
2870b57cec5SDimitry Andric         break;
2880b57cec5SDimitry Andric     }
2890b57cec5SDimitry Andric 
2900b57cec5SDimitry Andric     // Live segment ends exactly at Stop. Move to the next segment.
2910b57cec5SDimitry Andric     if (LVI->end == Stop && ++LVI == LVE)
2920b57cec5SDimitry Andric       break;
2930b57cec5SDimitry Andric 
2940b57cec5SDimitry Andric     // Pick the next basic block.
2950b57cec5SDimitry Andric     if (LVI->start < Stop)
2960b57cec5SDimitry Andric       ++MFI;
2970b57cec5SDimitry Andric     else
2980b57cec5SDimitry Andric       MFI = LIS.getMBBFromIndex(LVI->start)->getIterator();
2990b57cec5SDimitry Andric   }
3000b57cec5SDimitry Andric 
3015f757f3fSDimitry Andric   LooksLikeLoopIV = EnableLoopIVHeuristic && UseBlocks.size() == 2 &&
3025f757f3fSDimitry Andric                     any_of(UseBlocks, [this](BlockInfo &BI) {
3035f757f3fSDimitry Andric                       MachineLoop *L = Loops.getLoopFor(BI.MBB);
3045f757f3fSDimitry Andric                       return BI.LiveIn && BI.LiveOut && BI.FirstDef && L &&
3055f757f3fSDimitry Andric                              L->isLoopLatch(BI.MBB);
3065f757f3fSDimitry Andric                     });
3075f757f3fSDimitry Andric 
3080b57cec5SDimitry Andric   assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
3090b57cec5SDimitry Andric }
3100b57cec5SDimitry Andric 
countLiveBlocks(const LiveInterval * cli) const3110b57cec5SDimitry Andric unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
3120b57cec5SDimitry Andric   if (cli->empty())
3130b57cec5SDimitry Andric     return 0;
3140b57cec5SDimitry Andric   LiveInterval *li = const_cast<LiveInterval*>(cli);
3150b57cec5SDimitry Andric   LiveInterval::iterator LVI = li->begin();
3160b57cec5SDimitry Andric   LiveInterval::iterator LVE = li->end();
3170b57cec5SDimitry Andric   unsigned Count = 0;
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric   // Loop over basic blocks where li is live.
3200b57cec5SDimitry Andric   MachineFunction::const_iterator MFI =
3210b57cec5SDimitry Andric       LIS.getMBBFromIndex(LVI->start)->getIterator();
3220b57cec5SDimitry Andric   SlotIndex Stop = LIS.getMBBEndIdx(&*MFI);
3230b57cec5SDimitry Andric   while (true) {
3240b57cec5SDimitry Andric     ++Count;
3250b57cec5SDimitry Andric     LVI = li->advanceTo(LVI, Stop);
3260b57cec5SDimitry Andric     if (LVI == LVE)
3270b57cec5SDimitry Andric       return Count;
3280b57cec5SDimitry Andric     do {
3290b57cec5SDimitry Andric       ++MFI;
3300b57cec5SDimitry Andric       Stop = LIS.getMBBEndIdx(&*MFI);
3310b57cec5SDimitry Andric     } while (Stop <= LVI->start);
3320b57cec5SDimitry Andric   }
3330b57cec5SDimitry Andric }
3340b57cec5SDimitry Andric 
isOriginalEndpoint(SlotIndex Idx) const3350b57cec5SDimitry Andric bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
336bdd1243dSDimitry Andric   Register OrigReg = VRM.getOriginal(CurLI->reg());
3370b57cec5SDimitry Andric   const LiveInterval &Orig = LIS.getInterval(OrigReg);
3380b57cec5SDimitry Andric   assert(!Orig.empty() && "Splitting empty interval?");
3390b57cec5SDimitry Andric   LiveInterval::const_iterator I = Orig.find(Idx);
3400b57cec5SDimitry Andric 
3410b57cec5SDimitry Andric   // Range containing Idx should begin at Idx.
3420b57cec5SDimitry Andric   if (I != Orig.end() && I->start <= Idx)
3430b57cec5SDimitry Andric     return I->start == Idx;
3440b57cec5SDimitry Andric 
3450b57cec5SDimitry Andric   // Range does not contain Idx, previous must end at Idx.
3460b57cec5SDimitry Andric   return I != Orig.begin() && (--I)->end == Idx;
3470b57cec5SDimitry Andric }
3480b57cec5SDimitry Andric 
analyze(const LiveInterval * li)3490b57cec5SDimitry Andric void SplitAnalysis::analyze(const LiveInterval *li) {
3500b57cec5SDimitry Andric   clear();
3510b57cec5SDimitry Andric   CurLI = li;
3520b57cec5SDimitry Andric   analyzeUses();
3530b57cec5SDimitry Andric }
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3560b57cec5SDimitry Andric //                               Split Editor
3570b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry 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)360fcaf7f86SDimitry Andric SplitEditor::SplitEditor(SplitAnalysis &SA, LiveIntervals &LIS, VirtRegMap &VRM,
361fe6060f1SDimitry Andric                          MachineDominatorTree &MDT,
362fe6060f1SDimitry Andric                          MachineBlockFrequencyInfo &MBFI, VirtRegAuxInfo &VRAI)
363fcaf7f86SDimitry Andric     : SA(SA), LIS(LIS), VRM(VRM), MRI(VRM.getMachineFunction().getRegInfo()),
364fcaf7f86SDimitry Andric       MDT(MDT), TII(*VRM.getMachineFunction().getSubtarget().getInstrInfo()),
365fe6060f1SDimitry Andric       TRI(*VRM.getMachineFunction().getSubtarget().getRegisterInfo()),
366fe6060f1SDimitry Andric       MBFI(MBFI), VRAI(VRAI), RegAssign(Allocator) {}
3670b57cec5SDimitry Andric 
reset(LiveRangeEdit & LRE,ComplementSpillMode SM)3680b57cec5SDimitry Andric void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
3690b57cec5SDimitry Andric   Edit = &LRE;
3700b57cec5SDimitry Andric   SpillMode = SM;
3710b57cec5SDimitry Andric   OpenIdx = 0;
3720b57cec5SDimitry Andric   RegAssign.clear();
3730b57cec5SDimitry Andric   Values.clear();
3740b57cec5SDimitry Andric 
3755ffd83dbSDimitry Andric   // Reset the LiveIntervalCalc instances needed for this spill mode.
3765ffd83dbSDimitry Andric   LICalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
3770b57cec5SDimitry Andric                   &LIS.getVNInfoAllocator());
3780b57cec5SDimitry Andric   if (SpillMode)
3795ffd83dbSDimitry Andric     LICalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
3800b57cec5SDimitry Andric                     &LIS.getVNInfoAllocator());
3810b57cec5SDimitry Andric 
382fcaf7f86SDimitry Andric   Edit->anyRematerializable();
3830b57cec5SDimitry Andric }
3840b57cec5SDimitry Andric 
3850b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const3860b57cec5SDimitry Andric LLVM_DUMP_METHOD void SplitEditor::dump() const {
3870b57cec5SDimitry Andric   if (RegAssign.empty()) {
3880b57cec5SDimitry Andric     dbgs() << " empty\n";
3890b57cec5SDimitry Andric     return;
3900b57cec5SDimitry Andric   }
3910b57cec5SDimitry Andric 
3920b57cec5SDimitry Andric   for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
3930b57cec5SDimitry Andric     dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
3940b57cec5SDimitry Andric   dbgs() << '\n';
3950b57cec5SDimitry Andric }
3960b57cec5SDimitry Andric #endif
3970b57cec5SDimitry Andric 
39881ad6265SDimitry Andric /// Find a subrange corresponding to the exact lane mask @p LM in the live
39981ad6265SDimitry Andric /// interval @p LI. The interval @p LI is assumed to contain such a subrange.
40081ad6265SDimitry Andric /// This function is used to find corresponding subranges between the
40181ad6265SDimitry Andric /// original interval and the new intervals.
getSubrangeImpl(LaneBitmask LM,T & LI)40281ad6265SDimitry Andric template <typename T> auto &getSubrangeImpl(LaneBitmask LM, T &LI) {
40381ad6265SDimitry Andric   for (auto &S : LI.subranges())
4040b57cec5SDimitry Andric     if (S.LaneMask == LM)
4050b57cec5SDimitry Andric       return S;
4060b57cec5SDimitry Andric   llvm_unreachable("SubRange for this mask not found");
4070b57cec5SDimitry Andric }
4080b57cec5SDimitry Andric 
getSubRangeForMaskExact(LaneBitmask LM,LiveInterval & LI)40981ad6265SDimitry Andric LiveInterval::SubRange &getSubRangeForMaskExact(LaneBitmask LM,
410e8d8bef9SDimitry Andric                                                 LiveInterval &LI) {
41181ad6265SDimitry Andric   return getSubrangeImpl(LM, LI);
41281ad6265SDimitry Andric }
41381ad6265SDimitry Andric 
getSubRangeForMaskExact(LaneBitmask LM,const LiveInterval & LI)41481ad6265SDimitry Andric const LiveInterval::SubRange &getSubRangeForMaskExact(LaneBitmask LM,
41581ad6265SDimitry Andric                                                       const LiveInterval &LI) {
41681ad6265SDimitry Andric   return getSubrangeImpl(LM, LI);
41781ad6265SDimitry Andric }
41881ad6265SDimitry Andric 
41981ad6265SDimitry Andric /// Find a subrange corresponding to the lane mask @p LM, or a superset of it,
42081ad6265SDimitry Andric /// in the live interval @p LI. The interval @p LI is assumed to contain such
42181ad6265SDimitry Andric /// a subrange.  This function is used to find corresponding subranges between
42281ad6265SDimitry Andric /// the original interval and the new intervals.
getSubRangeForMask(LaneBitmask LM,const LiveInterval & LI)42381ad6265SDimitry Andric const LiveInterval::SubRange &getSubRangeForMask(LaneBitmask LM,
42481ad6265SDimitry Andric                                                  const LiveInterval &LI) {
42581ad6265SDimitry Andric   for (const LiveInterval::SubRange &S : LI.subranges())
426e8d8bef9SDimitry Andric     if ((S.LaneMask & LM) == LM)
427e8d8bef9SDimitry Andric       return S;
428e8d8bef9SDimitry Andric   llvm_unreachable("SubRange for this mask not found");
429e8d8bef9SDimitry Andric }
430e8d8bef9SDimitry Andric 
addDeadDef(LiveInterval & LI,VNInfo * VNI,bool Original)4310b57cec5SDimitry Andric void SplitEditor::addDeadDef(LiveInterval &LI, VNInfo *VNI, bool Original) {
4320b57cec5SDimitry Andric   if (!LI.hasSubRanges()) {
4330b57cec5SDimitry Andric     LI.createDeadDef(VNI);
4340b57cec5SDimitry Andric     return;
4350b57cec5SDimitry Andric   }
4360b57cec5SDimitry Andric 
4370b57cec5SDimitry Andric   SlotIndex Def = VNI->def;
4380b57cec5SDimitry Andric   if (Original) {
4390b57cec5SDimitry Andric     // If we are transferring a def from the original interval, make sure
4400b57cec5SDimitry Andric     // to only update the subranges for which the original subranges had
4410b57cec5SDimitry Andric     // a def at this location.
4420b57cec5SDimitry Andric     for (LiveInterval::SubRange &S : LI.subranges()) {
4430b57cec5SDimitry Andric       auto &PS = getSubRangeForMask(S.LaneMask, Edit->getParent());
4440b57cec5SDimitry Andric       VNInfo *PV = PS.getVNInfoAt(Def);
4450b57cec5SDimitry Andric       if (PV != nullptr && PV->def == Def)
4460b57cec5SDimitry Andric         S.createDeadDef(Def, LIS.getVNInfoAllocator());
4470b57cec5SDimitry Andric     }
4480b57cec5SDimitry Andric   } else {
4490b57cec5SDimitry Andric     // This is a new def: either from rematerialization, or from an inserted
4500b57cec5SDimitry Andric     // copy. Since rematerialization can regenerate a definition of a sub-
4510b57cec5SDimitry Andric     // register, we need to check which subranges need to be updated.
4520b57cec5SDimitry Andric     const MachineInstr *DefMI = LIS.getInstructionFromIndex(Def);
4530b57cec5SDimitry Andric     assert(DefMI != nullptr);
4540b57cec5SDimitry Andric     LaneBitmask LM;
4550b57cec5SDimitry Andric     for (const MachineOperand &DefOp : DefMI->defs()) {
4568bcb0991SDimitry Andric       Register R = DefOp.getReg();
457e8d8bef9SDimitry Andric       if (R != LI.reg())
4580b57cec5SDimitry Andric         continue;
4590b57cec5SDimitry Andric       if (unsigned SR = DefOp.getSubReg())
4600b57cec5SDimitry Andric         LM |= TRI.getSubRegIndexLaneMask(SR);
4610b57cec5SDimitry Andric       else {
4620b57cec5SDimitry Andric         LM = MRI.getMaxLaneMaskForVReg(R);
4630b57cec5SDimitry Andric         break;
4640b57cec5SDimitry Andric       }
4650b57cec5SDimitry Andric     }
4660b57cec5SDimitry Andric     for (LiveInterval::SubRange &S : LI.subranges())
4670b57cec5SDimitry Andric       if ((S.LaneMask & LM).any())
4680b57cec5SDimitry Andric         S.createDeadDef(Def, LIS.getVNInfoAllocator());
4690b57cec5SDimitry Andric   }
4700b57cec5SDimitry Andric }
4710b57cec5SDimitry Andric 
defValue(unsigned RegIdx,const VNInfo * ParentVNI,SlotIndex Idx,bool Original)4720b57cec5SDimitry Andric VNInfo *SplitEditor::defValue(unsigned RegIdx,
4730b57cec5SDimitry Andric                               const VNInfo *ParentVNI,
4740b57cec5SDimitry Andric                               SlotIndex Idx,
4750b57cec5SDimitry Andric                               bool Original) {
4760b57cec5SDimitry Andric   assert(ParentVNI && "Mapping  NULL value");
4770b57cec5SDimitry Andric   assert(Idx.isValid() && "Invalid SlotIndex");
4780b57cec5SDimitry Andric   assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
4790b57cec5SDimitry Andric   LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
4800b57cec5SDimitry Andric 
4810b57cec5SDimitry Andric   // Create a new value.
4820b57cec5SDimitry Andric   VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
4830b57cec5SDimitry Andric 
4840b57cec5SDimitry Andric   bool Force = LI->hasSubRanges();
4850b57cec5SDimitry Andric   ValueForcePair FP(Force ? nullptr : VNI, Force);
4860b57cec5SDimitry Andric   // Use insert for lookup, so we can add missing values with a second lookup.
4870b57cec5SDimitry Andric   std::pair<ValueMap::iterator, bool> InsP =
4880b57cec5SDimitry Andric     Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), FP));
4890b57cec5SDimitry Andric 
4900b57cec5SDimitry Andric   // This was the first time (RegIdx, ParentVNI) was mapped, and it is not
4910b57cec5SDimitry Andric   // forced. Keep it as a simple def without any liveness.
4920b57cec5SDimitry Andric   if (!Force && InsP.second)
4930b57cec5SDimitry Andric     return VNI;
4940b57cec5SDimitry Andric 
4950b57cec5SDimitry Andric   // If the previous value was a simple mapping, add liveness for it now.
4960b57cec5SDimitry Andric   if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
4970b57cec5SDimitry Andric     addDeadDef(*LI, OldVNI, Original);
4980b57cec5SDimitry Andric 
4990b57cec5SDimitry Andric     // No longer a simple mapping.  Switch to a complex mapping. If the
5000b57cec5SDimitry Andric     // interval has subranges, make it a forced mapping.
5010b57cec5SDimitry Andric     InsP.first->second = ValueForcePair(nullptr, Force);
5020b57cec5SDimitry Andric   }
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric   // This is a complex mapping, add liveness for VNI
5050b57cec5SDimitry Andric   addDeadDef(*LI, VNI, Original);
5060b57cec5SDimitry Andric   return VNI;
5070b57cec5SDimitry Andric }
5080b57cec5SDimitry Andric 
forceRecompute(unsigned RegIdx,const VNInfo & ParentVNI)5090b57cec5SDimitry Andric void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo &ParentVNI) {
5100b57cec5SDimitry Andric   ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI.id)];
5110b57cec5SDimitry Andric   VNInfo *VNI = VFP.getPointer();
5120b57cec5SDimitry Andric 
5130b57cec5SDimitry Andric   // ParentVNI was either unmapped or already complex mapped. Either way, just
5140b57cec5SDimitry Andric   // set the force bit.
5150b57cec5SDimitry Andric   if (!VNI) {
5160b57cec5SDimitry Andric     VFP.setInt(true);
5170b57cec5SDimitry Andric     return;
5180b57cec5SDimitry Andric   }
5190b57cec5SDimitry Andric 
5200b57cec5SDimitry Andric   // This was previously a single mapping. Make sure the old def is represented
5210b57cec5SDimitry Andric   // by a trivial live range.
5220b57cec5SDimitry Andric   addDeadDef(LIS.getInterval(Edit->get(RegIdx)), VNI, false);
5230b57cec5SDimitry Andric 
5240b57cec5SDimitry Andric   // Mark as complex mapped, forced.
5250b57cec5SDimitry Andric   VFP = ValueForcePair(nullptr, true);
5260b57cec5SDimitry Andric }
5270b57cec5SDimitry Andric 
buildSingleSubRegCopy(Register FromReg,Register ToReg,MachineBasicBlock & MBB,MachineBasicBlock::iterator InsertBefore,unsigned SubIdx,LiveInterval & DestLI,bool Late,SlotIndex Def,const MCInstrDesc & Desc)5285f757f3fSDimitry Andric SlotIndex SplitEditor::buildSingleSubRegCopy(
5295f757f3fSDimitry Andric     Register FromReg, Register ToReg, MachineBasicBlock &MBB,
5305f757f3fSDimitry Andric     MachineBasicBlock::iterator InsertBefore, unsigned SubIdx,
5315f757f3fSDimitry Andric     LiveInterval &DestLI, bool Late, SlotIndex Def, const MCInstrDesc &Desc) {
5320b57cec5SDimitry Andric   bool FirstCopy = !Def.isValid();
5330b57cec5SDimitry Andric   MachineInstr *CopyMI = BuildMI(MBB, InsertBefore, DebugLoc(), Desc)
5340b57cec5SDimitry Andric       .addReg(ToReg, RegState::Define | getUndefRegState(FirstCopy)
5350b57cec5SDimitry Andric               | getInternalReadRegState(!FirstCopy), SubIdx)
5360b57cec5SDimitry Andric       .addReg(FromReg, 0, SubIdx);
5370b57cec5SDimitry Andric 
5380b57cec5SDimitry Andric   SlotIndexes &Indexes = *LIS.getSlotIndexes();
5390b57cec5SDimitry Andric   if (FirstCopy) {
5400b57cec5SDimitry Andric     Def = Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot();
5410b57cec5SDimitry Andric   } else {
5420b57cec5SDimitry Andric     CopyMI->bundleWithPred();
5430b57cec5SDimitry Andric   }
5440b57cec5SDimitry Andric   return Def;
5450b57cec5SDimitry Andric }
5460b57cec5SDimitry Andric 
buildCopy(Register FromReg,Register ToReg,LaneBitmask LaneMask,MachineBasicBlock & MBB,MachineBasicBlock::iterator InsertBefore,bool Late,unsigned RegIdx)547e8d8bef9SDimitry Andric SlotIndex SplitEditor::buildCopy(Register FromReg, Register ToReg,
5480b57cec5SDimitry Andric     LaneBitmask LaneMask, MachineBasicBlock &MBB,
5490b57cec5SDimitry Andric     MachineBasicBlock::iterator InsertBefore, bool Late, unsigned RegIdx) {
5505f757f3fSDimitry Andric   const MCInstrDesc &Desc =
5515f757f3fSDimitry Andric       TII.get(TII.getLiveRangeSplitOpcode(FromReg, *MBB.getParent()));
552349cc55cSDimitry Andric   SlotIndexes &Indexes = *LIS.getSlotIndexes();
5530b57cec5SDimitry Andric   if (LaneMask.all() || LaneMask == MRI.getMaxLaneMaskForVReg(FromReg)) {
5540b57cec5SDimitry Andric     // The full vreg is copied.
5550b57cec5SDimitry Andric     MachineInstr *CopyMI =
5560b57cec5SDimitry Andric         BuildMI(MBB, InsertBefore, DebugLoc(), Desc, ToReg).addReg(FromReg);
5570b57cec5SDimitry Andric     return Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot();
5580b57cec5SDimitry Andric   }
5590b57cec5SDimitry Andric 
5600b57cec5SDimitry Andric   // Only a subset of lanes needs to be copied. The following is a simple
5610b57cec5SDimitry Andric   // heuristic to construct a sequence of COPYs. We could add a target
5620b57cec5SDimitry Andric   // specific callback if this turns out to be suboptimal.
5630b57cec5SDimitry Andric   LiveInterval &DestLI = LIS.getInterval(Edit->get(RegIdx));
5640b57cec5SDimitry Andric 
5650b57cec5SDimitry Andric   // First pass: Try to find a perfectly matching subregister index. If none
5660b57cec5SDimitry Andric   // exists find the one covering the most lanemask bits.
5670b57cec5SDimitry Andric   const TargetRegisterClass *RC = MRI.getRegClass(FromReg);
5680b57cec5SDimitry Andric   assert(RC == MRI.getRegClass(ToReg) && "Should have same reg class");
5690b57cec5SDimitry Andric 
570349cc55cSDimitry Andric   SmallVector<unsigned, 8> SubIndexes;
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric   // Abort if we cannot possibly implement the COPY with the given indexes.
573349cc55cSDimitry Andric   if (!TRI.getCoveringSubRegIndexes(MRI, RC, LaneMask, SubIndexes))
5740b57cec5SDimitry Andric     report_fatal_error("Impossible to implement partial COPY");
5750b57cec5SDimitry Andric 
576fe6060f1SDimitry Andric   SlotIndex Def;
577349cc55cSDimitry Andric   for (unsigned BestIdx : SubIndexes) {
578fe6060f1SDimitry Andric     Def = buildSingleSubRegCopy(FromReg, ToReg, MBB, InsertBefore, BestIdx,
5795f757f3fSDimitry Andric                                 DestLI, Late, Def, Desc);
5800b57cec5SDimitry Andric   }
5810b57cec5SDimitry Andric 
582349cc55cSDimitry Andric   BumpPtrAllocator &Allocator = LIS.getVNInfoAllocator();
583349cc55cSDimitry Andric   DestLI.refineSubRanges(
584349cc55cSDimitry Andric       Allocator, LaneMask,
585349cc55cSDimitry Andric       [Def, &Allocator](LiveInterval::SubRange &SR) {
586349cc55cSDimitry Andric         SR.createDeadDef(Def, Allocator);
587349cc55cSDimitry Andric       },
588349cc55cSDimitry Andric       Indexes, TRI);
589349cc55cSDimitry Andric 
5900b57cec5SDimitry Andric   return Def;
5910b57cec5SDimitry Andric }
5920b57cec5SDimitry Andric 
defFromParent(unsigned RegIdx,const VNInfo * ParentVNI,SlotIndex UseIdx,MachineBasicBlock & MBB,MachineBasicBlock::iterator I)59381ad6265SDimitry Andric VNInfo *SplitEditor::defFromParent(unsigned RegIdx, const VNInfo *ParentVNI,
59481ad6265SDimitry Andric                                    SlotIndex UseIdx, MachineBasicBlock &MBB,
5950b57cec5SDimitry Andric                                    MachineBasicBlock::iterator I) {
5960b57cec5SDimitry Andric   SlotIndex Def;
5970b57cec5SDimitry Andric   LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
5980b57cec5SDimitry Andric 
5990b57cec5SDimitry Andric   // We may be trying to avoid interference that ends at a deleted instruction,
6000b57cec5SDimitry Andric   // so always begin RegIdx 0 early and all others late.
6010b57cec5SDimitry Andric   bool Late = RegIdx != 0;
6020b57cec5SDimitry Andric 
6030b57cec5SDimitry Andric   // Attempt cheap-as-a-copy rematerialization.
604bdd1243dSDimitry Andric   Register Original = VRM.getOriginal(Edit->get(RegIdx));
6050b57cec5SDimitry Andric   LiveInterval &OrigLI = LIS.getInterval(Original);
6060b57cec5SDimitry Andric   VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx);
6070b57cec5SDimitry Andric 
608e8d8bef9SDimitry Andric   Register Reg = LI->reg();
6090b57cec5SDimitry Andric   bool DidRemat = false;
6100b57cec5SDimitry Andric   if (OrigVNI) {
6110b57cec5SDimitry Andric     LiveRangeEdit::Remat RM(ParentVNI);
6120b57cec5SDimitry Andric     RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def);
6130b57cec5SDimitry Andric     if (Edit->canRematerializeAt(RM, OrigVNI, UseIdx, true)) {
6140b57cec5SDimitry Andric       Def = Edit->rematerializeAt(MBB, I, Reg, RM, TRI, Late);
6150b57cec5SDimitry Andric       ++NumRemats;
6160b57cec5SDimitry Andric       DidRemat = true;
6170b57cec5SDimitry Andric     }
6180b57cec5SDimitry Andric   }
6190b57cec5SDimitry Andric   if (!DidRemat) {
6200b57cec5SDimitry Andric     LaneBitmask LaneMask;
621e8d8bef9SDimitry Andric     if (OrigLI.hasSubRanges()) {
6220b57cec5SDimitry Andric       LaneMask = LaneBitmask::getNone();
623e8d8bef9SDimitry Andric       for (LiveInterval::SubRange &S : OrigLI.subranges()) {
624e8d8bef9SDimitry Andric         if (S.liveAt(UseIdx))
6250b57cec5SDimitry Andric           LaneMask |= S.LaneMask;
626e8d8bef9SDimitry Andric       }
6270b57cec5SDimitry Andric     } else {
6280b57cec5SDimitry Andric       LaneMask = LaneBitmask::getAll();
6290b57cec5SDimitry Andric     }
6300b57cec5SDimitry Andric 
631e8d8bef9SDimitry Andric     if (LaneMask.none()) {
632e8d8bef9SDimitry Andric       const MCInstrDesc &Desc = TII.get(TargetOpcode::IMPLICIT_DEF);
633e8d8bef9SDimitry Andric       MachineInstr *ImplicitDef = BuildMI(MBB, I, DebugLoc(), Desc, Reg);
634e8d8bef9SDimitry Andric       SlotIndexes &Indexes = *LIS.getSlotIndexes();
635e8d8bef9SDimitry Andric       Def = Indexes.insertMachineInstrInMaps(*ImplicitDef, Late).getRegSlot();
636e8d8bef9SDimitry Andric     } else {
6370b57cec5SDimitry Andric       ++NumCopies;
6380b57cec5SDimitry Andric       Def = buildCopy(Edit->getReg(), Reg, LaneMask, MBB, I, Late, RegIdx);
6390b57cec5SDimitry Andric     }
640e8d8bef9SDimitry Andric   }
6410b57cec5SDimitry Andric 
6420b57cec5SDimitry Andric   // Define the value in Reg.
6430b57cec5SDimitry Andric   return defValue(RegIdx, ParentVNI, Def, false);
6440b57cec5SDimitry Andric }
6450b57cec5SDimitry Andric 
6460b57cec5SDimitry Andric /// Create a new virtual register and live interval.
openIntv()6470b57cec5SDimitry Andric unsigned SplitEditor::openIntv() {
6480b57cec5SDimitry Andric   // Create the complement as index 0.
6490b57cec5SDimitry Andric   if (Edit->empty())
6500b57cec5SDimitry Andric     Edit->createEmptyInterval();
6510b57cec5SDimitry Andric 
6520b57cec5SDimitry Andric   // Create the open interval.
6530b57cec5SDimitry Andric   OpenIdx = Edit->size();
6540b57cec5SDimitry Andric   Edit->createEmptyInterval();
6550b57cec5SDimitry Andric   return OpenIdx;
6560b57cec5SDimitry Andric }
6570b57cec5SDimitry Andric 
selectIntv(unsigned Idx)6580b57cec5SDimitry Andric void SplitEditor::selectIntv(unsigned Idx) {
6590b57cec5SDimitry Andric   assert(Idx != 0 && "Cannot select the complement interval");
6600b57cec5SDimitry Andric   assert(Idx < Edit->size() && "Can only select previously opened interval");
6610b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "    selectIntv " << OpenIdx << " -> " << Idx << '\n');
6620b57cec5SDimitry Andric   OpenIdx = Idx;
6630b57cec5SDimitry Andric }
6640b57cec5SDimitry Andric 
enterIntvBefore(SlotIndex Idx)6650b57cec5SDimitry Andric SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
6660b57cec5SDimitry Andric   assert(OpenIdx && "openIntv not called before enterIntvBefore");
6670b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "    enterIntvBefore " << Idx);
6680b57cec5SDimitry Andric   Idx = Idx.getBaseIndex();
6690b57cec5SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
6700b57cec5SDimitry Andric   if (!ParentVNI) {
6710b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ": not live\n");
6720b57cec5SDimitry Andric     return Idx;
6730b57cec5SDimitry Andric   }
6740b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
6750b57cec5SDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
6760b57cec5SDimitry Andric   assert(MI && "enterIntvBefore called with invalid index");
6770b57cec5SDimitry Andric 
6780b57cec5SDimitry Andric   VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
6790b57cec5SDimitry Andric   return VNI->def;
6800b57cec5SDimitry Andric }
6810b57cec5SDimitry Andric 
enterIntvAfter(SlotIndex Idx)6820b57cec5SDimitry Andric SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
6830b57cec5SDimitry Andric   assert(OpenIdx && "openIntv not called before enterIntvAfter");
6840b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "    enterIntvAfter " << Idx);
6850b57cec5SDimitry Andric   Idx = Idx.getBoundaryIndex();
6860b57cec5SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
6870b57cec5SDimitry Andric   if (!ParentVNI) {
6880b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ": not live\n");
6890b57cec5SDimitry Andric     return Idx;
6900b57cec5SDimitry Andric   }
6910b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
6920b57cec5SDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
6930b57cec5SDimitry Andric   assert(MI && "enterIntvAfter called with invalid index");
6940b57cec5SDimitry Andric 
6950b57cec5SDimitry Andric   VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
6960b57cec5SDimitry Andric                               std::next(MachineBasicBlock::iterator(MI)));
6970b57cec5SDimitry Andric   return VNI->def;
6980b57cec5SDimitry Andric }
6990b57cec5SDimitry Andric 
enterIntvAtEnd(MachineBasicBlock & MBB)7000b57cec5SDimitry Andric SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
7010b57cec5SDimitry Andric   assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
7020b57cec5SDimitry Andric   SlotIndex End = LIS.getMBBEndIdx(&MBB);
7030b57cec5SDimitry Andric   SlotIndex Last = End.getPrevSlot();
7040b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "    enterIntvAtEnd " << printMBBReference(MBB) << ", "
7050b57cec5SDimitry Andric                     << Last);
7060b57cec5SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
7070b57cec5SDimitry Andric   if (!ParentVNI) {
7080b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ": not live\n");
7090b57cec5SDimitry Andric     return End;
7100b57cec5SDimitry Andric   }
711fe6060f1SDimitry Andric   SlotIndex LSP = SA.getLastSplitPoint(&MBB);
712fe6060f1SDimitry Andric   if (LSP < Last) {
713fe6060f1SDimitry Andric     // It could be that the use after LSP is a def, and thus the ParentVNI
714fe6060f1SDimitry Andric     // just selected starts at that def.  For this case to exist, the def
715fe6060f1SDimitry Andric     // must be part of a tied def/use pair (as otherwise we'd have split
716fe6060f1SDimitry Andric     // distinct live ranges into individual live intervals), and thus we
717fe6060f1SDimitry Andric     // can insert the def into the VNI of the use and the tied def/use
718fe6060f1SDimitry Andric     // pair can live in the resulting interval.
719fe6060f1SDimitry Andric     Last = LSP;
720fe6060f1SDimitry Andric     ParentVNI = Edit->getParent().getVNInfoAt(Last);
721fe6060f1SDimitry Andric     if (!ParentVNI) {
722fe6060f1SDimitry Andric       // undef use --> undef tied def
723fe6060f1SDimitry Andric       LLVM_DEBUG(dbgs() << ": tied use not live\n");
724fe6060f1SDimitry Andric       return End;
725fe6060f1SDimitry Andric     }
726fe6060f1SDimitry Andric   }
727fe6060f1SDimitry Andric 
7280b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id);
7290b57cec5SDimitry Andric   VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
7300b57cec5SDimitry Andric                               SA.getLastSplitPointIter(&MBB));
7310b57cec5SDimitry Andric   RegAssign.insert(VNI->def, End, OpenIdx);
7320b57cec5SDimitry Andric   LLVM_DEBUG(dump());
7330b57cec5SDimitry Andric   return VNI->def;
7340b57cec5SDimitry Andric }
7350b57cec5SDimitry Andric 
7360b57cec5SDimitry Andric /// useIntv - indicate that all instructions in MBB should use OpenLI.
useIntv(const MachineBasicBlock & MBB)7370b57cec5SDimitry Andric void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
7380b57cec5SDimitry Andric   useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
7390b57cec5SDimitry Andric }
7400b57cec5SDimitry Andric 
useIntv(SlotIndex Start,SlotIndex End)7410b57cec5SDimitry Andric void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
7420b57cec5SDimitry Andric   assert(OpenIdx && "openIntv not called before useIntv");
7430b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "    useIntv [" << Start << ';' << End << "):");
7440b57cec5SDimitry Andric   RegAssign.insert(Start, End, OpenIdx);
7450b57cec5SDimitry Andric   LLVM_DEBUG(dump());
7460b57cec5SDimitry Andric }
7470b57cec5SDimitry Andric 
leaveIntvAfter(SlotIndex Idx)7480b57cec5SDimitry Andric SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
7490b57cec5SDimitry Andric   assert(OpenIdx && "openIntv not called before leaveIntvAfter");
7500b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "    leaveIntvAfter " << Idx);
7510b57cec5SDimitry Andric 
7520b57cec5SDimitry Andric   // The interval must be live beyond the instruction at Idx.
7530b57cec5SDimitry Andric   SlotIndex Boundary = Idx.getBoundaryIndex();
7540b57cec5SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
7550b57cec5SDimitry Andric   if (!ParentVNI) {
7560b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ": not live\n");
7570b57cec5SDimitry Andric     return Boundary.getNextSlot();
7580b57cec5SDimitry Andric   }
7590b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
7600b57cec5SDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
7610b57cec5SDimitry Andric   assert(MI && "No instruction at index");
7620b57cec5SDimitry Andric 
7630b57cec5SDimitry Andric   // In spill mode, make live ranges as short as possible by inserting the copy
7640b57cec5SDimitry Andric   // before MI.  This is only possible if that instruction doesn't redefine the
7650b57cec5SDimitry Andric   // value.  The inserted COPY is not a kill, and we don't need to recompute
7660b57cec5SDimitry Andric   // the source live range.  The spiller also won't try to hoist this copy.
7670b57cec5SDimitry Andric   if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
7680b57cec5SDimitry Andric       MI->readsVirtualRegister(Edit->getReg())) {
7690b57cec5SDimitry Andric     forceRecompute(0, *ParentVNI);
7700b57cec5SDimitry Andric     defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
7710b57cec5SDimitry Andric     return Idx;
7720b57cec5SDimitry Andric   }
7730b57cec5SDimitry Andric 
7740b57cec5SDimitry Andric   VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
7750b57cec5SDimitry Andric                               std::next(MachineBasicBlock::iterator(MI)));
7760b57cec5SDimitry Andric   return VNI->def;
7770b57cec5SDimitry Andric }
7780b57cec5SDimitry Andric 
leaveIntvBefore(SlotIndex Idx)7790b57cec5SDimitry Andric SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
7800b57cec5SDimitry Andric   assert(OpenIdx && "openIntv not called before leaveIntvBefore");
7810b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "    leaveIntvBefore " << Idx);
7820b57cec5SDimitry Andric 
7830b57cec5SDimitry Andric   // The interval must be live into the instruction at Idx.
7840b57cec5SDimitry Andric   Idx = Idx.getBaseIndex();
7850b57cec5SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
7860b57cec5SDimitry Andric   if (!ParentVNI) {
7870b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ": not live\n");
7880b57cec5SDimitry Andric     return Idx.getNextSlot();
7890b57cec5SDimitry Andric   }
7900b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
7910b57cec5SDimitry Andric 
7920b57cec5SDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
7930b57cec5SDimitry Andric   assert(MI && "No instruction at index");
7940b57cec5SDimitry Andric   VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
7950b57cec5SDimitry Andric   return VNI->def;
7960b57cec5SDimitry Andric }
7970b57cec5SDimitry Andric 
leaveIntvAtTop(MachineBasicBlock & MBB)7980b57cec5SDimitry Andric SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
7990b57cec5SDimitry Andric   assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
8000b57cec5SDimitry Andric   SlotIndex Start = LIS.getMBBStartIdx(&MBB);
8010b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "    leaveIntvAtTop " << printMBBReference(MBB) << ", "
8020b57cec5SDimitry Andric                     << Start);
8030b57cec5SDimitry Andric 
8040b57cec5SDimitry Andric   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
8050b57cec5SDimitry Andric   if (!ParentVNI) {
8060b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ": not live\n");
8070b57cec5SDimitry Andric     return Start;
8080b57cec5SDimitry Andric   }
8090b57cec5SDimitry Andric 
8105f757f3fSDimitry Andric   unsigned RegIdx = 0;
8115f757f3fSDimitry Andric   Register Reg = LIS.getInterval(Edit->get(RegIdx)).reg();
8125f757f3fSDimitry Andric   VNInfo *VNI = defFromParent(RegIdx, ParentVNI, Start, MBB,
8135f757f3fSDimitry Andric                               MBB.SkipPHIsLabelsAndDebug(MBB.begin(), Reg));
8140b57cec5SDimitry Andric   RegAssign.insert(Start, VNI->def, OpenIdx);
8150b57cec5SDimitry Andric   LLVM_DEBUG(dump());
8160b57cec5SDimitry Andric   return VNI->def;
8170b57cec5SDimitry Andric }
8180b57cec5SDimitry Andric 
hasTiedUseOf(MachineInstr & MI,unsigned Reg)819fe6060f1SDimitry Andric static bool hasTiedUseOf(MachineInstr &MI, unsigned Reg) {
820fe6060f1SDimitry Andric   return any_of(MI.defs(), [Reg](const MachineOperand &MO) {
821fe6060f1SDimitry Andric     return MO.isReg() && MO.isTied() && MO.getReg() == Reg;
822fe6060f1SDimitry Andric   });
823fe6060f1SDimitry Andric }
824fe6060f1SDimitry Andric 
overlapIntv(SlotIndex Start,SlotIndex End)8250b57cec5SDimitry Andric void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
8260b57cec5SDimitry Andric   assert(OpenIdx && "openIntv not called before overlapIntv");
8270b57cec5SDimitry Andric   const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
8280b57cec5SDimitry Andric   assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
8290b57cec5SDimitry Andric          "Parent changes value in extended range");
8300b57cec5SDimitry Andric   assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
8310b57cec5SDimitry Andric          "Range cannot span basic blocks");
8320b57cec5SDimitry Andric 
8335ffd83dbSDimitry Andric   // The complement interval will be extended as needed by LICalc.extend().
8340b57cec5SDimitry Andric   if (ParentVNI)
8350b57cec5SDimitry Andric     forceRecompute(0, *ParentVNI);
836fe6060f1SDimitry Andric 
837fe6060f1SDimitry Andric   // If the last use is tied to a def, we can't mark it as live for the
838fe6060f1SDimitry Andric   // interval which includes only the use.  That would cause the tied pair
839fe6060f1SDimitry Andric   // to end up in two different intervals.
840fe6060f1SDimitry Andric   if (auto *MI = LIS.getInstructionFromIndex(End))
841fe6060f1SDimitry Andric     if (hasTiedUseOf(*MI, Edit->getReg())) {
842fe6060f1SDimitry Andric       LLVM_DEBUG(dbgs() << "skip overlap due to tied def at end\n");
843fe6060f1SDimitry Andric       return;
844fe6060f1SDimitry Andric     }
845fe6060f1SDimitry Andric 
8460b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "    overlapIntv [" << Start << ';' << End << "):");
8470b57cec5SDimitry Andric   RegAssign.insert(Start, End, OpenIdx);
8480b57cec5SDimitry Andric   LLVM_DEBUG(dump());
8490b57cec5SDimitry Andric }
8500b57cec5SDimitry Andric 
8510b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8520b57cec5SDimitry Andric //                                  Spill modes
8530b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8540b57cec5SDimitry Andric 
removeBackCopies(SmallVectorImpl<VNInfo * > & Copies)8550b57cec5SDimitry Andric void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
8560b57cec5SDimitry Andric   LiveInterval *LI = &LIS.getInterval(Edit->get(0));
8570b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
8580b57cec5SDimitry Andric   RegAssignMap::iterator AssignI;
8590b57cec5SDimitry Andric   AssignI.setMap(RegAssign);
8600b57cec5SDimitry Andric 
861fe6060f1SDimitry Andric   for (const VNInfo *C : Copies) {
862fe6060f1SDimitry Andric     SlotIndex Def = C->def;
8630b57cec5SDimitry Andric     MachineInstr *MI = LIS.getInstructionFromIndex(Def);
8640b57cec5SDimitry Andric     assert(MI && "No instruction for back-copy");
8650b57cec5SDimitry Andric 
8660b57cec5SDimitry Andric     MachineBasicBlock *MBB = MI->getParent();
8670b57cec5SDimitry Andric     MachineBasicBlock::iterator MBBI(MI);
8680b57cec5SDimitry Andric     bool AtBegin;
8690b57cec5SDimitry Andric     do AtBegin = MBBI == MBB->begin();
870fe6060f1SDimitry Andric     while (!AtBegin && (--MBBI)->isDebugOrPseudoInstr());
8710b57cec5SDimitry Andric 
8720b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
8730b57cec5SDimitry Andric     LIS.removeVRegDefAt(*LI, Def);
8740b57cec5SDimitry Andric     LIS.RemoveMachineInstrFromMaps(*MI);
8750b57cec5SDimitry Andric     MI->eraseFromParent();
8760b57cec5SDimitry Andric 
8770b57cec5SDimitry Andric     // Adjust RegAssign if a register assignment is killed at Def. We want to
8780b57cec5SDimitry Andric     // avoid calculating the live range of the source register if possible.
8790b57cec5SDimitry Andric     AssignI.find(Def.getPrevSlot());
8800b57cec5SDimitry Andric     if (!AssignI.valid() || AssignI.start() >= Def)
8810b57cec5SDimitry Andric       continue;
8820b57cec5SDimitry Andric     // If MI doesn't kill the assigned register, just leave it.
8830b57cec5SDimitry Andric     if (AssignI.stop() != Def)
8840b57cec5SDimitry Andric       continue;
8850b57cec5SDimitry Andric     unsigned RegIdx = AssignI.value();
886fe6060f1SDimitry Andric     // We could hoist back-copy right after another back-copy. As a result
887fe6060f1SDimitry Andric     // MMBI points to copy instruction which is actually dead now.
888fe6060f1SDimitry Andric     // We cannot set its stop to MBBI which will be the same as start and
889fe6060f1SDimitry Andric     // interval does not support that.
890fe6060f1SDimitry Andric     SlotIndex Kill =
891fe6060f1SDimitry Andric         AtBegin ? SlotIndex() : LIS.getInstructionIndex(*MBBI).getRegSlot();
892fe6060f1SDimitry Andric     if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg()) ||
893fe6060f1SDimitry Andric         Kill <= AssignI.start()) {
8940b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "  cannot find simple kill of RegIdx " << RegIdx
8950b57cec5SDimitry Andric                         << '\n');
8960b57cec5SDimitry Andric       forceRecompute(RegIdx, *Edit->getParent().getVNInfoAt(Def));
8970b57cec5SDimitry Andric     } else {
8980b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "  move kill to " << Kill << '\t' << *MBBI);
8990b57cec5SDimitry Andric       AssignI.setStop(Kill);
9000b57cec5SDimitry Andric     }
9010b57cec5SDimitry Andric   }
9020b57cec5SDimitry Andric }
9030b57cec5SDimitry Andric 
9040b57cec5SDimitry Andric MachineBasicBlock*
findShallowDominator(MachineBasicBlock * MBB,MachineBasicBlock * DefMBB)9050b57cec5SDimitry Andric SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
9060b57cec5SDimitry Andric                                   MachineBasicBlock *DefMBB) {
9070b57cec5SDimitry Andric   if (MBB == DefMBB)
9080b57cec5SDimitry Andric     return MBB;
9090b57cec5SDimitry Andric   assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
9100b57cec5SDimitry Andric 
9110b57cec5SDimitry Andric   const MachineLoopInfo &Loops = SA.Loops;
9120b57cec5SDimitry Andric   const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
9130b57cec5SDimitry Andric   MachineDomTreeNode *DefDomNode = MDT[DefMBB];
9140b57cec5SDimitry Andric 
9150b57cec5SDimitry Andric   // Best candidate so far.
9160b57cec5SDimitry Andric   MachineBasicBlock *BestMBB = MBB;
9170b57cec5SDimitry Andric   unsigned BestDepth = std::numeric_limits<unsigned>::max();
9180b57cec5SDimitry Andric 
9190b57cec5SDimitry Andric   while (true) {
9200b57cec5SDimitry Andric     const MachineLoop *Loop = Loops.getLoopFor(MBB);
9210b57cec5SDimitry Andric 
9220b57cec5SDimitry Andric     // MBB isn't in a loop, it doesn't get any better.  All dominators have a
9230b57cec5SDimitry Andric     // higher frequency by definition.
9240b57cec5SDimitry Andric     if (!Loop) {
9250b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
9260b57cec5SDimitry Andric                         << " dominates " << printMBBReference(*MBB)
9270b57cec5SDimitry Andric                         << " at depth 0\n");
9280b57cec5SDimitry Andric       return MBB;
9290b57cec5SDimitry Andric     }
9300b57cec5SDimitry Andric 
9310b57cec5SDimitry Andric     // We'll never be able to exit the DefLoop.
9320b57cec5SDimitry Andric     if (Loop == DefLoop) {
9330b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
9340b57cec5SDimitry Andric                         << " dominates " << printMBBReference(*MBB)
9350b57cec5SDimitry Andric                         << " in the same loop\n");
9360b57cec5SDimitry Andric       return MBB;
9370b57cec5SDimitry Andric     }
9380b57cec5SDimitry Andric 
9390b57cec5SDimitry Andric     // Least busy dominator seen so far.
9400b57cec5SDimitry Andric     unsigned Depth = Loop->getLoopDepth();
9410b57cec5SDimitry Andric     if (Depth < BestDepth) {
9420b57cec5SDimitry Andric       BestMBB = MBB;
9430b57cec5SDimitry Andric       BestDepth = Depth;
9440b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
9450b57cec5SDimitry Andric                         << " dominates " << printMBBReference(*MBB)
9460b57cec5SDimitry Andric                         << " at depth " << Depth << '\n');
9470b57cec5SDimitry Andric     }
9480b57cec5SDimitry Andric 
9490b57cec5SDimitry Andric     // Leave loop by going to the immediate dominator of the loop header.
9500b57cec5SDimitry Andric     // This is a bigger stride than simply walking up the dominator tree.
9510b57cec5SDimitry Andric     MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
9520b57cec5SDimitry Andric 
9530b57cec5SDimitry Andric     // Too far up the dominator tree?
9540b57cec5SDimitry Andric     if (!IDom || !MDT.dominates(DefDomNode, IDom))
9550b57cec5SDimitry Andric       return BestMBB;
9560b57cec5SDimitry Andric 
9570b57cec5SDimitry Andric     MBB = IDom->getBlock();
9580b57cec5SDimitry Andric   }
9590b57cec5SDimitry Andric }
9600b57cec5SDimitry Andric 
computeRedundantBackCopies(DenseSet<unsigned> & NotToHoistSet,SmallVectorImpl<VNInfo * > & BackCopies)9610b57cec5SDimitry Andric void SplitEditor::computeRedundantBackCopies(
9620b57cec5SDimitry Andric     DenseSet<unsigned> &NotToHoistSet, SmallVectorImpl<VNInfo *> &BackCopies) {
9630b57cec5SDimitry Andric   LiveInterval *LI = &LIS.getInterval(Edit->get(0));
96481ad6265SDimitry Andric   const LiveInterval *Parent = &Edit->getParent();
9650b57cec5SDimitry Andric   SmallVector<SmallPtrSet<VNInfo *, 8>, 8> EqualVNs(Parent->getNumValNums());
9660b57cec5SDimitry Andric   SmallPtrSet<VNInfo *, 8> DominatedVNIs;
9670b57cec5SDimitry Andric 
9680b57cec5SDimitry Andric   // Aggregate VNIs having the same value as ParentVNI.
9690b57cec5SDimitry Andric   for (VNInfo *VNI : LI->valnos) {
9700b57cec5SDimitry Andric     if (VNI->isUnused())
9710b57cec5SDimitry Andric       continue;
9720b57cec5SDimitry Andric     VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
9730b57cec5SDimitry Andric     EqualVNs[ParentVNI->id].insert(VNI);
9740b57cec5SDimitry Andric   }
9750b57cec5SDimitry Andric 
9760b57cec5SDimitry Andric   // For VNI aggregation of each ParentVNI, collect dominated, i.e.,
9770b57cec5SDimitry Andric   // redundant VNIs to BackCopies.
9780b57cec5SDimitry Andric   for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
97981ad6265SDimitry Andric     const VNInfo *ParentVNI = Parent->getValNumInfo(i);
9800b57cec5SDimitry Andric     if (!NotToHoistSet.count(ParentVNI->id))
9810b57cec5SDimitry Andric       continue;
9820b57cec5SDimitry Andric     SmallPtrSetIterator<VNInfo *> It1 = EqualVNs[ParentVNI->id].begin();
9830b57cec5SDimitry Andric     SmallPtrSetIterator<VNInfo *> It2 = It1;
9840b57cec5SDimitry Andric     for (; It1 != EqualVNs[ParentVNI->id].end(); ++It1) {
9850b57cec5SDimitry Andric       It2 = It1;
9860b57cec5SDimitry Andric       for (++It2; It2 != EqualVNs[ParentVNI->id].end(); ++It2) {
9870b57cec5SDimitry Andric         if (DominatedVNIs.count(*It1) || DominatedVNIs.count(*It2))
9880b57cec5SDimitry Andric           continue;
9890b57cec5SDimitry Andric 
9900b57cec5SDimitry Andric         MachineBasicBlock *MBB1 = LIS.getMBBFromIndex((*It1)->def);
9910b57cec5SDimitry Andric         MachineBasicBlock *MBB2 = LIS.getMBBFromIndex((*It2)->def);
9920b57cec5SDimitry Andric         if (MBB1 == MBB2) {
9930b57cec5SDimitry Andric           DominatedVNIs.insert((*It1)->def < (*It2)->def ? (*It2) : (*It1));
9940b57cec5SDimitry Andric         } else if (MDT.dominates(MBB1, MBB2)) {
9950b57cec5SDimitry Andric           DominatedVNIs.insert(*It2);
9960b57cec5SDimitry Andric         } else if (MDT.dominates(MBB2, MBB1)) {
9970b57cec5SDimitry Andric           DominatedVNIs.insert(*It1);
9980b57cec5SDimitry Andric         }
9990b57cec5SDimitry Andric       }
10000b57cec5SDimitry Andric     }
10010b57cec5SDimitry Andric     if (!DominatedVNIs.empty()) {
10020b57cec5SDimitry Andric       forceRecompute(0, *ParentVNI);
1003e8d8bef9SDimitry Andric       append_range(BackCopies, DominatedVNIs);
10040b57cec5SDimitry Andric       DominatedVNIs.clear();
10050b57cec5SDimitry Andric     }
10060b57cec5SDimitry Andric   }
10070b57cec5SDimitry Andric }
10080b57cec5SDimitry Andric 
10090b57cec5SDimitry Andric /// For SM_Size mode, find a common dominator for all the back-copies for
10100b57cec5SDimitry Andric /// the same ParentVNI and hoist the backcopies to the dominator BB.
10110b57cec5SDimitry Andric /// For SM_Speed mode, if the common dominator is hot and it is not beneficial
10120b57cec5SDimitry Andric /// to do the hoisting, simply remove the dominated backcopies for the same
10130b57cec5SDimitry Andric /// ParentVNI.
hoistCopies()10140b57cec5SDimitry Andric void SplitEditor::hoistCopies() {
10150b57cec5SDimitry Andric   // Get the complement interval, always RegIdx 0.
10160b57cec5SDimitry Andric   LiveInterval *LI = &LIS.getInterval(Edit->get(0));
101781ad6265SDimitry Andric   const LiveInterval *Parent = &Edit->getParent();
10180b57cec5SDimitry Andric 
10190b57cec5SDimitry Andric   // Track the nearest common dominator for all back-copies for each ParentVNI,
10200b57cec5SDimitry Andric   // indexed by ParentVNI->id.
10210b57cec5SDimitry Andric   using DomPair = std::pair<MachineBasicBlock *, SlotIndex>;
10220b57cec5SDimitry Andric   SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
10230b57cec5SDimitry Andric   // The total cost of all the back-copies for each ParentVNI.
10240b57cec5SDimitry Andric   SmallVector<BlockFrequency, 8> Costs(Parent->getNumValNums());
10250b57cec5SDimitry Andric   // The ParentVNI->id set for which hoisting back-copies are not beneficial
10260b57cec5SDimitry Andric   // for Speed.
10270b57cec5SDimitry Andric   DenseSet<unsigned> NotToHoistSet;
10280b57cec5SDimitry Andric 
10290b57cec5SDimitry Andric   // Find the nearest common dominator for parent values with multiple
10300b57cec5SDimitry Andric   // back-copies.  If a single back-copy dominates, put it in DomPair.second.
10310b57cec5SDimitry Andric   for (VNInfo *VNI : LI->valnos) {
10320b57cec5SDimitry Andric     if (VNI->isUnused())
10330b57cec5SDimitry Andric       continue;
10340b57cec5SDimitry Andric     VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
10350b57cec5SDimitry Andric     assert(ParentVNI && "Parent not live at complement def");
10360b57cec5SDimitry Andric 
10370b57cec5SDimitry Andric     // Don't hoist remats.  The complement is probably going to disappear
10380b57cec5SDimitry Andric     // completely anyway.
10390b57cec5SDimitry Andric     if (Edit->didRematerialize(ParentVNI))
10400b57cec5SDimitry Andric       continue;
10410b57cec5SDimitry Andric 
10420b57cec5SDimitry Andric     MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
10430b57cec5SDimitry Andric 
10440b57cec5SDimitry Andric     DomPair &Dom = NearestDom[ParentVNI->id];
10450b57cec5SDimitry Andric 
10460b57cec5SDimitry Andric     // Keep directly defined parent values.  This is either a PHI or an
10470b57cec5SDimitry Andric     // instruction in the complement range.  All other copies of ParentVNI
10480b57cec5SDimitry Andric     // should be eliminated.
10490b57cec5SDimitry Andric     if (VNI->def == ParentVNI->def) {
10500b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
10510b57cec5SDimitry Andric       Dom = DomPair(ValMBB, VNI->def);
10520b57cec5SDimitry Andric       continue;
10530b57cec5SDimitry Andric     }
10540b57cec5SDimitry Andric     // Skip the singly mapped values.  There is nothing to gain from hoisting a
10550b57cec5SDimitry Andric     // single back-copy.
10560b57cec5SDimitry Andric     if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
10570b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
10580b57cec5SDimitry Andric       continue;
10590b57cec5SDimitry Andric     }
10600b57cec5SDimitry Andric 
10610b57cec5SDimitry Andric     if (!Dom.first) {
10620b57cec5SDimitry Andric       // First time we see ParentVNI.  VNI dominates itself.
10630b57cec5SDimitry Andric       Dom = DomPair(ValMBB, VNI->def);
10640b57cec5SDimitry Andric     } else if (Dom.first == ValMBB) {
10650b57cec5SDimitry Andric       // Two defs in the same block.  Pick the earlier def.
10660b57cec5SDimitry Andric       if (!Dom.second.isValid() || VNI->def < Dom.second)
10670b57cec5SDimitry Andric         Dom.second = VNI->def;
10680b57cec5SDimitry Andric     } else {
10690b57cec5SDimitry Andric       // Different basic blocks. Check if one dominates.
10700b57cec5SDimitry Andric       MachineBasicBlock *Near =
10710b57cec5SDimitry Andric         MDT.findNearestCommonDominator(Dom.first, ValMBB);
10720b57cec5SDimitry Andric       if (Near == ValMBB)
10730b57cec5SDimitry Andric         // Def ValMBB dominates.
10740b57cec5SDimitry Andric         Dom = DomPair(ValMBB, VNI->def);
10750b57cec5SDimitry Andric       else if (Near != Dom.first)
10760b57cec5SDimitry Andric         // None dominate. Hoist to common dominator, need new def.
10770b57cec5SDimitry Andric         Dom = DomPair(Near, SlotIndex());
10780b57cec5SDimitry Andric       Costs[ParentVNI->id] += MBFI.getBlockFreq(ValMBB);
10790b57cec5SDimitry Andric     }
10800b57cec5SDimitry Andric 
10810b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@'
10820b57cec5SDimitry Andric                       << VNI->def << " for parent " << ParentVNI->id << '@'
10830b57cec5SDimitry Andric                       << ParentVNI->def << " hoist to "
10840b57cec5SDimitry Andric                       << printMBBReference(*Dom.first) << ' ' << Dom.second
10850b57cec5SDimitry Andric                       << '\n');
10860b57cec5SDimitry Andric   }
10870b57cec5SDimitry Andric 
10880b57cec5SDimitry Andric   // Insert the hoisted copies.
10890b57cec5SDimitry Andric   for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
10900b57cec5SDimitry Andric     DomPair &Dom = NearestDom[i];
10910b57cec5SDimitry Andric     if (!Dom.first || Dom.second.isValid())
10920b57cec5SDimitry Andric       continue;
10930b57cec5SDimitry Andric     // This value needs a hoisted copy inserted at the end of Dom.first.
109481ad6265SDimitry Andric     const VNInfo *ParentVNI = Parent->getValNumInfo(i);
10950b57cec5SDimitry Andric     MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
10960b57cec5SDimitry Andric     // Get a less loopy dominator than Dom.first.
10970b57cec5SDimitry Andric     Dom.first = findShallowDominator(Dom.first, DefMBB);
10980b57cec5SDimitry Andric     if (SpillMode == SM_Speed &&
10990b57cec5SDimitry Andric         MBFI.getBlockFreq(Dom.first) > Costs[ParentVNI->id]) {
11000b57cec5SDimitry Andric       NotToHoistSet.insert(ParentVNI->id);
11010b57cec5SDimitry Andric       continue;
11020b57cec5SDimitry Andric     }
1103fe6060f1SDimitry Andric     SlotIndex LSP = SA.getLastSplitPoint(Dom.first);
1104fe6060f1SDimitry Andric     if (LSP <= ParentVNI->def) {
1105fe6060f1SDimitry Andric       NotToHoistSet.insert(ParentVNI->id);
1106fe6060f1SDimitry Andric       continue;
1107fe6060f1SDimitry Andric     }
1108fe6060f1SDimitry Andric     Dom.second = defFromParent(0, ParentVNI, LSP, *Dom.first,
11090b57cec5SDimitry Andric                                SA.getLastSplitPointIter(Dom.first))->def;
11100b57cec5SDimitry Andric   }
11110b57cec5SDimitry Andric 
11120b57cec5SDimitry Andric   // Remove redundant back-copies that are now known to be dominated by another
11130b57cec5SDimitry Andric   // def with the same value.
11140b57cec5SDimitry Andric   SmallVector<VNInfo*, 8> BackCopies;
11150b57cec5SDimitry Andric   for (VNInfo *VNI : LI->valnos) {
11160b57cec5SDimitry Andric     if (VNI->isUnused())
11170b57cec5SDimitry Andric       continue;
11180b57cec5SDimitry Andric     VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
11190b57cec5SDimitry Andric     const DomPair &Dom = NearestDom[ParentVNI->id];
11200b57cec5SDimitry Andric     if (!Dom.first || Dom.second == VNI->def ||
11210b57cec5SDimitry Andric         NotToHoistSet.count(ParentVNI->id))
11220b57cec5SDimitry Andric       continue;
11230b57cec5SDimitry Andric     BackCopies.push_back(VNI);
11240b57cec5SDimitry Andric     forceRecompute(0, *ParentVNI);
11250b57cec5SDimitry Andric   }
11260b57cec5SDimitry Andric 
11270b57cec5SDimitry Andric   // If it is not beneficial to hoist all the BackCopies, simply remove
11280b57cec5SDimitry Andric   // redundant BackCopies in speed mode.
11290b57cec5SDimitry Andric   if (SpillMode == SM_Speed && !NotToHoistSet.empty())
11300b57cec5SDimitry Andric     computeRedundantBackCopies(NotToHoistSet, BackCopies);
11310b57cec5SDimitry Andric 
11320b57cec5SDimitry Andric   removeBackCopies(BackCopies);
11330b57cec5SDimitry Andric }
11340b57cec5SDimitry Andric 
11350b57cec5SDimitry Andric /// transferValues - Transfer all possible values to the new live ranges.
11365ffd83dbSDimitry Andric /// Values that were rematerialized are left alone, they need LICalc.extend().
transferValues()11370b57cec5SDimitry Andric bool SplitEditor::transferValues() {
11380b57cec5SDimitry Andric   bool Skipped = false;
11390b57cec5SDimitry Andric   RegAssignMap::const_iterator AssignI = RegAssign.begin();
11400b57cec5SDimitry Andric   for (const LiveRange::Segment &S : Edit->getParent()) {
11410b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "  blit " << S << ':');
11420b57cec5SDimitry Andric     VNInfo *ParentVNI = S.valno;
11430b57cec5SDimitry Andric     // RegAssign has holes where RegIdx 0 should be used.
11440b57cec5SDimitry Andric     SlotIndex Start = S.start;
11450b57cec5SDimitry Andric     AssignI.advanceTo(Start);
11460b57cec5SDimitry Andric     do {
11470b57cec5SDimitry Andric       unsigned RegIdx;
11480b57cec5SDimitry Andric       SlotIndex End = S.end;
11490b57cec5SDimitry Andric       if (!AssignI.valid()) {
11500b57cec5SDimitry Andric         RegIdx = 0;
11510b57cec5SDimitry Andric       } else if (AssignI.start() <= Start) {
11520b57cec5SDimitry Andric         RegIdx = AssignI.value();
11530b57cec5SDimitry Andric         if (AssignI.stop() < End) {
11540b57cec5SDimitry Andric           End = AssignI.stop();
11550b57cec5SDimitry Andric           ++AssignI;
11560b57cec5SDimitry Andric         }
11570b57cec5SDimitry Andric       } else {
11580b57cec5SDimitry Andric         RegIdx = 0;
11590b57cec5SDimitry Andric         End = std::min(End, AssignI.start());
11600b57cec5SDimitry Andric       }
11610b57cec5SDimitry Andric 
11620b57cec5SDimitry Andric       // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
11630b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx << '('
11640b57cec5SDimitry Andric                         << printReg(Edit->get(RegIdx)) << ')');
11650b57cec5SDimitry Andric       LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
11660b57cec5SDimitry Andric 
11670b57cec5SDimitry Andric       // Check for a simply defined value that can be blitted directly.
11680b57cec5SDimitry Andric       ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
11690b57cec5SDimitry Andric       if (VNInfo *VNI = VFP.getPointer()) {
11700b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << ':' << VNI->id);
11710b57cec5SDimitry Andric         LI.addSegment(LiveInterval::Segment(Start, End, VNI));
11720b57cec5SDimitry Andric         Start = End;
11730b57cec5SDimitry Andric         continue;
11740b57cec5SDimitry Andric       }
11750b57cec5SDimitry Andric 
11760b57cec5SDimitry Andric       // Skip values with forced recomputation.
11770b57cec5SDimitry Andric       if (VFP.getInt()) {
11780b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "(recalc)");
11790b57cec5SDimitry Andric         Skipped = true;
11800b57cec5SDimitry Andric         Start = End;
11810b57cec5SDimitry Andric         continue;
11820b57cec5SDimitry Andric       }
11830b57cec5SDimitry Andric 
11845ffd83dbSDimitry Andric       LiveIntervalCalc &LIC = getLICalc(RegIdx);
11850b57cec5SDimitry Andric 
11860b57cec5SDimitry Andric       // This value has multiple defs in RegIdx, but it wasn't rematerialized,
11870b57cec5SDimitry Andric       // so the live range is accurate. Add live-in blocks in [Start;End) to the
11880b57cec5SDimitry Andric       // LiveInBlocks.
11890b57cec5SDimitry Andric       MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
11900b57cec5SDimitry Andric       SlotIndex BlockStart, BlockEnd;
11910b57cec5SDimitry Andric       std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(&*MBB);
11920b57cec5SDimitry Andric 
11930b57cec5SDimitry Andric       // The first block may be live-in, or it may have its own def.
11940b57cec5SDimitry Andric       if (Start != BlockStart) {
11950b57cec5SDimitry Andric         VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
11960b57cec5SDimitry Andric         assert(VNI && "Missing def for complex mapped value");
11970b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << ':' << VNI->id << "*" << printMBBReference(*MBB));
11980b57cec5SDimitry Andric         // MBB has its own def. Is it also live-out?
11990b57cec5SDimitry Andric         if (BlockEnd <= End)
12005ffd83dbSDimitry Andric           LIC.setLiveOutValue(&*MBB, VNI);
12010b57cec5SDimitry Andric 
12020b57cec5SDimitry Andric         // Skip to the next block for live-in.
12030b57cec5SDimitry Andric         ++MBB;
12040b57cec5SDimitry Andric         BlockStart = BlockEnd;
12050b57cec5SDimitry Andric       }
12060b57cec5SDimitry Andric 
12070b57cec5SDimitry Andric       // Handle the live-in blocks covered by [Start;End).
12080b57cec5SDimitry Andric       assert(Start <= BlockStart && "Expected live-in block");
12090b57cec5SDimitry Andric       while (BlockStart < End) {
12100b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << ">" << printMBBReference(*MBB));
12110b57cec5SDimitry Andric         BlockEnd = LIS.getMBBEndIdx(&*MBB);
12120b57cec5SDimitry Andric         if (BlockStart == ParentVNI->def) {
12130b57cec5SDimitry Andric           // This block has the def of a parent PHI, so it isn't live-in.
12140b57cec5SDimitry Andric           assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
12150b57cec5SDimitry Andric           VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
12160b57cec5SDimitry Andric           assert(VNI && "Missing def for complex mapped parent PHI");
12170b57cec5SDimitry Andric           if (End >= BlockEnd)
12185ffd83dbSDimitry Andric             LIC.setLiveOutValue(&*MBB, VNI); // Live-out as well.
12190b57cec5SDimitry Andric         } else {
12200b57cec5SDimitry Andric           // This block needs a live-in value.  The last block covered may not
12210b57cec5SDimitry Andric           // be live-out.
12220b57cec5SDimitry Andric           if (End < BlockEnd)
12235ffd83dbSDimitry Andric             LIC.addLiveInBlock(LI, MDT[&*MBB], End);
12240b57cec5SDimitry Andric           else {
12250b57cec5SDimitry Andric             // Live-through, and we don't know the value.
12265ffd83dbSDimitry Andric             LIC.addLiveInBlock(LI, MDT[&*MBB]);
12275ffd83dbSDimitry Andric             LIC.setLiveOutValue(&*MBB, nullptr);
12280b57cec5SDimitry Andric           }
12290b57cec5SDimitry Andric         }
12300b57cec5SDimitry Andric         BlockStart = BlockEnd;
12310b57cec5SDimitry Andric         ++MBB;
12320b57cec5SDimitry Andric       }
12330b57cec5SDimitry Andric       Start = End;
12340b57cec5SDimitry Andric     } while (Start != S.end);
12350b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << '\n');
12360b57cec5SDimitry Andric   }
12370b57cec5SDimitry Andric 
12385ffd83dbSDimitry Andric   LICalc[0].calculateValues();
12390b57cec5SDimitry Andric   if (SpillMode)
12405ffd83dbSDimitry Andric     LICalc[1].calculateValues();
12410b57cec5SDimitry Andric 
12420b57cec5SDimitry Andric   return Skipped;
12430b57cec5SDimitry Andric }
12440b57cec5SDimitry Andric 
removeDeadSegment(SlotIndex Def,LiveRange & LR)12450b57cec5SDimitry Andric static bool removeDeadSegment(SlotIndex Def, LiveRange &LR) {
12460b57cec5SDimitry Andric   const LiveRange::Segment *Seg = LR.getSegmentContaining(Def);
12470b57cec5SDimitry Andric   if (Seg == nullptr)
12480b57cec5SDimitry Andric     return true;
12490b57cec5SDimitry Andric   if (Seg->end != Def.getDeadSlot())
12500b57cec5SDimitry Andric     return false;
12510b57cec5SDimitry Andric   // This is a dead PHI. Remove it.
12520b57cec5SDimitry Andric   LR.removeSegment(*Seg, true);
12530b57cec5SDimitry Andric   return true;
12540b57cec5SDimitry Andric }
12550b57cec5SDimitry Andric 
extendPHIRange(MachineBasicBlock & B,LiveIntervalCalc & LIC,LiveRange & LR,LaneBitmask LM,ArrayRef<SlotIndex> Undefs)12565ffd83dbSDimitry Andric void SplitEditor::extendPHIRange(MachineBasicBlock &B, LiveIntervalCalc &LIC,
12570b57cec5SDimitry Andric                                  LiveRange &LR, LaneBitmask LM,
12580b57cec5SDimitry Andric                                  ArrayRef<SlotIndex> Undefs) {
12590b57cec5SDimitry Andric   for (MachineBasicBlock *P : B.predecessors()) {
12600b57cec5SDimitry Andric     SlotIndex End = LIS.getMBBEndIdx(P);
12610b57cec5SDimitry Andric     SlotIndex LastUse = End.getPrevSlot();
12620b57cec5SDimitry Andric     // The predecessor may not have a live-out value. That is OK, like an
12630b57cec5SDimitry Andric     // undef PHI operand.
126481ad6265SDimitry Andric     const LiveInterval &PLI = Edit->getParent();
12650b57cec5SDimitry Andric     // Need the cast because the inputs to ?: would otherwise be deemed
12660b57cec5SDimitry Andric     // "incompatible": SubRange vs LiveInterval.
126781ad6265SDimitry Andric     const LiveRange &PSR = !LM.all() ? getSubRangeForMaskExact(LM, PLI)
126881ad6265SDimitry Andric                                      : static_cast<const LiveRange &>(PLI);
12690b57cec5SDimitry Andric     if (PSR.liveAt(LastUse))
12705ffd83dbSDimitry Andric       LIC.extend(LR, End, /*PhysReg=*/0, Undefs);
12710b57cec5SDimitry Andric   }
12720b57cec5SDimitry Andric }
12730b57cec5SDimitry Andric 
extendPHIKillRanges()12740b57cec5SDimitry Andric void SplitEditor::extendPHIKillRanges() {
12750b57cec5SDimitry Andric   // Extend live ranges to be live-out for successor PHI values.
12760b57cec5SDimitry Andric 
12770b57cec5SDimitry Andric   // Visit each PHI def slot in the parent live interval. If the def is dead,
12780b57cec5SDimitry Andric   // remove it. Otherwise, extend the live interval to reach the end indexes
12790b57cec5SDimitry Andric   // of all predecessor blocks.
12800b57cec5SDimitry Andric 
128181ad6265SDimitry Andric   const LiveInterval &ParentLI = Edit->getParent();
12820b57cec5SDimitry Andric   for (const VNInfo *V : ParentLI.valnos) {
12830b57cec5SDimitry Andric     if (V->isUnused() || !V->isPHIDef())
12840b57cec5SDimitry Andric       continue;
12850b57cec5SDimitry Andric 
12860b57cec5SDimitry Andric     unsigned RegIdx = RegAssign.lookup(V->def);
12870b57cec5SDimitry Andric     LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
12885ffd83dbSDimitry Andric     LiveIntervalCalc &LIC = getLICalc(RegIdx);
12890b57cec5SDimitry Andric     MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
12900b57cec5SDimitry Andric     if (!removeDeadSegment(V->def, LI))
12915ffd83dbSDimitry Andric       extendPHIRange(B, LIC, LI, LaneBitmask::getAll(), /*Undefs=*/{});
12920b57cec5SDimitry Andric   }
12930b57cec5SDimitry Andric 
12940b57cec5SDimitry Andric   SmallVector<SlotIndex, 4> Undefs;
12955ffd83dbSDimitry Andric   LiveIntervalCalc SubLIC;
12960b57cec5SDimitry Andric 
129781ad6265SDimitry Andric   for (const LiveInterval::SubRange &PS : ParentLI.subranges()) {
12980b57cec5SDimitry Andric     for (const VNInfo *V : PS.valnos) {
12990b57cec5SDimitry Andric       if (V->isUnused() || !V->isPHIDef())
13000b57cec5SDimitry Andric         continue;
13010b57cec5SDimitry Andric       unsigned RegIdx = RegAssign.lookup(V->def);
13020b57cec5SDimitry Andric       LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1303e8d8bef9SDimitry Andric       LiveInterval::SubRange &S = getSubRangeForMaskExact(PS.LaneMask, LI);
13040b57cec5SDimitry Andric       if (removeDeadSegment(V->def, S))
13050b57cec5SDimitry Andric         continue;
13060b57cec5SDimitry Andric 
13070b57cec5SDimitry Andric       MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
13085ffd83dbSDimitry Andric       SubLIC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
13090b57cec5SDimitry Andric                    &LIS.getVNInfoAllocator());
13100b57cec5SDimitry Andric       Undefs.clear();
13110b57cec5SDimitry Andric       LI.computeSubRangeUndefs(Undefs, PS.LaneMask, MRI, *LIS.getSlotIndexes());
13125ffd83dbSDimitry Andric       extendPHIRange(B, SubLIC, S, PS.LaneMask, Undefs);
13130b57cec5SDimitry Andric     }
13140b57cec5SDimitry Andric   }
13150b57cec5SDimitry Andric }
13160b57cec5SDimitry Andric 
13170b57cec5SDimitry Andric /// rewriteAssigned - Rewrite all uses of Edit->getReg().
rewriteAssigned(bool ExtendRanges)13180b57cec5SDimitry Andric void SplitEditor::rewriteAssigned(bool ExtendRanges) {
13190b57cec5SDimitry Andric   struct ExtPoint {
13200b57cec5SDimitry Andric     ExtPoint(const MachineOperand &O, unsigned R, SlotIndex N)
13210b57cec5SDimitry Andric       : MO(O), RegIdx(R), Next(N) {}
13220b57cec5SDimitry Andric 
13230b57cec5SDimitry Andric     MachineOperand MO;
13240b57cec5SDimitry Andric     unsigned RegIdx;
13250b57cec5SDimitry Andric     SlotIndex Next;
13260b57cec5SDimitry Andric   };
13270b57cec5SDimitry Andric 
13280b57cec5SDimitry Andric   SmallVector<ExtPoint,4> ExtPoints;
13290b57cec5SDimitry Andric 
1330fe6060f1SDimitry Andric   for (MachineOperand &MO :
1331fe6060f1SDimitry Andric        llvm::make_early_inc_range(MRI.reg_operands(Edit->getReg()))) {
13320b57cec5SDimitry Andric     MachineInstr *MI = MO.getParent();
13330b57cec5SDimitry Andric     // LiveDebugVariables should have handled all DBG_VALUE instructions.
13340b57cec5SDimitry Andric     if (MI->isDebugValue()) {
13350b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Zapping " << *MI);
13360b57cec5SDimitry Andric       MO.setReg(0);
13370b57cec5SDimitry Andric       continue;
13380b57cec5SDimitry Andric     }
13390b57cec5SDimitry Andric 
13400b57cec5SDimitry Andric     // <undef> operands don't really read the register, so it doesn't matter
13410b57cec5SDimitry Andric     // which register we choose.  When the use operand is tied to a def, we must
13420b57cec5SDimitry Andric     // use the same register as the def, so just do that always.
13430b57cec5SDimitry Andric     SlotIndex Idx = LIS.getInstructionIndex(*MI);
13440b57cec5SDimitry Andric     if (MO.isDef() || MO.isUndef())
13450b57cec5SDimitry Andric       Idx = Idx.getRegSlot(MO.isEarlyClobber());
13460b57cec5SDimitry Andric 
13470b57cec5SDimitry Andric     // Rewrite to the mapped register at Idx.
13480b57cec5SDimitry Andric     unsigned RegIdx = RegAssign.lookup(Idx);
13490b57cec5SDimitry Andric     LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1350e8d8bef9SDimitry Andric     MO.setReg(LI.reg());
13510b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "  rewr " << printMBBReference(*MI->getParent())
13520b57cec5SDimitry Andric                       << '\t' << Idx << ':' << RegIdx << '\t' << *MI);
13530b57cec5SDimitry Andric 
13540b57cec5SDimitry Andric     // Extend liveness to Idx if the instruction reads reg.
13550b57cec5SDimitry Andric     if (!ExtendRanges || MO.isUndef())
13560b57cec5SDimitry Andric       continue;
13570b57cec5SDimitry Andric 
13580b57cec5SDimitry Andric     // Skip instructions that don't read Reg.
13590b57cec5SDimitry Andric     if (MO.isDef()) {
13600b57cec5SDimitry Andric       if (!MO.getSubReg() && !MO.isEarlyClobber())
13610b57cec5SDimitry Andric         continue;
13620b57cec5SDimitry Andric       // We may want to extend a live range for a partial redef, or for a use
13630b57cec5SDimitry Andric       // tied to an early clobber.
136481ad6265SDimitry Andric       if (!Edit->getParent().liveAt(Idx.getPrevSlot()))
13650b57cec5SDimitry Andric         continue;
136681ad6265SDimitry Andric     } else {
136781ad6265SDimitry Andric       assert(MO.isUse());
136881ad6265SDimitry Andric       bool IsEarlyClobber = false;
136981ad6265SDimitry Andric       if (MO.isTied()) {
137081ad6265SDimitry Andric         // We want to extend a live range into `e` slot rather than `r` slot if
137181ad6265SDimitry Andric         // tied-def is early clobber, because the `e` slot already contained
137281ad6265SDimitry Andric         // in the live range of early-clobber tied-def operand, give an example
137381ad6265SDimitry Andric         // here:
137481ad6265SDimitry Andric         //  0  %0 = ...
137581ad6265SDimitry Andric         // 16  early-clobber %0 = Op %0 (tied-def 0), ...
137681ad6265SDimitry Andric         // 32  ... = Op %0
137781ad6265SDimitry Andric         // Before extend:
137881ad6265SDimitry Andric         //   %0 = [0r, 0d) [16e, 32d)
137981ad6265SDimitry Andric         // The point we want to extend is 0d to 16e not 16r in this case, but if
138081ad6265SDimitry Andric         // we use 16r here we will extend nothing because that already contained
138181ad6265SDimitry Andric         // in [16e, 32d).
138206c3fb27SDimitry Andric         unsigned OpIdx = MO.getOperandNo();
138381ad6265SDimitry Andric         unsigned DefOpIdx = MI->findTiedOperandIdx(OpIdx);
138481ad6265SDimitry Andric         const MachineOperand &DefOp = MI->getOperand(DefOpIdx);
138581ad6265SDimitry Andric         IsEarlyClobber = DefOp.isEarlyClobber();
138681ad6265SDimitry Andric       }
13870b57cec5SDimitry Andric 
138881ad6265SDimitry Andric       Idx = Idx.getRegSlot(IsEarlyClobber);
138981ad6265SDimitry Andric     }
139081ad6265SDimitry Andric 
139181ad6265SDimitry Andric     SlotIndex Next = Idx;
13920b57cec5SDimitry Andric     if (LI.hasSubRanges()) {
13930b57cec5SDimitry Andric       // We have to delay extending subranges until we have seen all operands
13940b57cec5SDimitry Andric       // defining the register. This is because a <def,read-undef> operand
13950b57cec5SDimitry Andric       // will create an "undef" point, and we cannot extend any subranges
13960b57cec5SDimitry Andric       // until all of them have been accounted for.
13970b57cec5SDimitry Andric       if (MO.isUse())
13980b57cec5SDimitry Andric         ExtPoints.push_back(ExtPoint(MO, RegIdx, Next));
13990b57cec5SDimitry Andric     } else {
14005ffd83dbSDimitry Andric       LiveIntervalCalc &LIC = getLICalc(RegIdx);
14015ffd83dbSDimitry Andric       LIC.extend(LI, Next, 0, ArrayRef<SlotIndex>());
14020b57cec5SDimitry Andric     }
14030b57cec5SDimitry Andric   }
14040b57cec5SDimitry Andric 
14050b57cec5SDimitry Andric   for (ExtPoint &EP : ExtPoints) {
14060b57cec5SDimitry Andric     LiveInterval &LI = LIS.getInterval(Edit->get(EP.RegIdx));
14070b57cec5SDimitry Andric     assert(LI.hasSubRanges());
14080b57cec5SDimitry Andric 
14095ffd83dbSDimitry Andric     LiveIntervalCalc SubLIC;
14108bcb0991SDimitry Andric     Register Reg = EP.MO.getReg(), Sub = EP.MO.getSubReg();
14110b57cec5SDimitry Andric     LaneBitmask LM = Sub != 0 ? TRI.getSubRegIndexLaneMask(Sub)
14120b57cec5SDimitry Andric                               : MRI.getMaxLaneMaskForVReg(Reg);
14130b57cec5SDimitry Andric     for (LiveInterval::SubRange &S : LI.subranges()) {
14140b57cec5SDimitry Andric       if ((S.LaneMask & LM).none())
14150b57cec5SDimitry Andric         continue;
14160b57cec5SDimitry Andric       // The problem here can be that the new register may have been created
14170b57cec5SDimitry Andric       // for a partially defined original register. For example:
14180b57cec5SDimitry Andric       //   %0:subreg_hireg<def,read-undef> = ...
14190b57cec5SDimitry Andric       //   ...
14200b57cec5SDimitry Andric       //   %1 = COPY %0
14210b57cec5SDimitry Andric       if (S.empty())
14220b57cec5SDimitry Andric         continue;
14235ffd83dbSDimitry Andric       SubLIC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
14240b57cec5SDimitry Andric                    &LIS.getVNInfoAllocator());
14250b57cec5SDimitry Andric       SmallVector<SlotIndex, 4> Undefs;
14260b57cec5SDimitry Andric       LI.computeSubRangeUndefs(Undefs, S.LaneMask, MRI, *LIS.getSlotIndexes());
14275ffd83dbSDimitry Andric       SubLIC.extend(S, EP.Next, 0, Undefs);
14280b57cec5SDimitry Andric     }
14290b57cec5SDimitry Andric   }
14300b57cec5SDimitry Andric 
1431e8d8bef9SDimitry Andric   for (Register R : *Edit) {
14320b57cec5SDimitry Andric     LiveInterval &LI = LIS.getInterval(R);
14330b57cec5SDimitry Andric     if (!LI.hasSubRanges())
14340b57cec5SDimitry Andric       continue;
14350b57cec5SDimitry Andric     LI.clear();
14360b57cec5SDimitry Andric     LI.removeEmptySubRanges();
14370b57cec5SDimitry Andric     LIS.constructMainRangeFromSubranges(LI);
14380b57cec5SDimitry Andric   }
14390b57cec5SDimitry Andric }
14400b57cec5SDimitry Andric 
deleteRematVictims()14410b57cec5SDimitry Andric void SplitEditor::deleteRematVictims() {
14420b57cec5SDimitry Andric   SmallVector<MachineInstr*, 8> Dead;
1443fe6060f1SDimitry Andric   for (const Register &R : *Edit) {
1444fe6060f1SDimitry Andric     LiveInterval *LI = &LIS.getInterval(R);
14450b57cec5SDimitry Andric     for (const LiveRange::Segment &S : LI->segments) {
14460b57cec5SDimitry Andric       // Dead defs end at the dead slot.
14470b57cec5SDimitry Andric       if (S.end != S.valno->def.getDeadSlot())
14480b57cec5SDimitry Andric         continue;
14490b57cec5SDimitry Andric       if (S.valno->isPHIDef())
14500b57cec5SDimitry Andric         continue;
14510b57cec5SDimitry Andric       MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def);
14520b57cec5SDimitry Andric       assert(MI && "Missing instruction for dead def");
1453e8d8bef9SDimitry Andric       MI->addRegisterDead(LI->reg(), &TRI);
14540b57cec5SDimitry Andric 
14550b57cec5SDimitry Andric       if (!MI->allDefsAreDead())
14560b57cec5SDimitry Andric         continue;
14570b57cec5SDimitry Andric 
14580b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "All defs dead: " << *MI);
14590b57cec5SDimitry Andric       Dead.push_back(MI);
14600b57cec5SDimitry Andric     }
14610b57cec5SDimitry Andric   }
14620b57cec5SDimitry Andric 
14630b57cec5SDimitry Andric   if (Dead.empty())
14640b57cec5SDimitry Andric     return;
14650b57cec5SDimitry Andric 
1466bdd1243dSDimitry Andric   Edit->eliminateDeadDefs(Dead, std::nullopt);
14670b57cec5SDimitry Andric }
14680b57cec5SDimitry Andric 
forceRecomputeVNI(const VNInfo & ParentVNI)14690b57cec5SDimitry Andric void SplitEditor::forceRecomputeVNI(const VNInfo &ParentVNI) {
14700b57cec5SDimitry Andric   // Fast-path for common case.
14710b57cec5SDimitry Andric   if (!ParentVNI.isPHIDef()) {
14720b57cec5SDimitry Andric     for (unsigned I = 0, E = Edit->size(); I != E; ++I)
14730b57cec5SDimitry Andric       forceRecompute(I, ParentVNI);
14740b57cec5SDimitry Andric     return;
14750b57cec5SDimitry Andric   }
14760b57cec5SDimitry Andric 
14770b57cec5SDimitry Andric   // Trace value through phis.
14780b57cec5SDimitry Andric   SmallPtrSet<const VNInfo *, 8> Visited; ///< whether VNI was/is in worklist.
14790b57cec5SDimitry Andric   SmallVector<const VNInfo *, 4> WorkList;
14800b57cec5SDimitry Andric   Visited.insert(&ParentVNI);
14810b57cec5SDimitry Andric   WorkList.push_back(&ParentVNI);
14820b57cec5SDimitry Andric 
14830b57cec5SDimitry Andric   const LiveInterval &ParentLI = Edit->getParent();
14840b57cec5SDimitry Andric   const SlotIndexes &Indexes = *LIS.getSlotIndexes();
14850b57cec5SDimitry Andric   do {
14860b57cec5SDimitry Andric     const VNInfo &VNI = *WorkList.back();
14870b57cec5SDimitry Andric     WorkList.pop_back();
14880b57cec5SDimitry Andric     for (unsigned I = 0, E = Edit->size(); I != E; ++I)
14890b57cec5SDimitry Andric       forceRecompute(I, VNI);
14900b57cec5SDimitry Andric     if (!VNI.isPHIDef())
14910b57cec5SDimitry Andric       continue;
14920b57cec5SDimitry Andric 
14930b57cec5SDimitry Andric     MachineBasicBlock &MBB = *Indexes.getMBBFromIndex(VNI.def);
14940b57cec5SDimitry Andric     for (const MachineBasicBlock *Pred : MBB.predecessors()) {
14950b57cec5SDimitry Andric       SlotIndex PredEnd = Indexes.getMBBEndIdx(Pred);
14960b57cec5SDimitry Andric       VNInfo *PredVNI = ParentLI.getVNInfoBefore(PredEnd);
14970b57cec5SDimitry Andric       assert(PredVNI && "Value available in PhiVNI predecessor");
14980b57cec5SDimitry Andric       if (Visited.insert(PredVNI).second)
14990b57cec5SDimitry Andric         WorkList.push_back(PredVNI);
15000b57cec5SDimitry Andric     }
15010b57cec5SDimitry Andric   } while(!WorkList.empty());
15020b57cec5SDimitry Andric }
15030b57cec5SDimitry Andric 
finish(SmallVectorImpl<unsigned> * LRMap)15040b57cec5SDimitry Andric void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
15050b57cec5SDimitry Andric   ++NumFinished;
15060b57cec5SDimitry Andric 
15070b57cec5SDimitry Andric   // At this point, the live intervals in Edit contain VNInfos corresponding to
15080b57cec5SDimitry Andric   // the inserted copies.
15090b57cec5SDimitry Andric 
15100b57cec5SDimitry Andric   // Add the original defs from the parent interval.
15110b57cec5SDimitry Andric   for (const VNInfo *ParentVNI : Edit->getParent().valnos) {
15120b57cec5SDimitry Andric     if (ParentVNI->isUnused())
15130b57cec5SDimitry Andric       continue;
15140b57cec5SDimitry Andric     unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
15150b57cec5SDimitry Andric     defValue(RegIdx, ParentVNI, ParentVNI->def, true);
15160b57cec5SDimitry Andric 
15170b57cec5SDimitry Andric     // Force rematted values to be recomputed everywhere.
15180b57cec5SDimitry Andric     // The new live ranges may be truncated.
15190b57cec5SDimitry Andric     if (Edit->didRematerialize(ParentVNI))
15200b57cec5SDimitry Andric       forceRecomputeVNI(*ParentVNI);
15210b57cec5SDimitry Andric   }
15220b57cec5SDimitry Andric 
15230b57cec5SDimitry Andric   // Hoist back-copies to the complement interval when in spill mode.
15240b57cec5SDimitry Andric   switch (SpillMode) {
15250b57cec5SDimitry Andric   case SM_Partition:
15260b57cec5SDimitry Andric     // Leave all back-copies as is.
15270b57cec5SDimitry Andric     break;
15280b57cec5SDimitry Andric   case SM_Size:
15290b57cec5SDimitry Andric   case SM_Speed:
15300b57cec5SDimitry Andric     // hoistCopies will behave differently between size and speed.
15310b57cec5SDimitry Andric     hoistCopies();
15320b57cec5SDimitry Andric   }
15330b57cec5SDimitry Andric 
15340b57cec5SDimitry Andric   // Transfer the simply mapped values, check if any are skipped.
15350b57cec5SDimitry Andric   bool Skipped = transferValues();
15360b57cec5SDimitry Andric 
15370b57cec5SDimitry Andric   // Rewrite virtual registers, possibly extending ranges.
15380b57cec5SDimitry Andric   rewriteAssigned(Skipped);
15390b57cec5SDimitry Andric 
15400b57cec5SDimitry Andric   if (Skipped)
15410b57cec5SDimitry Andric     extendPHIKillRanges();
15420b57cec5SDimitry Andric   else
15430b57cec5SDimitry Andric     ++NumSimple;
15440b57cec5SDimitry Andric 
15450b57cec5SDimitry Andric   // Delete defs that were rematted everywhere.
15460b57cec5SDimitry Andric   if (Skipped)
15470b57cec5SDimitry Andric     deleteRematVictims();
15480b57cec5SDimitry Andric 
15490b57cec5SDimitry Andric   // Get rid of unused values and set phi-kill flags.
1550e8d8bef9SDimitry Andric   for (Register Reg : *Edit) {
15510b57cec5SDimitry Andric     LiveInterval &LI = LIS.getInterval(Reg);
15520b57cec5SDimitry Andric     LI.removeEmptySubRanges();
15530b57cec5SDimitry Andric     LI.RenumberValues();
15540b57cec5SDimitry Andric   }
15550b57cec5SDimitry Andric 
15560b57cec5SDimitry Andric   // Provide a reverse mapping from original indices to Edit ranges.
15570b57cec5SDimitry Andric   if (LRMap) {
155881ad6265SDimitry Andric     auto Seq = llvm::seq<unsigned>(0, Edit->size());
155981ad6265SDimitry Andric     LRMap->assign(Seq.begin(), Seq.end());
15600b57cec5SDimitry Andric   }
15610b57cec5SDimitry Andric 
15620b57cec5SDimitry Andric   // Now check if any registers were separated into multiple components.
15630b57cec5SDimitry Andric   ConnectedVNInfoEqClasses ConEQ(LIS);
15640b57cec5SDimitry Andric   for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
15650b57cec5SDimitry Andric     // Don't use iterators, they are invalidated by create() below.
1566e8d8bef9SDimitry Andric     Register VReg = Edit->get(i);
15670b57cec5SDimitry Andric     LiveInterval &LI = LIS.getInterval(VReg);
15680b57cec5SDimitry Andric     SmallVector<LiveInterval*, 8> SplitLIs;
15690b57cec5SDimitry Andric     LIS.splitSeparateComponents(LI, SplitLIs);
1570e8d8bef9SDimitry Andric     Register Original = VRM.getOriginal(VReg);
15710b57cec5SDimitry Andric     for (LiveInterval *SplitLI : SplitLIs)
1572e8d8bef9SDimitry Andric       VRM.setIsSplitFromReg(SplitLI->reg(), Original);
15730b57cec5SDimitry Andric 
15740b57cec5SDimitry Andric     // The new intervals all map back to i.
15750b57cec5SDimitry Andric     if (LRMap)
15760b57cec5SDimitry Andric       LRMap->resize(Edit->size(), i);
15770b57cec5SDimitry Andric   }
15780b57cec5SDimitry Andric 
15790b57cec5SDimitry Andric   // Calculate spill weight and allocation hints for new intervals.
1580fe6060f1SDimitry Andric   Edit->calculateRegClassAndHint(VRM.getMachineFunction(), VRAI);
15810b57cec5SDimitry Andric 
15820b57cec5SDimitry Andric   assert(!LRMap || LRMap->size() == Edit->size());
15830b57cec5SDimitry Andric }
15840b57cec5SDimitry Andric 
15850b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
15860b57cec5SDimitry Andric //                            Single Block Splitting
15870b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
15880b57cec5SDimitry Andric 
shouldSplitSingleBlock(const BlockInfo & BI,bool SingleInstrs) const15890b57cec5SDimitry Andric bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
15900b57cec5SDimitry Andric                                            bool SingleInstrs) const {
15910b57cec5SDimitry Andric   // Always split for multiple instructions.
15920b57cec5SDimitry Andric   if (!BI.isOneInstr())
15930b57cec5SDimitry Andric     return true;
15940b57cec5SDimitry Andric   // Don't split for single instructions unless explicitly requested.
15950b57cec5SDimitry Andric   if (!SingleInstrs)
15960b57cec5SDimitry Andric     return false;
15970b57cec5SDimitry Andric   // Splitting a live-through range always makes progress.
15980b57cec5SDimitry Andric   if (BI.LiveIn && BI.LiveOut)
15990b57cec5SDimitry Andric     return true;
16000b57cec5SDimitry Andric   // No point in isolating a copy. It has no register class constraints.
16015f757f3fSDimitry Andric   MachineInstr *MI = LIS.getInstructionFromIndex(BI.FirstInstr);
16025f757f3fSDimitry Andric   bool copyLike = TII.isCopyInstr(*MI) || MI->isSubregToReg();
16035f757f3fSDimitry Andric   if (copyLike)
16040b57cec5SDimitry Andric     return false;
16050b57cec5SDimitry Andric   // Finally, don't isolate an end point that was created by earlier splits.
16060b57cec5SDimitry Andric   return isOriginalEndpoint(BI.FirstInstr);
16070b57cec5SDimitry Andric }
16080b57cec5SDimitry Andric 
splitSingleBlock(const SplitAnalysis::BlockInfo & BI)16090b57cec5SDimitry Andric void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
16100b57cec5SDimitry Andric   openIntv();
1611fe6060f1SDimitry Andric   SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB);
16120b57cec5SDimitry Andric   SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
16130b57cec5SDimitry Andric     LastSplitPoint));
16140b57cec5SDimitry Andric   if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
16150b57cec5SDimitry Andric     useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
16160b57cec5SDimitry Andric   } else {
16170b57cec5SDimitry Andric       // The last use is after the last valid split point.
16180b57cec5SDimitry Andric     SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
16190b57cec5SDimitry Andric     useIntv(SegStart, SegStop);
16200b57cec5SDimitry Andric     overlapIntv(SegStop, BI.LastInstr);
16210b57cec5SDimitry Andric   }
16220b57cec5SDimitry Andric }
16230b57cec5SDimitry Andric 
16240b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
16250b57cec5SDimitry Andric //                    Global Live Range Splitting Support
16260b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
16270b57cec5SDimitry Andric 
16280b57cec5SDimitry Andric // These methods support a method of global live range splitting that uses a
16290b57cec5SDimitry Andric // global algorithm to decide intervals for CFG edges. They will insert split
16300b57cec5SDimitry Andric // points and color intervals in basic blocks while avoiding interference.
16310b57cec5SDimitry Andric //
16320b57cec5SDimitry Andric // Note that splitSingleBlock is also useful for blocks where both CFG edges
16330b57cec5SDimitry Andric // are on the stack.
16340b57cec5SDimitry Andric 
splitLiveThroughBlock(unsigned MBBNum,unsigned IntvIn,SlotIndex LeaveBefore,unsigned IntvOut,SlotIndex EnterAfter)16350b57cec5SDimitry Andric void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
16360b57cec5SDimitry Andric                                         unsigned IntvIn, SlotIndex LeaveBefore,
16370b57cec5SDimitry Andric                                         unsigned IntvOut, SlotIndex EnterAfter){
16380b57cec5SDimitry Andric   SlotIndex Start, Stop;
16390b57cec5SDimitry Andric   std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
16400b57cec5SDimitry Andric 
16410b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "%bb." << MBBNum << " [" << Start << ';' << Stop
16420b57cec5SDimitry Andric                     << ") intf " << LeaveBefore << '-' << EnterAfter
16430b57cec5SDimitry Andric                     << ", live-through " << IntvIn << " -> " << IntvOut);
16440b57cec5SDimitry Andric 
16450b57cec5SDimitry Andric   assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
16460b57cec5SDimitry Andric 
16470b57cec5SDimitry Andric   assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
16480b57cec5SDimitry Andric   assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
16490b57cec5SDimitry Andric   assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
16500b57cec5SDimitry Andric 
16510b57cec5SDimitry Andric   MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
16520b57cec5SDimitry Andric 
16530b57cec5SDimitry Andric   if (!IntvOut) {
16540b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ", spill on entry.\n");
16550b57cec5SDimitry Andric     //
16560b57cec5SDimitry Andric     //        <<<<<<<<<    Possible LeaveBefore interference.
16570b57cec5SDimitry Andric     //    |-----------|    Live through.
16580b57cec5SDimitry Andric     //    -____________    Spill on entry.
16590b57cec5SDimitry Andric     //
16600b57cec5SDimitry Andric     selectIntv(IntvIn);
16610b57cec5SDimitry Andric     SlotIndex Idx = leaveIntvAtTop(*MBB);
16620b57cec5SDimitry Andric     assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
16630b57cec5SDimitry Andric     (void)Idx;
16640b57cec5SDimitry Andric     return;
16650b57cec5SDimitry Andric   }
16660b57cec5SDimitry Andric 
16670b57cec5SDimitry Andric   if (!IntvIn) {
16680b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ", reload on exit.\n");
16690b57cec5SDimitry Andric     //
16700b57cec5SDimitry Andric     //    >>>>>>>          Possible EnterAfter interference.
16710b57cec5SDimitry Andric     //    |-----------|    Live through.
16720b57cec5SDimitry Andric     //    ___________--    Reload on exit.
16730b57cec5SDimitry Andric     //
16740b57cec5SDimitry Andric     selectIntv(IntvOut);
16750b57cec5SDimitry Andric     SlotIndex Idx = enterIntvAtEnd(*MBB);
16760b57cec5SDimitry Andric     assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
16770b57cec5SDimitry Andric     (void)Idx;
16780b57cec5SDimitry Andric     return;
16790b57cec5SDimitry Andric   }
16800b57cec5SDimitry Andric 
16810b57cec5SDimitry Andric   if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
16820b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ", straight through.\n");
16830b57cec5SDimitry Andric     //
16840b57cec5SDimitry Andric     //    |-----------|    Live through.
16850b57cec5SDimitry Andric     //    -------------    Straight through, same intv, no interference.
16860b57cec5SDimitry Andric     //
16870b57cec5SDimitry Andric     selectIntv(IntvOut);
16880b57cec5SDimitry Andric     useIntv(Start, Stop);
16890b57cec5SDimitry Andric     return;
16900b57cec5SDimitry Andric   }
16910b57cec5SDimitry Andric 
16920b57cec5SDimitry Andric   // We cannot legally insert splits after LSP.
16930b57cec5SDimitry Andric   SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
16940b57cec5SDimitry Andric   assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
16950b57cec5SDimitry Andric 
16960b57cec5SDimitry Andric   if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
16970b57cec5SDimitry Andric                   LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
16980b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ", switch avoiding interference.\n");
16990b57cec5SDimitry Andric     //
17000b57cec5SDimitry Andric     //    >>>>     <<<<    Non-overlapping EnterAfter/LeaveBefore interference.
17010b57cec5SDimitry Andric     //    |-----------|    Live through.
17020b57cec5SDimitry Andric     //    ------=======    Switch intervals between interference.
17030b57cec5SDimitry Andric     //
17040b57cec5SDimitry Andric     selectIntv(IntvOut);
17050b57cec5SDimitry Andric     SlotIndex Idx;
17060b57cec5SDimitry Andric     if (LeaveBefore && LeaveBefore < LSP) {
17070b57cec5SDimitry Andric       Idx = enterIntvBefore(LeaveBefore);
17080b57cec5SDimitry Andric       useIntv(Idx, Stop);
17090b57cec5SDimitry Andric     } else {
17100b57cec5SDimitry Andric       Idx = enterIntvAtEnd(*MBB);
17110b57cec5SDimitry Andric     }
17120b57cec5SDimitry Andric     selectIntv(IntvIn);
17130b57cec5SDimitry Andric     useIntv(Start, Idx);
17140b57cec5SDimitry Andric     assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
17150b57cec5SDimitry Andric     assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
17160b57cec5SDimitry Andric     return;
17170b57cec5SDimitry Andric   }
17180b57cec5SDimitry Andric 
17190b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << ", create local intv for interference.\n");
17200b57cec5SDimitry Andric   //
17210b57cec5SDimitry Andric   //    >>><><><><<<<    Overlapping EnterAfter/LeaveBefore interference.
17220b57cec5SDimitry Andric   //    |-----------|    Live through.
17230b57cec5SDimitry Andric   //    ==---------==    Switch intervals before/after interference.
17240b57cec5SDimitry Andric   //
17250b57cec5SDimitry Andric   assert(LeaveBefore <= EnterAfter && "Missed case");
17260b57cec5SDimitry Andric 
17270b57cec5SDimitry Andric   selectIntv(IntvOut);
17280b57cec5SDimitry Andric   SlotIndex Idx = enterIntvAfter(EnterAfter);
17290b57cec5SDimitry Andric   useIntv(Idx, Stop);
17300b57cec5SDimitry Andric   assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
17310b57cec5SDimitry Andric 
17320b57cec5SDimitry Andric   selectIntv(IntvIn);
17330b57cec5SDimitry Andric   Idx = leaveIntvBefore(LeaveBefore);
17340b57cec5SDimitry Andric   useIntv(Start, Idx);
17350b57cec5SDimitry Andric   assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
17360b57cec5SDimitry Andric }
17370b57cec5SDimitry Andric 
splitRegInBlock(const SplitAnalysis::BlockInfo & BI,unsigned IntvIn,SlotIndex LeaveBefore)17380b57cec5SDimitry Andric void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
17390b57cec5SDimitry Andric                                   unsigned IntvIn, SlotIndex LeaveBefore) {
17400b57cec5SDimitry Andric   SlotIndex Start, Stop;
17410b57cec5SDimitry Andric   std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
17420b57cec5SDimitry Andric 
17430b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << printMBBReference(*BI.MBB) << " [" << Start << ';'
17440b57cec5SDimitry Andric                     << Stop << "), uses " << BI.FirstInstr << '-'
17450b57cec5SDimitry Andric                     << BI.LastInstr << ", reg-in " << IntvIn
17460b57cec5SDimitry Andric                     << ", leave before " << LeaveBefore
17470b57cec5SDimitry Andric                     << (BI.LiveOut ? ", stack-out" : ", killed in block"));
17480b57cec5SDimitry Andric 
17490b57cec5SDimitry Andric   assert(IntvIn && "Must have register in");
17500b57cec5SDimitry Andric   assert(BI.LiveIn && "Must be live-in");
17510b57cec5SDimitry Andric   assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
17520b57cec5SDimitry Andric 
17530b57cec5SDimitry Andric   if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
17540b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << " before interference.\n");
17550b57cec5SDimitry Andric     //
17560b57cec5SDimitry Andric     //               <<<    Interference after kill.
17570b57cec5SDimitry Andric     //     |---o---x   |    Killed in block.
17580b57cec5SDimitry Andric     //     =========        Use IntvIn everywhere.
17590b57cec5SDimitry Andric     //
17600b57cec5SDimitry Andric     selectIntv(IntvIn);
17610b57cec5SDimitry Andric     useIntv(Start, BI.LastInstr);
17620b57cec5SDimitry Andric     return;
17630b57cec5SDimitry Andric   }
17640b57cec5SDimitry Andric 
1765fe6060f1SDimitry Andric   SlotIndex LSP = SA.getLastSplitPoint(BI.MBB);
17660b57cec5SDimitry Andric 
17670b57cec5SDimitry Andric   if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
17680b57cec5SDimitry Andric     //
17690b57cec5SDimitry Andric     //               <<<    Possible interference after last use.
17700b57cec5SDimitry Andric     //     |---o---o---|    Live-out on stack.
17710b57cec5SDimitry Andric     //     =========____    Leave IntvIn after last use.
17720b57cec5SDimitry Andric     //
17730b57cec5SDimitry Andric     //                 <    Interference after last use.
17740b57cec5SDimitry Andric     //     |---o---o--o|    Live-out on stack, late last use.
17750b57cec5SDimitry Andric     //     ============     Copy to stack after LSP, overlap IntvIn.
17760b57cec5SDimitry Andric     //            \_____    Stack interval is live-out.
17770b57cec5SDimitry Andric     //
17780b57cec5SDimitry Andric     if (BI.LastInstr < LSP) {
17790b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << ", spill after last use before interference.\n");
17800b57cec5SDimitry Andric       selectIntv(IntvIn);
17810b57cec5SDimitry Andric       SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
17820b57cec5SDimitry Andric       useIntv(Start, Idx);
17830b57cec5SDimitry Andric       assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
17840b57cec5SDimitry Andric     } else {
17850b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << ", spill before last split point.\n");
17860b57cec5SDimitry Andric       selectIntv(IntvIn);
17870b57cec5SDimitry Andric       SlotIndex Idx = leaveIntvBefore(LSP);
17880b57cec5SDimitry Andric       overlapIntv(Idx, BI.LastInstr);
17890b57cec5SDimitry Andric       useIntv(Start, Idx);
17900b57cec5SDimitry Andric       assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
17910b57cec5SDimitry Andric     }
17920b57cec5SDimitry Andric     return;
17930b57cec5SDimitry Andric   }
17940b57cec5SDimitry Andric 
17950b57cec5SDimitry Andric   // The interference is overlapping somewhere we wanted to use IntvIn. That
17960b57cec5SDimitry Andric   // means we need to create a local interval that can be allocated a
17970b57cec5SDimitry Andric   // different register.
17980b57cec5SDimitry Andric   unsigned LocalIntv = openIntv();
17990b57cec5SDimitry Andric   (void)LocalIntv;
18000b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
18010b57cec5SDimitry Andric 
18020b57cec5SDimitry Andric   if (!BI.LiveOut || BI.LastInstr < LSP) {
18030b57cec5SDimitry Andric     //
18040b57cec5SDimitry Andric     //           <<<<<<<    Interference overlapping uses.
18050b57cec5SDimitry Andric     //     |---o---o---|    Live-out on stack.
18060b57cec5SDimitry Andric     //     =====----____    Leave IntvIn before interference, then spill.
18070b57cec5SDimitry Andric     //
18080b57cec5SDimitry Andric     SlotIndex To = leaveIntvAfter(BI.LastInstr);
18090b57cec5SDimitry Andric     SlotIndex From = enterIntvBefore(LeaveBefore);
18100b57cec5SDimitry Andric     useIntv(From, To);
18110b57cec5SDimitry Andric     selectIntv(IntvIn);
18120b57cec5SDimitry Andric     useIntv(Start, From);
18130b57cec5SDimitry Andric     assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
18140b57cec5SDimitry Andric     return;
18150b57cec5SDimitry Andric   }
18160b57cec5SDimitry Andric 
18170b57cec5SDimitry Andric   //           <<<<<<<    Interference overlapping uses.
18180b57cec5SDimitry Andric   //     |---o---o--o|    Live-out on stack, late last use.
18190b57cec5SDimitry Andric   //     =====-------     Copy to stack before LSP, overlap LocalIntv.
18200b57cec5SDimitry Andric   //            \_____    Stack interval is live-out.
18210b57cec5SDimitry Andric   //
18220b57cec5SDimitry Andric   SlotIndex To = leaveIntvBefore(LSP);
18230b57cec5SDimitry Andric   overlapIntv(To, BI.LastInstr);
18240b57cec5SDimitry Andric   SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
18250b57cec5SDimitry Andric   useIntv(From, To);
18260b57cec5SDimitry Andric   selectIntv(IntvIn);
18270b57cec5SDimitry Andric   useIntv(Start, From);
18280b57cec5SDimitry Andric   assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
18290b57cec5SDimitry Andric }
18300b57cec5SDimitry Andric 
splitRegOutBlock(const SplitAnalysis::BlockInfo & BI,unsigned IntvOut,SlotIndex EnterAfter)18310b57cec5SDimitry Andric void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
18320b57cec5SDimitry Andric                                    unsigned IntvOut, SlotIndex EnterAfter) {
18330b57cec5SDimitry Andric   SlotIndex Start, Stop;
18340b57cec5SDimitry Andric   std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
18350b57cec5SDimitry Andric 
18360b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << printMBBReference(*BI.MBB) << " [" << Start << ';'
18370b57cec5SDimitry Andric                     << Stop << "), uses " << BI.FirstInstr << '-'
18380b57cec5SDimitry Andric                     << BI.LastInstr << ", reg-out " << IntvOut
18390b57cec5SDimitry Andric                     << ", enter after " << EnterAfter
18400b57cec5SDimitry Andric                     << (BI.LiveIn ? ", stack-in" : ", defined in block"));
18410b57cec5SDimitry Andric 
1842fe6060f1SDimitry Andric   SlotIndex LSP = SA.getLastSplitPoint(BI.MBB);
18430b57cec5SDimitry Andric 
18440b57cec5SDimitry Andric   assert(IntvOut && "Must have register out");
18450b57cec5SDimitry Andric   assert(BI.LiveOut && "Must be live-out");
18460b57cec5SDimitry Andric   assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
18470b57cec5SDimitry Andric 
18480b57cec5SDimitry Andric   if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
18490b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << " after interference.\n");
18500b57cec5SDimitry Andric     //
18510b57cec5SDimitry Andric     //    >>>>             Interference before def.
18520b57cec5SDimitry Andric     //    |   o---o---|    Defined in block.
18530b57cec5SDimitry Andric     //        =========    Use IntvOut everywhere.
18540b57cec5SDimitry Andric     //
18550b57cec5SDimitry Andric     selectIntv(IntvOut);
18560b57cec5SDimitry Andric     useIntv(BI.FirstInstr, Stop);
18570b57cec5SDimitry Andric     return;
18580b57cec5SDimitry Andric   }
18590b57cec5SDimitry Andric 
18600b57cec5SDimitry Andric   if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
18610b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ", reload after interference.\n");
18620b57cec5SDimitry Andric     //
18630b57cec5SDimitry Andric     //    >>>>             Interference before def.
18640b57cec5SDimitry Andric     //    |---o---o---|    Live-through, stack-in.
18650b57cec5SDimitry Andric     //    ____=========    Enter IntvOut before first use.
18660b57cec5SDimitry Andric     //
18670b57cec5SDimitry Andric     selectIntv(IntvOut);
18680b57cec5SDimitry Andric     SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
18690b57cec5SDimitry Andric     useIntv(Idx, Stop);
18700b57cec5SDimitry Andric     assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
18710b57cec5SDimitry Andric     return;
18720b57cec5SDimitry Andric   }
18730b57cec5SDimitry Andric 
18740b57cec5SDimitry Andric   // The interference is overlapping somewhere we wanted to use IntvOut. That
18750b57cec5SDimitry Andric   // means we need to create a local interval that can be allocated a
18760b57cec5SDimitry Andric   // different register.
18770b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << ", interference overlaps uses.\n");
18780b57cec5SDimitry Andric   //
18790b57cec5SDimitry Andric   //    >>>>>>>          Interference overlapping uses.
18800b57cec5SDimitry Andric   //    |---o---o---|    Live-through, stack-in.
18810b57cec5SDimitry Andric   //    ____---======    Create local interval for interference range.
18820b57cec5SDimitry Andric   //
18830b57cec5SDimitry Andric   selectIntv(IntvOut);
18840b57cec5SDimitry Andric   SlotIndex Idx = enterIntvAfter(EnterAfter);
18850b57cec5SDimitry Andric   useIntv(Idx, Stop);
18860b57cec5SDimitry Andric   assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
18870b57cec5SDimitry Andric 
18880b57cec5SDimitry Andric   openIntv();
18890b57cec5SDimitry Andric   SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
18900b57cec5SDimitry Andric   useIntv(From, Idx);
18910b57cec5SDimitry Andric }
1892fe6060f1SDimitry Andric 
print(raw_ostream & OS) const1893fe6060f1SDimitry Andric void SplitAnalysis::BlockInfo::print(raw_ostream &OS) const {
1894fe6060f1SDimitry Andric   OS << "{" << printMBBReference(*MBB) << ", "
1895fe6060f1SDimitry Andric      << "uses " << FirstInstr << " to " << LastInstr << ", "
1896fe6060f1SDimitry Andric      << "1st def " << FirstDef << ", "
1897fe6060f1SDimitry Andric      << (LiveIn ? "live in" : "dead in") << ", "
1898fe6060f1SDimitry Andric      << (LiveOut ? "live out" : "dead out") << "}";
1899fe6060f1SDimitry Andric }
1900fe6060f1SDimitry Andric 
dump() const1901fe6060f1SDimitry Andric void SplitAnalysis::BlockInfo::dump() const {
1902fe6060f1SDimitry Andric   print(dbgs());
1903fe6060f1SDimitry Andric   dbgs() << "\n";
1904fe6060f1SDimitry Andric }
1905