17330f729Sjoerg //===- SplitKit.cpp - Toolkit for splitting live ranges -------------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file contains the SplitAnalysis class as well as mutator functions for
107330f729Sjoerg // live range splitting.
117330f729Sjoerg //
127330f729Sjoerg //===----------------------------------------------------------------------===//
137330f729Sjoerg
147330f729Sjoerg #include "SplitKit.h"
157330f729Sjoerg #include "llvm/ADT/None.h"
167330f729Sjoerg #include "llvm/ADT/STLExtras.h"
177330f729Sjoerg #include "llvm/ADT/Statistic.h"
18*82d56013Sjoerg #include "llvm/Analysis/AliasAnalysis.h"
197330f729Sjoerg #include "llvm/CodeGen/LiveRangeEdit.h"
207330f729Sjoerg #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
217330f729Sjoerg #include "llvm/CodeGen/MachineDominators.h"
227330f729Sjoerg #include "llvm/CodeGen/MachineInstr.h"
237330f729Sjoerg #include "llvm/CodeGen/MachineInstrBuilder.h"
247330f729Sjoerg #include "llvm/CodeGen/MachineLoopInfo.h"
257330f729Sjoerg #include "llvm/CodeGen/MachineOperand.h"
267330f729Sjoerg #include "llvm/CodeGen/MachineRegisterInfo.h"
277330f729Sjoerg #include "llvm/CodeGen/TargetInstrInfo.h"
287330f729Sjoerg #include "llvm/CodeGen/TargetOpcodes.h"
297330f729Sjoerg #include "llvm/CodeGen/TargetRegisterInfo.h"
307330f729Sjoerg #include "llvm/CodeGen/TargetSubtargetInfo.h"
317330f729Sjoerg #include "llvm/CodeGen/VirtRegMap.h"
327330f729Sjoerg #include "llvm/Config/llvm-config.h"
337330f729Sjoerg #include "llvm/IR/DebugLoc.h"
347330f729Sjoerg #include "llvm/Support/Allocator.h"
357330f729Sjoerg #include "llvm/Support/BlockFrequency.h"
367330f729Sjoerg #include "llvm/Support/Debug.h"
377330f729Sjoerg #include "llvm/Support/ErrorHandling.h"
387330f729Sjoerg #include "llvm/Support/raw_ostream.h"
397330f729Sjoerg #include <algorithm>
407330f729Sjoerg #include <cassert>
417330f729Sjoerg #include <iterator>
427330f729Sjoerg #include <limits>
437330f729Sjoerg #include <tuple>
447330f729Sjoerg
457330f729Sjoerg using namespace llvm;
467330f729Sjoerg
477330f729Sjoerg #define DEBUG_TYPE "regalloc"
487330f729Sjoerg
497330f729Sjoerg STATISTIC(NumFinished, "Number of splits finished");
507330f729Sjoerg STATISTIC(NumSimple, "Number of splits that were simple");
517330f729Sjoerg STATISTIC(NumCopies, "Number of copies inserted for splitting");
527330f729Sjoerg STATISTIC(NumRemats, "Number of rematerialized defs for splitting");
537330f729Sjoerg STATISTIC(NumRepairs, "Number of invalid live ranges repaired");
547330f729Sjoerg
557330f729Sjoerg //===----------------------------------------------------------------------===//
567330f729Sjoerg // Last Insert Point Analysis
577330f729Sjoerg //===----------------------------------------------------------------------===//
587330f729Sjoerg
InsertPointAnalysis(const LiveIntervals & lis,unsigned BBNum)597330f729Sjoerg InsertPointAnalysis::InsertPointAnalysis(const LiveIntervals &lis,
607330f729Sjoerg unsigned BBNum)
617330f729Sjoerg : LIS(lis), LastInsertPoint(BBNum) {}
627330f729Sjoerg
637330f729Sjoerg SlotIndex
computeLastInsertPoint(const LiveInterval & CurLI,const MachineBasicBlock & MBB)647330f729Sjoerg InsertPointAnalysis::computeLastInsertPoint(const LiveInterval &CurLI,
657330f729Sjoerg const MachineBasicBlock &MBB) {
667330f729Sjoerg unsigned Num = MBB.getNumber();
677330f729Sjoerg std::pair<SlotIndex, SlotIndex> &LIP = LastInsertPoint[Num];
687330f729Sjoerg SlotIndex MBBEnd = LIS.getMBBEndIdx(&MBB);
697330f729Sjoerg
70*82d56013Sjoerg SmallVector<const MachineBasicBlock *, 1> ExceptionalSuccessors;
71*82d56013Sjoerg bool EHPadSuccessor = false;
72*82d56013Sjoerg for (const MachineBasicBlock *SMBB : MBB.successors()) {
73*82d56013Sjoerg if (SMBB->isEHPad()) {
74*82d56013Sjoerg ExceptionalSuccessors.push_back(SMBB);
75*82d56013Sjoerg EHPadSuccessor = true;
76*82d56013Sjoerg } else if (SMBB->isInlineAsmBrIndirectTarget())
77*82d56013Sjoerg ExceptionalSuccessors.push_back(SMBB);
78*82d56013Sjoerg }
797330f729Sjoerg
807330f729Sjoerg // Compute insert points on the first call. The pair is independent of the
817330f729Sjoerg // current live interval.
827330f729Sjoerg if (!LIP.first.isValid()) {
837330f729Sjoerg MachineBasicBlock::const_iterator FirstTerm = MBB.getFirstTerminator();
847330f729Sjoerg if (FirstTerm == MBB.end())
857330f729Sjoerg LIP.first = MBBEnd;
867330f729Sjoerg else
877330f729Sjoerg LIP.first = LIS.getInstructionIndex(*FirstTerm);
887330f729Sjoerg
89*82d56013Sjoerg // If there is a landing pad or inlineasm_br successor, also find the
90*82d56013Sjoerg // instruction. If there is no such instruction, we don't need to do
91*82d56013Sjoerg // anything special. We assume there cannot be multiple instructions that
92*82d56013Sjoerg // are Calls with EHPad successors or INLINEASM_BR in a block. Further, we
93*82d56013Sjoerg // assume that if there are any, they will be after any other call
94*82d56013Sjoerg // instructions in the block.
95*82d56013Sjoerg if (ExceptionalSuccessors.empty())
967330f729Sjoerg return LIP.first;
97*82d56013Sjoerg for (const MachineInstr &MI : llvm::reverse(MBB)) {
98*82d56013Sjoerg if ((EHPadSuccessor && MI.isCall()) ||
99*82d56013Sjoerg MI.getOpcode() == TargetOpcode::INLINEASM_BR) {
100*82d56013Sjoerg LIP.second = LIS.getInstructionIndex(MI);
1017330f729Sjoerg break;
1027330f729Sjoerg }
1037330f729Sjoerg }
1047330f729Sjoerg }
1057330f729Sjoerg
1067330f729Sjoerg // If CurLI is live into a landing pad successor, move the last insert point
1077330f729Sjoerg // back to the call that may throw.
1087330f729Sjoerg if (!LIP.second)
1097330f729Sjoerg return LIP.first;
1107330f729Sjoerg
111*82d56013Sjoerg if (none_of(ExceptionalSuccessors, [&](const MachineBasicBlock *EHPad) {
1127330f729Sjoerg return LIS.isLiveInToMBB(CurLI, EHPad);
1137330f729Sjoerg }))
1147330f729Sjoerg return LIP.first;
1157330f729Sjoerg
1167330f729Sjoerg // Find the value leaving MBB.
1177330f729Sjoerg const VNInfo *VNI = CurLI.getVNInfoBefore(MBBEnd);
1187330f729Sjoerg if (!VNI)
1197330f729Sjoerg return LIP.first;
1207330f729Sjoerg
121*82d56013Sjoerg // The def of statepoint instruction is a gc relocation and it should be alive
122*82d56013Sjoerg // in landing pad. So we cannot split interval after statepoint instruction.
123*82d56013Sjoerg if (SlotIndex::isSameInstr(VNI->def, LIP.second))
124*82d56013Sjoerg if (auto *I = LIS.getInstructionFromIndex(LIP.second))
125*82d56013Sjoerg if (I->getOpcode() == TargetOpcode::STATEPOINT)
126*82d56013Sjoerg return LIP.second;
127*82d56013Sjoerg
1287330f729Sjoerg // If the value leaving MBB was defined after the call in MBB, it can't
1297330f729Sjoerg // really be live-in to the landing pad. This can happen if the landing pad
1307330f729Sjoerg // has a PHI, and this register is undef on the exceptional edge.
1317330f729Sjoerg // <rdar://problem/10664933>
1327330f729Sjoerg if (!SlotIndex::isEarlierInstr(VNI->def, LIP.second) && VNI->def < MBBEnd)
1337330f729Sjoerg return LIP.first;
1347330f729Sjoerg
1357330f729Sjoerg // Value is properly live-in to the landing pad.
1367330f729Sjoerg // Only allow inserts before the call.
1377330f729Sjoerg return LIP.second;
1387330f729Sjoerg }
1397330f729Sjoerg
1407330f729Sjoerg MachineBasicBlock::iterator
getLastInsertPointIter(const LiveInterval & CurLI,MachineBasicBlock & MBB)1417330f729Sjoerg InsertPointAnalysis::getLastInsertPointIter(const LiveInterval &CurLI,
1427330f729Sjoerg MachineBasicBlock &MBB) {
1437330f729Sjoerg SlotIndex LIP = getLastInsertPoint(CurLI, MBB);
1447330f729Sjoerg if (LIP == LIS.getMBBEndIdx(&MBB))
1457330f729Sjoerg return MBB.end();
1467330f729Sjoerg return LIS.getInstructionFromIndex(LIP);
1477330f729Sjoerg }
1487330f729Sjoerg
1497330f729Sjoerg //===----------------------------------------------------------------------===//
1507330f729Sjoerg // Split Analysis
1517330f729Sjoerg //===----------------------------------------------------------------------===//
1527330f729Sjoerg
SplitAnalysis(const VirtRegMap & vrm,const LiveIntervals & lis,const MachineLoopInfo & mli)1537330f729Sjoerg SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis,
1547330f729Sjoerg const MachineLoopInfo &mli)
1557330f729Sjoerg : MF(vrm.getMachineFunction()), VRM(vrm), LIS(lis), Loops(mli),
1567330f729Sjoerg TII(*MF.getSubtarget().getInstrInfo()), IPA(lis, MF.getNumBlockIDs()) {}
1577330f729Sjoerg
clear()1587330f729Sjoerg void SplitAnalysis::clear() {
1597330f729Sjoerg UseSlots.clear();
1607330f729Sjoerg UseBlocks.clear();
1617330f729Sjoerg ThroughBlocks.clear();
1627330f729Sjoerg CurLI = nullptr;
1637330f729Sjoerg DidRepairRange = false;
1647330f729Sjoerg }
1657330f729Sjoerg
1667330f729Sjoerg /// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
analyzeUses()1677330f729Sjoerg void SplitAnalysis::analyzeUses() {
1687330f729Sjoerg assert(UseSlots.empty() && "Call clear first");
1697330f729Sjoerg
1707330f729Sjoerg // First get all the defs from the interval values. This provides the correct
1717330f729Sjoerg // slots for early clobbers.
1727330f729Sjoerg for (const VNInfo *VNI : CurLI->valnos)
1737330f729Sjoerg if (!VNI->isPHIDef() && !VNI->isUnused())
1747330f729Sjoerg UseSlots.push_back(VNI->def);
1757330f729Sjoerg
1767330f729Sjoerg // Get use slots form the use-def chain.
1777330f729Sjoerg const MachineRegisterInfo &MRI = MF.getRegInfo();
178*82d56013Sjoerg for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg()))
1797330f729Sjoerg if (!MO.isUndef())
1807330f729Sjoerg UseSlots.push_back(LIS.getInstructionIndex(*MO.getParent()).getRegSlot());
1817330f729Sjoerg
1827330f729Sjoerg array_pod_sort(UseSlots.begin(), UseSlots.end());
1837330f729Sjoerg
1847330f729Sjoerg // Remove duplicates, keeping the smaller slot for each instruction.
1857330f729Sjoerg // That is what we want for early clobbers.
1867330f729Sjoerg UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
1877330f729Sjoerg SlotIndex::isSameInstr),
1887330f729Sjoerg UseSlots.end());
1897330f729Sjoerg
1907330f729Sjoerg // Compute per-live block info.
1917330f729Sjoerg if (!calcLiveBlockInfo()) {
1927330f729Sjoerg // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
1937330f729Sjoerg // I am looking at you, RegisterCoalescer!
1947330f729Sjoerg DidRepairRange = true;
1957330f729Sjoerg ++NumRepairs;
1967330f729Sjoerg LLVM_DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
1977330f729Sjoerg const_cast<LiveIntervals&>(LIS)
1987330f729Sjoerg .shrinkToUses(const_cast<LiveInterval*>(CurLI));
1997330f729Sjoerg UseBlocks.clear();
2007330f729Sjoerg ThroughBlocks.clear();
2017330f729Sjoerg bool fixed = calcLiveBlockInfo();
2027330f729Sjoerg (void)fixed;
2037330f729Sjoerg assert(fixed && "Couldn't fix broken live interval");
2047330f729Sjoerg }
2057330f729Sjoerg
2067330f729Sjoerg LLVM_DEBUG(dbgs() << "Analyze counted " << UseSlots.size() << " instrs in "
2077330f729Sjoerg << UseBlocks.size() << " blocks, through "
2087330f729Sjoerg << NumThroughBlocks << " blocks.\n");
2097330f729Sjoerg }
2107330f729Sjoerg
2117330f729Sjoerg /// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
2127330f729Sjoerg /// where CurLI is live.
calcLiveBlockInfo()2137330f729Sjoerg bool SplitAnalysis::calcLiveBlockInfo() {
2147330f729Sjoerg ThroughBlocks.resize(MF.getNumBlockIDs());
2157330f729Sjoerg NumThroughBlocks = NumGapBlocks = 0;
2167330f729Sjoerg if (CurLI->empty())
2177330f729Sjoerg return true;
2187330f729Sjoerg
2197330f729Sjoerg LiveInterval::const_iterator LVI = CurLI->begin();
2207330f729Sjoerg LiveInterval::const_iterator LVE = CurLI->end();
2217330f729Sjoerg
2227330f729Sjoerg SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
2237330f729Sjoerg UseI = UseSlots.begin();
2247330f729Sjoerg UseE = UseSlots.end();
2257330f729Sjoerg
2267330f729Sjoerg // Loop over basic blocks where CurLI is live.
2277330f729Sjoerg MachineFunction::iterator MFI =
2287330f729Sjoerg LIS.getMBBFromIndex(LVI->start)->getIterator();
2297330f729Sjoerg while (true) {
2307330f729Sjoerg BlockInfo BI;
2317330f729Sjoerg BI.MBB = &*MFI;
2327330f729Sjoerg SlotIndex Start, Stop;
2337330f729Sjoerg std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
2347330f729Sjoerg
2357330f729Sjoerg // If the block contains no uses, the range must be live through. At one
2367330f729Sjoerg // point, RegisterCoalescer could create dangling ranges that ended
2377330f729Sjoerg // mid-block.
2387330f729Sjoerg if (UseI == UseE || *UseI >= Stop) {
2397330f729Sjoerg ++NumThroughBlocks;
2407330f729Sjoerg ThroughBlocks.set(BI.MBB->getNumber());
2417330f729Sjoerg // The range shouldn't end mid-block if there are no uses. This shouldn't
2427330f729Sjoerg // happen.
2437330f729Sjoerg if (LVI->end < Stop)
2447330f729Sjoerg return false;
2457330f729Sjoerg } else {
2467330f729Sjoerg // This block has uses. Find the first and last uses in the block.
2477330f729Sjoerg BI.FirstInstr = *UseI;
2487330f729Sjoerg assert(BI.FirstInstr >= Start);
2497330f729Sjoerg do ++UseI;
2507330f729Sjoerg while (UseI != UseE && *UseI < Stop);
2517330f729Sjoerg BI.LastInstr = UseI[-1];
2527330f729Sjoerg assert(BI.LastInstr < Stop);
2537330f729Sjoerg
2547330f729Sjoerg // LVI is the first live segment overlapping MBB.
2557330f729Sjoerg BI.LiveIn = LVI->start <= Start;
2567330f729Sjoerg
2577330f729Sjoerg // When not live in, the first use should be a def.
2587330f729Sjoerg if (!BI.LiveIn) {
2597330f729Sjoerg assert(LVI->start == LVI->valno->def && "Dangling Segment start");
2607330f729Sjoerg assert(LVI->start == BI.FirstInstr && "First instr should be a def");
2617330f729Sjoerg BI.FirstDef = BI.FirstInstr;
2627330f729Sjoerg }
2637330f729Sjoerg
2647330f729Sjoerg // Look for gaps in the live range.
2657330f729Sjoerg BI.LiveOut = true;
2667330f729Sjoerg while (LVI->end < Stop) {
2677330f729Sjoerg SlotIndex LastStop = LVI->end;
2687330f729Sjoerg if (++LVI == LVE || LVI->start >= Stop) {
2697330f729Sjoerg BI.LiveOut = false;
2707330f729Sjoerg BI.LastInstr = LastStop;
2717330f729Sjoerg break;
2727330f729Sjoerg }
2737330f729Sjoerg
2747330f729Sjoerg if (LastStop < LVI->start) {
2757330f729Sjoerg // There is a gap in the live range. Create duplicate entries for the
2767330f729Sjoerg // live-in snippet and the live-out snippet.
2777330f729Sjoerg ++NumGapBlocks;
2787330f729Sjoerg
2797330f729Sjoerg // Push the Live-in part.
2807330f729Sjoerg BI.LiveOut = false;
2817330f729Sjoerg UseBlocks.push_back(BI);
2827330f729Sjoerg UseBlocks.back().LastInstr = LastStop;
2837330f729Sjoerg
2847330f729Sjoerg // Set up BI for the live-out part.
2857330f729Sjoerg BI.LiveIn = false;
2867330f729Sjoerg BI.LiveOut = true;
2877330f729Sjoerg BI.FirstInstr = BI.FirstDef = LVI->start;
2887330f729Sjoerg }
2897330f729Sjoerg
2907330f729Sjoerg // A Segment that starts in the middle of the block must be a def.
2917330f729Sjoerg assert(LVI->start == LVI->valno->def && "Dangling Segment start");
2927330f729Sjoerg if (!BI.FirstDef)
2937330f729Sjoerg BI.FirstDef = LVI->start;
2947330f729Sjoerg }
2957330f729Sjoerg
2967330f729Sjoerg UseBlocks.push_back(BI);
2977330f729Sjoerg
2987330f729Sjoerg // LVI is now at LVE or LVI->end >= Stop.
2997330f729Sjoerg if (LVI == LVE)
3007330f729Sjoerg break;
3017330f729Sjoerg }
3027330f729Sjoerg
3037330f729Sjoerg // Live segment ends exactly at Stop. Move to the next segment.
3047330f729Sjoerg if (LVI->end == Stop && ++LVI == LVE)
3057330f729Sjoerg break;
3067330f729Sjoerg
3077330f729Sjoerg // Pick the next basic block.
3087330f729Sjoerg if (LVI->start < Stop)
3097330f729Sjoerg ++MFI;
3107330f729Sjoerg else
3117330f729Sjoerg MFI = LIS.getMBBFromIndex(LVI->start)->getIterator();
3127330f729Sjoerg }
3137330f729Sjoerg
3147330f729Sjoerg assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
3157330f729Sjoerg return true;
3167330f729Sjoerg }
3177330f729Sjoerg
countLiveBlocks(const LiveInterval * cli) const3187330f729Sjoerg unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
3197330f729Sjoerg if (cli->empty())
3207330f729Sjoerg return 0;
3217330f729Sjoerg LiveInterval *li = const_cast<LiveInterval*>(cli);
3227330f729Sjoerg LiveInterval::iterator LVI = li->begin();
3237330f729Sjoerg LiveInterval::iterator LVE = li->end();
3247330f729Sjoerg unsigned Count = 0;
3257330f729Sjoerg
3267330f729Sjoerg // Loop over basic blocks where li is live.
3277330f729Sjoerg MachineFunction::const_iterator MFI =
3287330f729Sjoerg LIS.getMBBFromIndex(LVI->start)->getIterator();
3297330f729Sjoerg SlotIndex Stop = LIS.getMBBEndIdx(&*MFI);
3307330f729Sjoerg while (true) {
3317330f729Sjoerg ++Count;
3327330f729Sjoerg LVI = li->advanceTo(LVI, Stop);
3337330f729Sjoerg if (LVI == LVE)
3347330f729Sjoerg return Count;
3357330f729Sjoerg do {
3367330f729Sjoerg ++MFI;
3377330f729Sjoerg Stop = LIS.getMBBEndIdx(&*MFI);
3387330f729Sjoerg } while (Stop <= LVI->start);
3397330f729Sjoerg }
3407330f729Sjoerg }
3417330f729Sjoerg
isOriginalEndpoint(SlotIndex Idx) const3427330f729Sjoerg bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
343*82d56013Sjoerg unsigned OrigReg = VRM.getOriginal(CurLI->reg());
3447330f729Sjoerg const LiveInterval &Orig = LIS.getInterval(OrigReg);
3457330f729Sjoerg assert(!Orig.empty() && "Splitting empty interval?");
3467330f729Sjoerg LiveInterval::const_iterator I = Orig.find(Idx);
3477330f729Sjoerg
3487330f729Sjoerg // Range containing Idx should begin at Idx.
3497330f729Sjoerg if (I != Orig.end() && I->start <= Idx)
3507330f729Sjoerg return I->start == Idx;
3517330f729Sjoerg
3527330f729Sjoerg // Range does not contain Idx, previous must end at Idx.
3537330f729Sjoerg return I != Orig.begin() && (--I)->end == Idx;
3547330f729Sjoerg }
3557330f729Sjoerg
analyze(const LiveInterval * li)3567330f729Sjoerg void SplitAnalysis::analyze(const LiveInterval *li) {
3577330f729Sjoerg clear();
3587330f729Sjoerg CurLI = li;
3597330f729Sjoerg analyzeUses();
3607330f729Sjoerg }
3617330f729Sjoerg
3627330f729Sjoerg //===----------------------------------------------------------------------===//
3637330f729Sjoerg // Split Editor
3647330f729Sjoerg //===----------------------------------------------------------------------===//
3657330f729Sjoerg
3667330f729Sjoerg /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
SplitEditor(SplitAnalysis & SA,AliasAnalysis & AA,LiveIntervals & LIS,VirtRegMap & VRM,MachineDominatorTree & MDT,MachineBlockFrequencyInfo & MBFI,VirtRegAuxInfo & VRAI)367*82d56013Sjoerg SplitEditor::SplitEditor(SplitAnalysis &SA, AliasAnalysis &AA,
368*82d56013Sjoerg LiveIntervals &LIS, VirtRegMap &VRM,
369*82d56013Sjoerg MachineDominatorTree &MDT,
370*82d56013Sjoerg MachineBlockFrequencyInfo &MBFI, VirtRegAuxInfo &VRAI)
371*82d56013Sjoerg : SA(SA), AA(AA), LIS(LIS), VRM(VRM),
372*82d56013Sjoerg MRI(VRM.getMachineFunction().getRegInfo()), MDT(MDT),
373*82d56013Sjoerg TII(*VRM.getMachineFunction().getSubtarget().getInstrInfo()),
374*82d56013Sjoerg TRI(*VRM.getMachineFunction().getSubtarget().getRegisterInfo()),
375*82d56013Sjoerg MBFI(MBFI), VRAI(VRAI), RegAssign(Allocator) {}
3767330f729Sjoerg
reset(LiveRangeEdit & LRE,ComplementSpillMode SM)3777330f729Sjoerg void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
3787330f729Sjoerg Edit = &LRE;
3797330f729Sjoerg SpillMode = SM;
3807330f729Sjoerg OpenIdx = 0;
3817330f729Sjoerg RegAssign.clear();
3827330f729Sjoerg Values.clear();
3837330f729Sjoerg
384*82d56013Sjoerg // Reset the LiveIntervalCalc instances needed for this spill mode.
385*82d56013Sjoerg LICalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
3867330f729Sjoerg &LIS.getVNInfoAllocator());
3877330f729Sjoerg if (SpillMode)
388*82d56013Sjoerg LICalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
3897330f729Sjoerg &LIS.getVNInfoAllocator());
3907330f729Sjoerg
3917330f729Sjoerg // We don't need an AliasAnalysis since we will only be performing
3927330f729Sjoerg // cheap-as-a-copy remats anyway.
3937330f729Sjoerg Edit->anyRematerializable(nullptr);
3947330f729Sjoerg }
3957330f729Sjoerg
3967330f729Sjoerg #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const3977330f729Sjoerg LLVM_DUMP_METHOD void SplitEditor::dump() const {
3987330f729Sjoerg if (RegAssign.empty()) {
3997330f729Sjoerg dbgs() << " empty\n";
4007330f729Sjoerg return;
4017330f729Sjoerg }
4027330f729Sjoerg
4037330f729Sjoerg for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
4047330f729Sjoerg dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
4057330f729Sjoerg dbgs() << '\n';
4067330f729Sjoerg }
4077330f729Sjoerg #endif
4087330f729Sjoerg
getSubRangeForMaskExact(LaneBitmask LM,LiveInterval & LI)409*82d56013Sjoerg LiveInterval::SubRange &SplitEditor::getSubRangeForMaskExact(LaneBitmask LM,
4107330f729Sjoerg LiveInterval &LI) {
4117330f729Sjoerg for (LiveInterval::SubRange &S : LI.subranges())
4127330f729Sjoerg if (S.LaneMask == LM)
4137330f729Sjoerg return S;
4147330f729Sjoerg llvm_unreachable("SubRange for this mask not found");
4157330f729Sjoerg }
4167330f729Sjoerg
getSubRangeForMask(LaneBitmask LM,LiveInterval & LI)417*82d56013Sjoerg LiveInterval::SubRange &SplitEditor::getSubRangeForMask(LaneBitmask LM,
418*82d56013Sjoerg LiveInterval &LI) {
419*82d56013Sjoerg for (LiveInterval::SubRange &S : LI.subranges())
420*82d56013Sjoerg if ((S.LaneMask & LM) == LM)
421*82d56013Sjoerg return S;
422*82d56013Sjoerg llvm_unreachable("SubRange for this mask not found");
423*82d56013Sjoerg }
424*82d56013Sjoerg
addDeadDef(LiveInterval & LI,VNInfo * VNI,bool Original)4257330f729Sjoerg void SplitEditor::addDeadDef(LiveInterval &LI, VNInfo *VNI, bool Original) {
4267330f729Sjoerg if (!LI.hasSubRanges()) {
4277330f729Sjoerg LI.createDeadDef(VNI);
4287330f729Sjoerg return;
4297330f729Sjoerg }
4307330f729Sjoerg
4317330f729Sjoerg SlotIndex Def = VNI->def;
4327330f729Sjoerg if (Original) {
4337330f729Sjoerg // If we are transferring a def from the original interval, make sure
4347330f729Sjoerg // to only update the subranges for which the original subranges had
4357330f729Sjoerg // a def at this location.
4367330f729Sjoerg for (LiveInterval::SubRange &S : LI.subranges()) {
4377330f729Sjoerg auto &PS = getSubRangeForMask(S.LaneMask, Edit->getParent());
4387330f729Sjoerg VNInfo *PV = PS.getVNInfoAt(Def);
4397330f729Sjoerg if (PV != nullptr && PV->def == Def)
4407330f729Sjoerg S.createDeadDef(Def, LIS.getVNInfoAllocator());
4417330f729Sjoerg }
4427330f729Sjoerg } else {
4437330f729Sjoerg // This is a new def: either from rematerialization, or from an inserted
4447330f729Sjoerg // copy. Since rematerialization can regenerate a definition of a sub-
4457330f729Sjoerg // register, we need to check which subranges need to be updated.
4467330f729Sjoerg const MachineInstr *DefMI = LIS.getInstructionFromIndex(Def);
4477330f729Sjoerg assert(DefMI != nullptr);
4487330f729Sjoerg LaneBitmask LM;
4497330f729Sjoerg for (const MachineOperand &DefOp : DefMI->defs()) {
4507330f729Sjoerg Register R = DefOp.getReg();
451*82d56013Sjoerg if (R != LI.reg())
4527330f729Sjoerg continue;
4537330f729Sjoerg if (unsigned SR = DefOp.getSubReg())
4547330f729Sjoerg LM |= TRI.getSubRegIndexLaneMask(SR);
4557330f729Sjoerg else {
4567330f729Sjoerg LM = MRI.getMaxLaneMaskForVReg(R);
4577330f729Sjoerg break;
4587330f729Sjoerg }
4597330f729Sjoerg }
4607330f729Sjoerg for (LiveInterval::SubRange &S : LI.subranges())
4617330f729Sjoerg if ((S.LaneMask & LM).any())
4627330f729Sjoerg S.createDeadDef(Def, LIS.getVNInfoAllocator());
4637330f729Sjoerg }
4647330f729Sjoerg }
4657330f729Sjoerg
defValue(unsigned RegIdx,const VNInfo * ParentVNI,SlotIndex Idx,bool Original)4667330f729Sjoerg VNInfo *SplitEditor::defValue(unsigned RegIdx,
4677330f729Sjoerg const VNInfo *ParentVNI,
4687330f729Sjoerg SlotIndex Idx,
4697330f729Sjoerg bool Original) {
4707330f729Sjoerg assert(ParentVNI && "Mapping NULL value");
4717330f729Sjoerg assert(Idx.isValid() && "Invalid SlotIndex");
4727330f729Sjoerg assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
4737330f729Sjoerg LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
4747330f729Sjoerg
4757330f729Sjoerg // Create a new value.
4767330f729Sjoerg VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
4777330f729Sjoerg
4787330f729Sjoerg bool Force = LI->hasSubRanges();
4797330f729Sjoerg ValueForcePair FP(Force ? nullptr : VNI, Force);
4807330f729Sjoerg // Use insert for lookup, so we can add missing values with a second lookup.
4817330f729Sjoerg std::pair<ValueMap::iterator, bool> InsP =
4827330f729Sjoerg Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), FP));
4837330f729Sjoerg
4847330f729Sjoerg // This was the first time (RegIdx, ParentVNI) was mapped, and it is not
4857330f729Sjoerg // forced. Keep it as a simple def without any liveness.
4867330f729Sjoerg if (!Force && InsP.second)
4877330f729Sjoerg return VNI;
4887330f729Sjoerg
4897330f729Sjoerg // If the previous value was a simple mapping, add liveness for it now.
4907330f729Sjoerg if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
4917330f729Sjoerg addDeadDef(*LI, OldVNI, Original);
4927330f729Sjoerg
4937330f729Sjoerg // No longer a simple mapping. Switch to a complex mapping. If the
4947330f729Sjoerg // interval has subranges, make it a forced mapping.
4957330f729Sjoerg InsP.first->second = ValueForcePair(nullptr, Force);
4967330f729Sjoerg }
4977330f729Sjoerg
4987330f729Sjoerg // This is a complex mapping, add liveness for VNI
4997330f729Sjoerg addDeadDef(*LI, VNI, Original);
5007330f729Sjoerg return VNI;
5017330f729Sjoerg }
5027330f729Sjoerg
forceRecompute(unsigned RegIdx,const VNInfo & ParentVNI)5037330f729Sjoerg void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo &ParentVNI) {
5047330f729Sjoerg ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI.id)];
5057330f729Sjoerg VNInfo *VNI = VFP.getPointer();
5067330f729Sjoerg
5077330f729Sjoerg // ParentVNI was either unmapped or already complex mapped. Either way, just
5087330f729Sjoerg // set the force bit.
5097330f729Sjoerg if (!VNI) {
5107330f729Sjoerg VFP.setInt(true);
5117330f729Sjoerg return;
5127330f729Sjoerg }
5137330f729Sjoerg
5147330f729Sjoerg // This was previously a single mapping. Make sure the old def is represented
5157330f729Sjoerg // by a trivial live range.
5167330f729Sjoerg addDeadDef(LIS.getInterval(Edit->get(RegIdx)), VNI, false);
5177330f729Sjoerg
5187330f729Sjoerg // Mark as complex mapped, forced.
5197330f729Sjoerg VFP = ValueForcePair(nullptr, true);
5207330f729Sjoerg }
5217330f729Sjoerg
buildSingleSubRegCopy(Register FromReg,Register ToReg,MachineBasicBlock & MBB,MachineBasicBlock::iterator InsertBefore,unsigned SubIdx,LiveInterval & DestLI,bool Late,SlotIndex Def)522*82d56013Sjoerg SlotIndex SplitEditor::buildSingleSubRegCopy(Register FromReg, Register ToReg,
5237330f729Sjoerg MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
5247330f729Sjoerg unsigned SubIdx, LiveInterval &DestLI, bool Late, SlotIndex Def) {
5257330f729Sjoerg const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY);
5267330f729Sjoerg bool FirstCopy = !Def.isValid();
5277330f729Sjoerg MachineInstr *CopyMI = BuildMI(MBB, InsertBefore, DebugLoc(), Desc)
5287330f729Sjoerg .addReg(ToReg, RegState::Define | getUndefRegState(FirstCopy)
5297330f729Sjoerg | getInternalReadRegState(!FirstCopy), SubIdx)
5307330f729Sjoerg .addReg(FromReg, 0, SubIdx);
5317330f729Sjoerg
5327330f729Sjoerg BumpPtrAllocator &Allocator = LIS.getVNInfoAllocator();
5337330f729Sjoerg SlotIndexes &Indexes = *LIS.getSlotIndexes();
5347330f729Sjoerg if (FirstCopy) {
5357330f729Sjoerg Def = Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot();
5367330f729Sjoerg } else {
5377330f729Sjoerg CopyMI->bundleWithPred();
5387330f729Sjoerg }
5397330f729Sjoerg LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubIdx);
5407330f729Sjoerg DestLI.refineSubRanges(Allocator, LaneMask,
5417330f729Sjoerg [Def, &Allocator](LiveInterval::SubRange &SR) {
5427330f729Sjoerg SR.createDeadDef(Def, Allocator);
5437330f729Sjoerg },
5447330f729Sjoerg Indexes, TRI);
5457330f729Sjoerg return Def;
5467330f729Sjoerg }
5477330f729Sjoerg
buildCopy(Register FromReg,Register ToReg,LaneBitmask LaneMask,MachineBasicBlock & MBB,MachineBasicBlock::iterator InsertBefore,bool Late,unsigned RegIdx)548*82d56013Sjoerg SlotIndex SplitEditor::buildCopy(Register FromReg, Register ToReg,
5497330f729Sjoerg LaneBitmask LaneMask, MachineBasicBlock &MBB,
5507330f729Sjoerg MachineBasicBlock::iterator InsertBefore, bool Late, unsigned RegIdx) {
5517330f729Sjoerg const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY);
5527330f729Sjoerg if (LaneMask.all() || LaneMask == MRI.getMaxLaneMaskForVReg(FromReg)) {
5537330f729Sjoerg // The full vreg is copied.
5547330f729Sjoerg MachineInstr *CopyMI =
5557330f729Sjoerg BuildMI(MBB, InsertBefore, DebugLoc(), Desc, ToReg).addReg(FromReg);
5567330f729Sjoerg SlotIndexes &Indexes = *LIS.getSlotIndexes();
5577330f729Sjoerg return Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot();
5587330f729Sjoerg }
5597330f729Sjoerg
5607330f729Sjoerg // Only a subset of lanes needs to be copied. The following is a simple
5617330f729Sjoerg // heuristic to construct a sequence of COPYs. We could add a target
5627330f729Sjoerg // specific callback if this turns out to be suboptimal.
5637330f729Sjoerg LiveInterval &DestLI = LIS.getInterval(Edit->get(RegIdx));
5647330f729Sjoerg
5657330f729Sjoerg // First pass: Try to find a perfectly matching subregister index. If none
5667330f729Sjoerg // exists find the one covering the most lanemask bits.
5677330f729Sjoerg const TargetRegisterClass *RC = MRI.getRegClass(FromReg);
5687330f729Sjoerg assert(RC == MRI.getRegClass(ToReg) && "Should have same reg class");
5697330f729Sjoerg
570*82d56013Sjoerg SmallVector<unsigned, 8> Indexes;
5717330f729Sjoerg
5727330f729Sjoerg // Abort if we cannot possibly implement the COPY with the given indexes.
573*82d56013Sjoerg if (!TRI.getCoveringSubRegIndexes(MRI, RC, LaneMask, Indexes))
5747330f729Sjoerg report_fatal_error("Impossible to implement partial COPY");
5757330f729Sjoerg
576*82d56013Sjoerg SlotIndex Def;
577*82d56013Sjoerg for (unsigned BestIdx : Indexes) {
578*82d56013Sjoerg Def = buildSingleSubRegCopy(FromReg, ToReg, MBB, InsertBefore, BestIdx,
5797330f729Sjoerg DestLI, Late, Def);
5807330f729Sjoerg }
5817330f729Sjoerg
5827330f729Sjoerg return Def;
5837330f729Sjoerg }
5847330f729Sjoerg
defFromParent(unsigned RegIdx,VNInfo * ParentVNI,SlotIndex UseIdx,MachineBasicBlock & MBB,MachineBasicBlock::iterator I)5857330f729Sjoerg VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
5867330f729Sjoerg VNInfo *ParentVNI,
5877330f729Sjoerg SlotIndex UseIdx,
5887330f729Sjoerg MachineBasicBlock &MBB,
5897330f729Sjoerg MachineBasicBlock::iterator I) {
5907330f729Sjoerg SlotIndex Def;
5917330f729Sjoerg LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
5927330f729Sjoerg
5937330f729Sjoerg // We may be trying to avoid interference that ends at a deleted instruction,
5947330f729Sjoerg // so always begin RegIdx 0 early and all others late.
5957330f729Sjoerg bool Late = RegIdx != 0;
5967330f729Sjoerg
5977330f729Sjoerg // Attempt cheap-as-a-copy rematerialization.
5987330f729Sjoerg unsigned Original = VRM.getOriginal(Edit->get(RegIdx));
5997330f729Sjoerg LiveInterval &OrigLI = LIS.getInterval(Original);
6007330f729Sjoerg VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx);
6017330f729Sjoerg
602*82d56013Sjoerg Register Reg = LI->reg();
6037330f729Sjoerg bool DidRemat = false;
6047330f729Sjoerg if (OrigVNI) {
6057330f729Sjoerg LiveRangeEdit::Remat RM(ParentVNI);
6067330f729Sjoerg RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def);
6077330f729Sjoerg if (Edit->canRematerializeAt(RM, OrigVNI, UseIdx, true)) {
6087330f729Sjoerg Def = Edit->rematerializeAt(MBB, I, Reg, RM, TRI, Late);
6097330f729Sjoerg ++NumRemats;
6107330f729Sjoerg DidRemat = true;
6117330f729Sjoerg }
6127330f729Sjoerg }
6137330f729Sjoerg if (!DidRemat) {
6147330f729Sjoerg LaneBitmask LaneMask;
615*82d56013Sjoerg if (OrigLI.hasSubRanges()) {
6167330f729Sjoerg LaneMask = LaneBitmask::getNone();
617*82d56013Sjoerg for (LiveInterval::SubRange &S : OrigLI.subranges()) {
618*82d56013Sjoerg if (S.liveAt(UseIdx))
6197330f729Sjoerg LaneMask |= S.LaneMask;
620*82d56013Sjoerg }
6217330f729Sjoerg } else {
6227330f729Sjoerg LaneMask = LaneBitmask::getAll();
6237330f729Sjoerg }
6247330f729Sjoerg
625*82d56013Sjoerg if (LaneMask.none()) {
626*82d56013Sjoerg const MCInstrDesc &Desc = TII.get(TargetOpcode::IMPLICIT_DEF);
627*82d56013Sjoerg MachineInstr *ImplicitDef = BuildMI(MBB, I, DebugLoc(), Desc, Reg);
628*82d56013Sjoerg SlotIndexes &Indexes = *LIS.getSlotIndexes();
629*82d56013Sjoerg Def = Indexes.insertMachineInstrInMaps(*ImplicitDef, Late).getRegSlot();
630*82d56013Sjoerg } else {
6317330f729Sjoerg ++NumCopies;
6327330f729Sjoerg Def = buildCopy(Edit->getReg(), Reg, LaneMask, MBB, I, Late, RegIdx);
6337330f729Sjoerg }
634*82d56013Sjoerg }
6357330f729Sjoerg
6367330f729Sjoerg // Define the value in Reg.
6377330f729Sjoerg return defValue(RegIdx, ParentVNI, Def, false);
6387330f729Sjoerg }
6397330f729Sjoerg
6407330f729Sjoerg /// Create a new virtual register and live interval.
openIntv()6417330f729Sjoerg unsigned SplitEditor::openIntv() {
6427330f729Sjoerg // Create the complement as index 0.
6437330f729Sjoerg if (Edit->empty())
6447330f729Sjoerg Edit->createEmptyInterval();
6457330f729Sjoerg
6467330f729Sjoerg // Create the open interval.
6477330f729Sjoerg OpenIdx = Edit->size();
6487330f729Sjoerg Edit->createEmptyInterval();
6497330f729Sjoerg return OpenIdx;
6507330f729Sjoerg }
6517330f729Sjoerg
selectIntv(unsigned Idx)6527330f729Sjoerg void SplitEditor::selectIntv(unsigned Idx) {
6537330f729Sjoerg assert(Idx != 0 && "Cannot select the complement interval");
6547330f729Sjoerg assert(Idx < Edit->size() && "Can only select previously opened interval");
6557330f729Sjoerg LLVM_DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n');
6567330f729Sjoerg OpenIdx = Idx;
6577330f729Sjoerg }
6587330f729Sjoerg
enterIntvBefore(SlotIndex Idx)6597330f729Sjoerg SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
6607330f729Sjoerg assert(OpenIdx && "openIntv not called before enterIntvBefore");
6617330f729Sjoerg LLVM_DEBUG(dbgs() << " enterIntvBefore " << Idx);
6627330f729Sjoerg Idx = Idx.getBaseIndex();
6637330f729Sjoerg VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
6647330f729Sjoerg if (!ParentVNI) {
6657330f729Sjoerg LLVM_DEBUG(dbgs() << ": not live\n");
6667330f729Sjoerg return Idx;
6677330f729Sjoerg }
6687330f729Sjoerg LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
6697330f729Sjoerg MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
6707330f729Sjoerg assert(MI && "enterIntvBefore called with invalid index");
6717330f729Sjoerg
6727330f729Sjoerg VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
6737330f729Sjoerg return VNI->def;
6747330f729Sjoerg }
6757330f729Sjoerg
enterIntvAfter(SlotIndex Idx)6767330f729Sjoerg SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
6777330f729Sjoerg assert(OpenIdx && "openIntv not called before enterIntvAfter");
6787330f729Sjoerg LLVM_DEBUG(dbgs() << " enterIntvAfter " << Idx);
6797330f729Sjoerg Idx = Idx.getBoundaryIndex();
6807330f729Sjoerg VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
6817330f729Sjoerg if (!ParentVNI) {
6827330f729Sjoerg LLVM_DEBUG(dbgs() << ": not live\n");
6837330f729Sjoerg return Idx;
6847330f729Sjoerg }
6857330f729Sjoerg LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
6867330f729Sjoerg MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
6877330f729Sjoerg assert(MI && "enterIntvAfter called with invalid index");
6887330f729Sjoerg
6897330f729Sjoerg VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
6907330f729Sjoerg std::next(MachineBasicBlock::iterator(MI)));
6917330f729Sjoerg return VNI->def;
6927330f729Sjoerg }
6937330f729Sjoerg
enterIntvAtEnd(MachineBasicBlock & MBB)6947330f729Sjoerg SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
6957330f729Sjoerg assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
6967330f729Sjoerg SlotIndex End = LIS.getMBBEndIdx(&MBB);
6977330f729Sjoerg SlotIndex Last = End.getPrevSlot();
6987330f729Sjoerg LLVM_DEBUG(dbgs() << " enterIntvAtEnd " << printMBBReference(MBB) << ", "
6997330f729Sjoerg << Last);
7007330f729Sjoerg VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
7017330f729Sjoerg if (!ParentVNI) {
7027330f729Sjoerg LLVM_DEBUG(dbgs() << ": not live\n");
7037330f729Sjoerg return End;
7047330f729Sjoerg }
705*82d56013Sjoerg SlotIndex LSP = SA.getLastSplitPoint(&MBB);
706*82d56013Sjoerg if (LSP < Last) {
707*82d56013Sjoerg // It could be that the use after LSP is a def, and thus the ParentVNI
708*82d56013Sjoerg // just selected starts at that def. For this case to exist, the def
709*82d56013Sjoerg // must be part of a tied def/use pair (as otherwise we'd have split
710*82d56013Sjoerg // distinct live ranges into individual live intervals), and thus we
711*82d56013Sjoerg // can insert the def into the VNI of the use and the tied def/use
712*82d56013Sjoerg // pair can live in the resulting interval.
713*82d56013Sjoerg Last = LSP;
714*82d56013Sjoerg ParentVNI = Edit->getParent().getVNInfoAt(Last);
715*82d56013Sjoerg if (!ParentVNI) {
716*82d56013Sjoerg // undef use --> undef tied def
717*82d56013Sjoerg LLVM_DEBUG(dbgs() << ": tied use not live\n");
718*82d56013Sjoerg return End;
719*82d56013Sjoerg }
720*82d56013Sjoerg }
721*82d56013Sjoerg
7227330f729Sjoerg LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id);
7237330f729Sjoerg VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
7247330f729Sjoerg SA.getLastSplitPointIter(&MBB));
7257330f729Sjoerg RegAssign.insert(VNI->def, End, OpenIdx);
7267330f729Sjoerg LLVM_DEBUG(dump());
7277330f729Sjoerg return VNI->def;
7287330f729Sjoerg }
7297330f729Sjoerg
7307330f729Sjoerg /// useIntv - indicate that all instructions in MBB should use OpenLI.
useIntv(const MachineBasicBlock & MBB)7317330f729Sjoerg void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
7327330f729Sjoerg useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
7337330f729Sjoerg }
7347330f729Sjoerg
useIntv(SlotIndex Start,SlotIndex End)7357330f729Sjoerg void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
7367330f729Sjoerg assert(OpenIdx && "openIntv not called before useIntv");
7377330f729Sjoerg LLVM_DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
7387330f729Sjoerg RegAssign.insert(Start, End, OpenIdx);
7397330f729Sjoerg LLVM_DEBUG(dump());
7407330f729Sjoerg }
7417330f729Sjoerg
leaveIntvAfter(SlotIndex Idx)7427330f729Sjoerg SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
7437330f729Sjoerg assert(OpenIdx && "openIntv not called before leaveIntvAfter");
7447330f729Sjoerg LLVM_DEBUG(dbgs() << " leaveIntvAfter " << Idx);
7457330f729Sjoerg
7467330f729Sjoerg // The interval must be live beyond the instruction at Idx.
7477330f729Sjoerg SlotIndex Boundary = Idx.getBoundaryIndex();
7487330f729Sjoerg VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
7497330f729Sjoerg if (!ParentVNI) {
7507330f729Sjoerg LLVM_DEBUG(dbgs() << ": not live\n");
7517330f729Sjoerg return Boundary.getNextSlot();
7527330f729Sjoerg }
7537330f729Sjoerg LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
7547330f729Sjoerg MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
7557330f729Sjoerg assert(MI && "No instruction at index");
7567330f729Sjoerg
7577330f729Sjoerg // In spill mode, make live ranges as short as possible by inserting the copy
7587330f729Sjoerg // before MI. This is only possible if that instruction doesn't redefine the
7597330f729Sjoerg // value. The inserted COPY is not a kill, and we don't need to recompute
7607330f729Sjoerg // the source live range. The spiller also won't try to hoist this copy.
7617330f729Sjoerg if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
7627330f729Sjoerg MI->readsVirtualRegister(Edit->getReg())) {
7637330f729Sjoerg forceRecompute(0, *ParentVNI);
7647330f729Sjoerg defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
7657330f729Sjoerg return Idx;
7667330f729Sjoerg }
7677330f729Sjoerg
7687330f729Sjoerg VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
7697330f729Sjoerg std::next(MachineBasicBlock::iterator(MI)));
7707330f729Sjoerg return VNI->def;
7717330f729Sjoerg }
7727330f729Sjoerg
leaveIntvBefore(SlotIndex Idx)7737330f729Sjoerg SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
7747330f729Sjoerg assert(OpenIdx && "openIntv not called before leaveIntvBefore");
7757330f729Sjoerg LLVM_DEBUG(dbgs() << " leaveIntvBefore " << Idx);
7767330f729Sjoerg
7777330f729Sjoerg // The interval must be live into the instruction at Idx.
7787330f729Sjoerg Idx = Idx.getBaseIndex();
7797330f729Sjoerg VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
7807330f729Sjoerg if (!ParentVNI) {
7817330f729Sjoerg LLVM_DEBUG(dbgs() << ": not live\n");
7827330f729Sjoerg return Idx.getNextSlot();
7837330f729Sjoerg }
7847330f729Sjoerg LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
7857330f729Sjoerg
7867330f729Sjoerg MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
7877330f729Sjoerg assert(MI && "No instruction at index");
7887330f729Sjoerg VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
7897330f729Sjoerg return VNI->def;
7907330f729Sjoerg }
7917330f729Sjoerg
leaveIntvAtTop(MachineBasicBlock & MBB)7927330f729Sjoerg SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
7937330f729Sjoerg assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
7947330f729Sjoerg SlotIndex Start = LIS.getMBBStartIdx(&MBB);
7957330f729Sjoerg LLVM_DEBUG(dbgs() << " leaveIntvAtTop " << printMBBReference(MBB) << ", "
7967330f729Sjoerg << Start);
7977330f729Sjoerg
7987330f729Sjoerg VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
7997330f729Sjoerg if (!ParentVNI) {
8007330f729Sjoerg LLVM_DEBUG(dbgs() << ": not live\n");
8017330f729Sjoerg return Start;
8027330f729Sjoerg }
8037330f729Sjoerg
8047330f729Sjoerg VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
8057330f729Sjoerg MBB.SkipPHIsLabelsAndDebug(MBB.begin()));
8067330f729Sjoerg RegAssign.insert(Start, VNI->def, OpenIdx);
8077330f729Sjoerg LLVM_DEBUG(dump());
8087330f729Sjoerg return VNI->def;
8097330f729Sjoerg }
8107330f729Sjoerg
hasTiedUseOf(MachineInstr & MI,unsigned Reg)811*82d56013Sjoerg static bool hasTiedUseOf(MachineInstr &MI, unsigned Reg) {
812*82d56013Sjoerg return any_of(MI.defs(), [Reg](const MachineOperand &MO) {
813*82d56013Sjoerg return MO.isReg() && MO.isTied() && MO.getReg() == Reg;
814*82d56013Sjoerg });
815*82d56013Sjoerg }
816*82d56013Sjoerg
overlapIntv(SlotIndex Start,SlotIndex End)8177330f729Sjoerg void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
8187330f729Sjoerg assert(OpenIdx && "openIntv not called before overlapIntv");
8197330f729Sjoerg const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
8207330f729Sjoerg assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
8217330f729Sjoerg "Parent changes value in extended range");
8227330f729Sjoerg assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
8237330f729Sjoerg "Range cannot span basic blocks");
8247330f729Sjoerg
825*82d56013Sjoerg // The complement interval will be extended as needed by LICalc.extend().
8267330f729Sjoerg if (ParentVNI)
8277330f729Sjoerg forceRecompute(0, *ParentVNI);
828*82d56013Sjoerg
829*82d56013Sjoerg // If the last use is tied to a def, we can't mark it as live for the
830*82d56013Sjoerg // interval which includes only the use. That would cause the tied pair
831*82d56013Sjoerg // to end up in two different intervals.
832*82d56013Sjoerg if (auto *MI = LIS.getInstructionFromIndex(End))
833*82d56013Sjoerg if (hasTiedUseOf(*MI, Edit->getReg())) {
834*82d56013Sjoerg LLVM_DEBUG(dbgs() << "skip overlap due to tied def at end\n");
835*82d56013Sjoerg return;
836*82d56013Sjoerg }
837*82d56013Sjoerg
8387330f729Sjoerg LLVM_DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
8397330f729Sjoerg RegAssign.insert(Start, End, OpenIdx);
8407330f729Sjoerg LLVM_DEBUG(dump());
8417330f729Sjoerg }
8427330f729Sjoerg
8437330f729Sjoerg //===----------------------------------------------------------------------===//
8447330f729Sjoerg // Spill modes
8457330f729Sjoerg //===----------------------------------------------------------------------===//
8467330f729Sjoerg
removeBackCopies(SmallVectorImpl<VNInfo * > & Copies)8477330f729Sjoerg void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
8487330f729Sjoerg LiveInterval *LI = &LIS.getInterval(Edit->get(0));
8497330f729Sjoerg LLVM_DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
8507330f729Sjoerg RegAssignMap::iterator AssignI;
8517330f729Sjoerg AssignI.setMap(RegAssign);
8527330f729Sjoerg
853*82d56013Sjoerg for (const VNInfo *C : Copies) {
854*82d56013Sjoerg SlotIndex Def = C->def;
8557330f729Sjoerg MachineInstr *MI = LIS.getInstructionFromIndex(Def);
8567330f729Sjoerg assert(MI && "No instruction for back-copy");
8577330f729Sjoerg
8587330f729Sjoerg MachineBasicBlock *MBB = MI->getParent();
8597330f729Sjoerg MachineBasicBlock::iterator MBBI(MI);
8607330f729Sjoerg bool AtBegin;
8617330f729Sjoerg do AtBegin = MBBI == MBB->begin();
862*82d56013Sjoerg while (!AtBegin && (--MBBI)->isDebugOrPseudoInstr());
8637330f729Sjoerg
8647330f729Sjoerg LLVM_DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
8657330f729Sjoerg LIS.removeVRegDefAt(*LI, Def);
8667330f729Sjoerg LIS.RemoveMachineInstrFromMaps(*MI);
8677330f729Sjoerg MI->eraseFromParent();
8687330f729Sjoerg
8697330f729Sjoerg // Adjust RegAssign if a register assignment is killed at Def. We want to
8707330f729Sjoerg // avoid calculating the live range of the source register if possible.
8717330f729Sjoerg AssignI.find(Def.getPrevSlot());
8727330f729Sjoerg if (!AssignI.valid() || AssignI.start() >= Def)
8737330f729Sjoerg continue;
8747330f729Sjoerg // If MI doesn't kill the assigned register, just leave it.
8757330f729Sjoerg if (AssignI.stop() != Def)
8767330f729Sjoerg continue;
8777330f729Sjoerg unsigned RegIdx = AssignI.value();
878*82d56013Sjoerg // We could hoist back-copy right after another back-copy. As a result
879*82d56013Sjoerg // MMBI points to copy instruction which is actually dead now.
880*82d56013Sjoerg // We cannot set its stop to MBBI which will be the same as start and
881*82d56013Sjoerg // interval does not support that.
882*82d56013Sjoerg SlotIndex Kill =
883*82d56013Sjoerg AtBegin ? SlotIndex() : LIS.getInstructionIndex(*MBBI).getRegSlot();
884*82d56013Sjoerg if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg()) ||
885*82d56013Sjoerg Kill <= AssignI.start()) {
8867330f729Sjoerg LLVM_DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx
8877330f729Sjoerg << '\n');
8887330f729Sjoerg forceRecompute(RegIdx, *Edit->getParent().getVNInfoAt(Def));
8897330f729Sjoerg } else {
8907330f729Sjoerg LLVM_DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI);
8917330f729Sjoerg AssignI.setStop(Kill);
8927330f729Sjoerg }
8937330f729Sjoerg }
8947330f729Sjoerg }
8957330f729Sjoerg
8967330f729Sjoerg MachineBasicBlock*
findShallowDominator(MachineBasicBlock * MBB,MachineBasicBlock * DefMBB)8977330f729Sjoerg SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
8987330f729Sjoerg MachineBasicBlock *DefMBB) {
8997330f729Sjoerg if (MBB == DefMBB)
9007330f729Sjoerg return MBB;
9017330f729Sjoerg assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
9027330f729Sjoerg
9037330f729Sjoerg const MachineLoopInfo &Loops = SA.Loops;
9047330f729Sjoerg const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
9057330f729Sjoerg MachineDomTreeNode *DefDomNode = MDT[DefMBB];
9067330f729Sjoerg
9077330f729Sjoerg // Best candidate so far.
9087330f729Sjoerg MachineBasicBlock *BestMBB = MBB;
9097330f729Sjoerg unsigned BestDepth = std::numeric_limits<unsigned>::max();
9107330f729Sjoerg
9117330f729Sjoerg while (true) {
9127330f729Sjoerg const MachineLoop *Loop = Loops.getLoopFor(MBB);
9137330f729Sjoerg
9147330f729Sjoerg // MBB isn't in a loop, it doesn't get any better. All dominators have a
9157330f729Sjoerg // higher frequency by definition.
9167330f729Sjoerg if (!Loop) {
9177330f729Sjoerg LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
9187330f729Sjoerg << " dominates " << printMBBReference(*MBB)
9197330f729Sjoerg << " at depth 0\n");
9207330f729Sjoerg return MBB;
9217330f729Sjoerg }
9227330f729Sjoerg
9237330f729Sjoerg // We'll never be able to exit the DefLoop.
9247330f729Sjoerg if (Loop == DefLoop) {
9257330f729Sjoerg LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
9267330f729Sjoerg << " dominates " << printMBBReference(*MBB)
9277330f729Sjoerg << " in the same loop\n");
9287330f729Sjoerg return MBB;
9297330f729Sjoerg }
9307330f729Sjoerg
9317330f729Sjoerg // Least busy dominator seen so far.
9327330f729Sjoerg unsigned Depth = Loop->getLoopDepth();
9337330f729Sjoerg if (Depth < BestDepth) {
9347330f729Sjoerg BestMBB = MBB;
9357330f729Sjoerg BestDepth = Depth;
9367330f729Sjoerg LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
9377330f729Sjoerg << " dominates " << printMBBReference(*MBB)
9387330f729Sjoerg << " at depth " << Depth << '\n');
9397330f729Sjoerg }
9407330f729Sjoerg
9417330f729Sjoerg // Leave loop by going to the immediate dominator of the loop header.
9427330f729Sjoerg // This is a bigger stride than simply walking up the dominator tree.
9437330f729Sjoerg MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
9447330f729Sjoerg
9457330f729Sjoerg // Too far up the dominator tree?
9467330f729Sjoerg if (!IDom || !MDT.dominates(DefDomNode, IDom))
9477330f729Sjoerg return BestMBB;
9487330f729Sjoerg
9497330f729Sjoerg MBB = IDom->getBlock();
9507330f729Sjoerg }
9517330f729Sjoerg }
9527330f729Sjoerg
computeRedundantBackCopies(DenseSet<unsigned> & NotToHoistSet,SmallVectorImpl<VNInfo * > & BackCopies)9537330f729Sjoerg void SplitEditor::computeRedundantBackCopies(
9547330f729Sjoerg DenseSet<unsigned> &NotToHoistSet, SmallVectorImpl<VNInfo *> &BackCopies) {
9557330f729Sjoerg LiveInterval *LI = &LIS.getInterval(Edit->get(0));
9567330f729Sjoerg LiveInterval *Parent = &Edit->getParent();
9577330f729Sjoerg SmallVector<SmallPtrSet<VNInfo *, 8>, 8> EqualVNs(Parent->getNumValNums());
9587330f729Sjoerg SmallPtrSet<VNInfo *, 8> DominatedVNIs;
9597330f729Sjoerg
9607330f729Sjoerg // Aggregate VNIs having the same value as ParentVNI.
9617330f729Sjoerg for (VNInfo *VNI : LI->valnos) {
9627330f729Sjoerg if (VNI->isUnused())
9637330f729Sjoerg continue;
9647330f729Sjoerg VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
9657330f729Sjoerg EqualVNs[ParentVNI->id].insert(VNI);
9667330f729Sjoerg }
9677330f729Sjoerg
9687330f729Sjoerg // For VNI aggregation of each ParentVNI, collect dominated, i.e.,
9697330f729Sjoerg // redundant VNIs to BackCopies.
9707330f729Sjoerg for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
9717330f729Sjoerg VNInfo *ParentVNI = Parent->getValNumInfo(i);
9727330f729Sjoerg if (!NotToHoistSet.count(ParentVNI->id))
9737330f729Sjoerg continue;
9747330f729Sjoerg SmallPtrSetIterator<VNInfo *> It1 = EqualVNs[ParentVNI->id].begin();
9757330f729Sjoerg SmallPtrSetIterator<VNInfo *> It2 = It1;
9767330f729Sjoerg for (; It1 != EqualVNs[ParentVNI->id].end(); ++It1) {
9777330f729Sjoerg It2 = It1;
9787330f729Sjoerg for (++It2; It2 != EqualVNs[ParentVNI->id].end(); ++It2) {
9797330f729Sjoerg if (DominatedVNIs.count(*It1) || DominatedVNIs.count(*It2))
9807330f729Sjoerg continue;
9817330f729Sjoerg
9827330f729Sjoerg MachineBasicBlock *MBB1 = LIS.getMBBFromIndex((*It1)->def);
9837330f729Sjoerg MachineBasicBlock *MBB2 = LIS.getMBBFromIndex((*It2)->def);
9847330f729Sjoerg if (MBB1 == MBB2) {
9857330f729Sjoerg DominatedVNIs.insert((*It1)->def < (*It2)->def ? (*It2) : (*It1));
9867330f729Sjoerg } else if (MDT.dominates(MBB1, MBB2)) {
9877330f729Sjoerg DominatedVNIs.insert(*It2);
9887330f729Sjoerg } else if (MDT.dominates(MBB2, MBB1)) {
9897330f729Sjoerg DominatedVNIs.insert(*It1);
9907330f729Sjoerg }
9917330f729Sjoerg }
9927330f729Sjoerg }
9937330f729Sjoerg if (!DominatedVNIs.empty()) {
9947330f729Sjoerg forceRecompute(0, *ParentVNI);
995*82d56013Sjoerg append_range(BackCopies, DominatedVNIs);
9967330f729Sjoerg DominatedVNIs.clear();
9977330f729Sjoerg }
9987330f729Sjoerg }
9997330f729Sjoerg }
10007330f729Sjoerg
10017330f729Sjoerg /// For SM_Size mode, find a common dominator for all the back-copies for
10027330f729Sjoerg /// the same ParentVNI and hoist the backcopies to the dominator BB.
10037330f729Sjoerg /// For SM_Speed mode, if the common dominator is hot and it is not beneficial
10047330f729Sjoerg /// to do the hoisting, simply remove the dominated backcopies for the same
10057330f729Sjoerg /// ParentVNI.
hoistCopies()10067330f729Sjoerg void SplitEditor::hoistCopies() {
10077330f729Sjoerg // Get the complement interval, always RegIdx 0.
10087330f729Sjoerg LiveInterval *LI = &LIS.getInterval(Edit->get(0));
10097330f729Sjoerg LiveInterval *Parent = &Edit->getParent();
10107330f729Sjoerg
10117330f729Sjoerg // Track the nearest common dominator for all back-copies for each ParentVNI,
10127330f729Sjoerg // indexed by ParentVNI->id.
10137330f729Sjoerg using DomPair = std::pair<MachineBasicBlock *, SlotIndex>;
10147330f729Sjoerg SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
10157330f729Sjoerg // The total cost of all the back-copies for each ParentVNI.
10167330f729Sjoerg SmallVector<BlockFrequency, 8> Costs(Parent->getNumValNums());
10177330f729Sjoerg // The ParentVNI->id set for which hoisting back-copies are not beneficial
10187330f729Sjoerg // for Speed.
10197330f729Sjoerg DenseSet<unsigned> NotToHoistSet;
10207330f729Sjoerg
10217330f729Sjoerg // Find the nearest common dominator for parent values with multiple
10227330f729Sjoerg // back-copies. If a single back-copy dominates, put it in DomPair.second.
10237330f729Sjoerg for (VNInfo *VNI : LI->valnos) {
10247330f729Sjoerg if (VNI->isUnused())
10257330f729Sjoerg continue;
10267330f729Sjoerg VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
10277330f729Sjoerg assert(ParentVNI && "Parent not live at complement def");
10287330f729Sjoerg
10297330f729Sjoerg // Don't hoist remats. The complement is probably going to disappear
10307330f729Sjoerg // completely anyway.
10317330f729Sjoerg if (Edit->didRematerialize(ParentVNI))
10327330f729Sjoerg continue;
10337330f729Sjoerg
10347330f729Sjoerg MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
10357330f729Sjoerg
10367330f729Sjoerg DomPair &Dom = NearestDom[ParentVNI->id];
10377330f729Sjoerg
10387330f729Sjoerg // Keep directly defined parent values. This is either a PHI or an
10397330f729Sjoerg // instruction in the complement range. All other copies of ParentVNI
10407330f729Sjoerg // should be eliminated.
10417330f729Sjoerg if (VNI->def == ParentVNI->def) {
10427330f729Sjoerg LLVM_DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
10437330f729Sjoerg Dom = DomPair(ValMBB, VNI->def);
10447330f729Sjoerg continue;
10457330f729Sjoerg }
10467330f729Sjoerg // Skip the singly mapped values. There is nothing to gain from hoisting a
10477330f729Sjoerg // single back-copy.
10487330f729Sjoerg if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
10497330f729Sjoerg LLVM_DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
10507330f729Sjoerg continue;
10517330f729Sjoerg }
10527330f729Sjoerg
10537330f729Sjoerg if (!Dom.first) {
10547330f729Sjoerg // First time we see ParentVNI. VNI dominates itself.
10557330f729Sjoerg Dom = DomPair(ValMBB, VNI->def);
10567330f729Sjoerg } else if (Dom.first == ValMBB) {
10577330f729Sjoerg // Two defs in the same block. Pick the earlier def.
10587330f729Sjoerg if (!Dom.second.isValid() || VNI->def < Dom.second)
10597330f729Sjoerg Dom.second = VNI->def;
10607330f729Sjoerg } else {
10617330f729Sjoerg // Different basic blocks. Check if one dominates.
10627330f729Sjoerg MachineBasicBlock *Near =
10637330f729Sjoerg MDT.findNearestCommonDominator(Dom.first, ValMBB);
10647330f729Sjoerg if (Near == ValMBB)
10657330f729Sjoerg // Def ValMBB dominates.
10667330f729Sjoerg Dom = DomPair(ValMBB, VNI->def);
10677330f729Sjoerg else if (Near != Dom.first)
10687330f729Sjoerg // None dominate. Hoist to common dominator, need new def.
10697330f729Sjoerg Dom = DomPair(Near, SlotIndex());
10707330f729Sjoerg Costs[ParentVNI->id] += MBFI.getBlockFreq(ValMBB);
10717330f729Sjoerg }
10727330f729Sjoerg
10737330f729Sjoerg LLVM_DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@'
10747330f729Sjoerg << VNI->def << " for parent " << ParentVNI->id << '@'
10757330f729Sjoerg << ParentVNI->def << " hoist to "
10767330f729Sjoerg << printMBBReference(*Dom.first) << ' ' << Dom.second
10777330f729Sjoerg << '\n');
10787330f729Sjoerg }
10797330f729Sjoerg
10807330f729Sjoerg // Insert the hoisted copies.
10817330f729Sjoerg for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
10827330f729Sjoerg DomPair &Dom = NearestDom[i];
10837330f729Sjoerg if (!Dom.first || Dom.second.isValid())
10847330f729Sjoerg continue;
10857330f729Sjoerg // This value needs a hoisted copy inserted at the end of Dom.first.
10867330f729Sjoerg VNInfo *ParentVNI = Parent->getValNumInfo(i);
10877330f729Sjoerg MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
10887330f729Sjoerg // Get a less loopy dominator than Dom.first.
10897330f729Sjoerg Dom.first = findShallowDominator(Dom.first, DefMBB);
10907330f729Sjoerg if (SpillMode == SM_Speed &&
10917330f729Sjoerg MBFI.getBlockFreq(Dom.first) > Costs[ParentVNI->id]) {
10927330f729Sjoerg NotToHoistSet.insert(ParentVNI->id);
10937330f729Sjoerg continue;
10947330f729Sjoerg }
1095*82d56013Sjoerg SlotIndex LSP = SA.getLastSplitPoint(Dom.first);
1096*82d56013Sjoerg if (LSP <= ParentVNI->def) {
1097*82d56013Sjoerg NotToHoistSet.insert(ParentVNI->id);
1098*82d56013Sjoerg continue;
1099*82d56013Sjoerg }
1100*82d56013Sjoerg Dom.second = defFromParent(0, ParentVNI, LSP, *Dom.first,
11017330f729Sjoerg SA.getLastSplitPointIter(Dom.first))->def;
11027330f729Sjoerg }
11037330f729Sjoerg
11047330f729Sjoerg // Remove redundant back-copies that are now known to be dominated by another
11057330f729Sjoerg // def with the same value.
11067330f729Sjoerg SmallVector<VNInfo*, 8> BackCopies;
11077330f729Sjoerg for (VNInfo *VNI : LI->valnos) {
11087330f729Sjoerg if (VNI->isUnused())
11097330f729Sjoerg continue;
11107330f729Sjoerg VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
11117330f729Sjoerg const DomPair &Dom = NearestDom[ParentVNI->id];
11127330f729Sjoerg if (!Dom.first || Dom.second == VNI->def ||
11137330f729Sjoerg NotToHoistSet.count(ParentVNI->id))
11147330f729Sjoerg continue;
11157330f729Sjoerg BackCopies.push_back(VNI);
11167330f729Sjoerg forceRecompute(0, *ParentVNI);
11177330f729Sjoerg }
11187330f729Sjoerg
11197330f729Sjoerg // If it is not beneficial to hoist all the BackCopies, simply remove
11207330f729Sjoerg // redundant BackCopies in speed mode.
11217330f729Sjoerg if (SpillMode == SM_Speed && !NotToHoistSet.empty())
11227330f729Sjoerg computeRedundantBackCopies(NotToHoistSet, BackCopies);
11237330f729Sjoerg
11247330f729Sjoerg removeBackCopies(BackCopies);
11257330f729Sjoerg }
11267330f729Sjoerg
11277330f729Sjoerg /// transferValues - Transfer all possible values to the new live ranges.
1128*82d56013Sjoerg /// Values that were rematerialized are left alone, they need LICalc.extend().
transferValues()11297330f729Sjoerg bool SplitEditor::transferValues() {
11307330f729Sjoerg bool Skipped = false;
11317330f729Sjoerg RegAssignMap::const_iterator AssignI = RegAssign.begin();
11327330f729Sjoerg for (const LiveRange::Segment &S : Edit->getParent()) {
11337330f729Sjoerg LLVM_DEBUG(dbgs() << " blit " << S << ':');
11347330f729Sjoerg VNInfo *ParentVNI = S.valno;
11357330f729Sjoerg // RegAssign has holes where RegIdx 0 should be used.
11367330f729Sjoerg SlotIndex Start = S.start;
11377330f729Sjoerg AssignI.advanceTo(Start);
11387330f729Sjoerg do {
11397330f729Sjoerg unsigned RegIdx;
11407330f729Sjoerg SlotIndex End = S.end;
11417330f729Sjoerg if (!AssignI.valid()) {
11427330f729Sjoerg RegIdx = 0;
11437330f729Sjoerg } else if (AssignI.start() <= Start) {
11447330f729Sjoerg RegIdx = AssignI.value();
11457330f729Sjoerg if (AssignI.stop() < End) {
11467330f729Sjoerg End = AssignI.stop();
11477330f729Sjoerg ++AssignI;
11487330f729Sjoerg }
11497330f729Sjoerg } else {
11507330f729Sjoerg RegIdx = 0;
11517330f729Sjoerg End = std::min(End, AssignI.start());
11527330f729Sjoerg }
11537330f729Sjoerg
11547330f729Sjoerg // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
11557330f729Sjoerg LLVM_DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx << '('
11567330f729Sjoerg << printReg(Edit->get(RegIdx)) << ')');
11577330f729Sjoerg LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
11587330f729Sjoerg
11597330f729Sjoerg // Check for a simply defined value that can be blitted directly.
11607330f729Sjoerg ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
11617330f729Sjoerg if (VNInfo *VNI = VFP.getPointer()) {
11627330f729Sjoerg LLVM_DEBUG(dbgs() << ':' << VNI->id);
11637330f729Sjoerg LI.addSegment(LiveInterval::Segment(Start, End, VNI));
11647330f729Sjoerg Start = End;
11657330f729Sjoerg continue;
11667330f729Sjoerg }
11677330f729Sjoerg
11687330f729Sjoerg // Skip values with forced recomputation.
11697330f729Sjoerg if (VFP.getInt()) {
11707330f729Sjoerg LLVM_DEBUG(dbgs() << "(recalc)");
11717330f729Sjoerg Skipped = true;
11727330f729Sjoerg Start = End;
11737330f729Sjoerg continue;
11747330f729Sjoerg }
11757330f729Sjoerg
1176*82d56013Sjoerg LiveIntervalCalc &LIC = getLICalc(RegIdx);
11777330f729Sjoerg
11787330f729Sjoerg // This value has multiple defs in RegIdx, but it wasn't rematerialized,
11797330f729Sjoerg // so the live range is accurate. Add live-in blocks in [Start;End) to the
11807330f729Sjoerg // LiveInBlocks.
11817330f729Sjoerg MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
11827330f729Sjoerg SlotIndex BlockStart, BlockEnd;
11837330f729Sjoerg std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(&*MBB);
11847330f729Sjoerg
11857330f729Sjoerg // The first block may be live-in, or it may have its own def.
11867330f729Sjoerg if (Start != BlockStart) {
11877330f729Sjoerg VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
11887330f729Sjoerg assert(VNI && "Missing def for complex mapped value");
11897330f729Sjoerg LLVM_DEBUG(dbgs() << ':' << VNI->id << "*" << printMBBReference(*MBB));
11907330f729Sjoerg // MBB has its own def. Is it also live-out?
11917330f729Sjoerg if (BlockEnd <= End)
1192*82d56013Sjoerg LIC.setLiveOutValue(&*MBB, VNI);
11937330f729Sjoerg
11947330f729Sjoerg // Skip to the next block for live-in.
11957330f729Sjoerg ++MBB;
11967330f729Sjoerg BlockStart = BlockEnd;
11977330f729Sjoerg }
11987330f729Sjoerg
11997330f729Sjoerg // Handle the live-in blocks covered by [Start;End).
12007330f729Sjoerg assert(Start <= BlockStart && "Expected live-in block");
12017330f729Sjoerg while (BlockStart < End) {
12027330f729Sjoerg LLVM_DEBUG(dbgs() << ">" << printMBBReference(*MBB));
12037330f729Sjoerg BlockEnd = LIS.getMBBEndIdx(&*MBB);
12047330f729Sjoerg if (BlockStart == ParentVNI->def) {
12057330f729Sjoerg // This block has the def of a parent PHI, so it isn't live-in.
12067330f729Sjoerg assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
12077330f729Sjoerg VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
12087330f729Sjoerg assert(VNI && "Missing def for complex mapped parent PHI");
12097330f729Sjoerg if (End >= BlockEnd)
1210*82d56013Sjoerg LIC.setLiveOutValue(&*MBB, VNI); // Live-out as well.
12117330f729Sjoerg } else {
12127330f729Sjoerg // This block needs a live-in value. The last block covered may not
12137330f729Sjoerg // be live-out.
12147330f729Sjoerg if (End < BlockEnd)
1215*82d56013Sjoerg LIC.addLiveInBlock(LI, MDT[&*MBB], End);
12167330f729Sjoerg else {
12177330f729Sjoerg // Live-through, and we don't know the value.
1218*82d56013Sjoerg LIC.addLiveInBlock(LI, MDT[&*MBB]);
1219*82d56013Sjoerg LIC.setLiveOutValue(&*MBB, nullptr);
12207330f729Sjoerg }
12217330f729Sjoerg }
12227330f729Sjoerg BlockStart = BlockEnd;
12237330f729Sjoerg ++MBB;
12247330f729Sjoerg }
12257330f729Sjoerg Start = End;
12267330f729Sjoerg } while (Start != S.end);
12277330f729Sjoerg LLVM_DEBUG(dbgs() << '\n');
12287330f729Sjoerg }
12297330f729Sjoerg
1230*82d56013Sjoerg LICalc[0].calculateValues();
12317330f729Sjoerg if (SpillMode)
1232*82d56013Sjoerg LICalc[1].calculateValues();
12337330f729Sjoerg
12347330f729Sjoerg return Skipped;
12357330f729Sjoerg }
12367330f729Sjoerg
removeDeadSegment(SlotIndex Def,LiveRange & LR)12377330f729Sjoerg static bool removeDeadSegment(SlotIndex Def, LiveRange &LR) {
12387330f729Sjoerg const LiveRange::Segment *Seg = LR.getSegmentContaining(Def);
12397330f729Sjoerg if (Seg == nullptr)
12407330f729Sjoerg return true;
12417330f729Sjoerg if (Seg->end != Def.getDeadSlot())
12427330f729Sjoerg return false;
12437330f729Sjoerg // This is a dead PHI. Remove it.
12447330f729Sjoerg LR.removeSegment(*Seg, true);
12457330f729Sjoerg return true;
12467330f729Sjoerg }
12477330f729Sjoerg
extendPHIRange(MachineBasicBlock & B,LiveIntervalCalc & LIC,LiveRange & LR,LaneBitmask LM,ArrayRef<SlotIndex> Undefs)1248*82d56013Sjoerg void SplitEditor::extendPHIRange(MachineBasicBlock &B, LiveIntervalCalc &LIC,
12497330f729Sjoerg LiveRange &LR, LaneBitmask LM,
12507330f729Sjoerg ArrayRef<SlotIndex> Undefs) {
12517330f729Sjoerg for (MachineBasicBlock *P : B.predecessors()) {
12527330f729Sjoerg SlotIndex End = LIS.getMBBEndIdx(P);
12537330f729Sjoerg SlotIndex LastUse = End.getPrevSlot();
12547330f729Sjoerg // The predecessor may not have a live-out value. That is OK, like an
12557330f729Sjoerg // undef PHI operand.
12567330f729Sjoerg LiveInterval &PLI = Edit->getParent();
12577330f729Sjoerg // Need the cast because the inputs to ?: would otherwise be deemed
12587330f729Sjoerg // "incompatible": SubRange vs LiveInterval.
1259*82d56013Sjoerg LiveRange &PSR = !LM.all() ? getSubRangeForMaskExact(LM, PLI)
12607330f729Sjoerg : static_cast<LiveRange &>(PLI);
12617330f729Sjoerg if (PSR.liveAt(LastUse))
1262*82d56013Sjoerg LIC.extend(LR, End, /*PhysReg=*/0, Undefs);
12637330f729Sjoerg }
12647330f729Sjoerg }
12657330f729Sjoerg
extendPHIKillRanges()12667330f729Sjoerg void SplitEditor::extendPHIKillRanges() {
12677330f729Sjoerg // Extend live ranges to be live-out for successor PHI values.
12687330f729Sjoerg
12697330f729Sjoerg // Visit each PHI def slot in the parent live interval. If the def is dead,
12707330f729Sjoerg // remove it. Otherwise, extend the live interval to reach the end indexes
12717330f729Sjoerg // of all predecessor blocks.
12727330f729Sjoerg
12737330f729Sjoerg LiveInterval &ParentLI = Edit->getParent();
12747330f729Sjoerg for (const VNInfo *V : ParentLI.valnos) {
12757330f729Sjoerg if (V->isUnused() || !V->isPHIDef())
12767330f729Sjoerg continue;
12777330f729Sjoerg
12787330f729Sjoerg unsigned RegIdx = RegAssign.lookup(V->def);
12797330f729Sjoerg LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1280*82d56013Sjoerg LiveIntervalCalc &LIC = getLICalc(RegIdx);
12817330f729Sjoerg MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
12827330f729Sjoerg if (!removeDeadSegment(V->def, LI))
1283*82d56013Sjoerg extendPHIRange(B, LIC, LI, LaneBitmask::getAll(), /*Undefs=*/{});
12847330f729Sjoerg }
12857330f729Sjoerg
12867330f729Sjoerg SmallVector<SlotIndex, 4> Undefs;
1287*82d56013Sjoerg LiveIntervalCalc SubLIC;
12887330f729Sjoerg
12897330f729Sjoerg for (LiveInterval::SubRange &PS : ParentLI.subranges()) {
12907330f729Sjoerg for (const VNInfo *V : PS.valnos) {
12917330f729Sjoerg if (V->isUnused() || !V->isPHIDef())
12927330f729Sjoerg continue;
12937330f729Sjoerg unsigned RegIdx = RegAssign.lookup(V->def);
12947330f729Sjoerg LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1295*82d56013Sjoerg LiveInterval::SubRange &S = getSubRangeForMaskExact(PS.LaneMask, LI);
12967330f729Sjoerg if (removeDeadSegment(V->def, S))
12977330f729Sjoerg continue;
12987330f729Sjoerg
12997330f729Sjoerg MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
1300*82d56013Sjoerg SubLIC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
13017330f729Sjoerg &LIS.getVNInfoAllocator());
13027330f729Sjoerg Undefs.clear();
13037330f729Sjoerg LI.computeSubRangeUndefs(Undefs, PS.LaneMask, MRI, *LIS.getSlotIndexes());
1304*82d56013Sjoerg extendPHIRange(B, SubLIC, S, PS.LaneMask, Undefs);
13057330f729Sjoerg }
13067330f729Sjoerg }
13077330f729Sjoerg }
13087330f729Sjoerg
13097330f729Sjoerg /// rewriteAssigned - Rewrite all uses of Edit->getReg().
rewriteAssigned(bool ExtendRanges)13107330f729Sjoerg void SplitEditor::rewriteAssigned(bool ExtendRanges) {
13117330f729Sjoerg struct ExtPoint {
13127330f729Sjoerg ExtPoint(const MachineOperand &O, unsigned R, SlotIndex N)
13137330f729Sjoerg : MO(O), RegIdx(R), Next(N) {}
13147330f729Sjoerg
13157330f729Sjoerg MachineOperand MO;
13167330f729Sjoerg unsigned RegIdx;
13177330f729Sjoerg SlotIndex Next;
13187330f729Sjoerg };
13197330f729Sjoerg
13207330f729Sjoerg SmallVector<ExtPoint,4> ExtPoints;
13217330f729Sjoerg
1322*82d56013Sjoerg for (MachineOperand &MO :
1323*82d56013Sjoerg llvm::make_early_inc_range(MRI.reg_operands(Edit->getReg()))) {
13247330f729Sjoerg MachineInstr *MI = MO.getParent();
13257330f729Sjoerg // LiveDebugVariables should have handled all DBG_VALUE instructions.
13267330f729Sjoerg if (MI->isDebugValue()) {
13277330f729Sjoerg LLVM_DEBUG(dbgs() << "Zapping " << *MI);
13287330f729Sjoerg MO.setReg(0);
13297330f729Sjoerg continue;
13307330f729Sjoerg }
13317330f729Sjoerg
13327330f729Sjoerg // <undef> operands don't really read the register, so it doesn't matter
13337330f729Sjoerg // which register we choose. When the use operand is tied to a def, we must
13347330f729Sjoerg // use the same register as the def, so just do that always.
13357330f729Sjoerg SlotIndex Idx = LIS.getInstructionIndex(*MI);
13367330f729Sjoerg if (MO.isDef() || MO.isUndef())
13377330f729Sjoerg Idx = Idx.getRegSlot(MO.isEarlyClobber());
13387330f729Sjoerg
13397330f729Sjoerg // Rewrite to the mapped register at Idx.
13407330f729Sjoerg unsigned RegIdx = RegAssign.lookup(Idx);
13417330f729Sjoerg LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1342*82d56013Sjoerg MO.setReg(LI.reg());
13437330f729Sjoerg LLVM_DEBUG(dbgs() << " rewr " << printMBBReference(*MI->getParent())
13447330f729Sjoerg << '\t' << Idx << ':' << RegIdx << '\t' << *MI);
13457330f729Sjoerg
13467330f729Sjoerg // Extend liveness to Idx if the instruction reads reg.
13477330f729Sjoerg if (!ExtendRanges || MO.isUndef())
13487330f729Sjoerg continue;
13497330f729Sjoerg
13507330f729Sjoerg // Skip instructions that don't read Reg.
13517330f729Sjoerg if (MO.isDef()) {
13527330f729Sjoerg if (!MO.getSubReg() && !MO.isEarlyClobber())
13537330f729Sjoerg continue;
13547330f729Sjoerg // We may want to extend a live range for a partial redef, or for a use
13557330f729Sjoerg // tied to an early clobber.
13567330f729Sjoerg Idx = Idx.getPrevSlot();
13577330f729Sjoerg if (!Edit->getParent().liveAt(Idx))
13587330f729Sjoerg continue;
13597330f729Sjoerg } else
13607330f729Sjoerg Idx = Idx.getRegSlot(true);
13617330f729Sjoerg
13627330f729Sjoerg SlotIndex Next = Idx.getNextSlot();
13637330f729Sjoerg if (LI.hasSubRanges()) {
13647330f729Sjoerg // We have to delay extending subranges until we have seen all operands
13657330f729Sjoerg // defining the register. This is because a <def,read-undef> operand
13667330f729Sjoerg // will create an "undef" point, and we cannot extend any subranges
13677330f729Sjoerg // until all of them have been accounted for.
13687330f729Sjoerg if (MO.isUse())
13697330f729Sjoerg ExtPoints.push_back(ExtPoint(MO, RegIdx, Next));
13707330f729Sjoerg } else {
1371*82d56013Sjoerg LiveIntervalCalc &LIC = getLICalc(RegIdx);
1372*82d56013Sjoerg LIC.extend(LI, Next, 0, ArrayRef<SlotIndex>());
13737330f729Sjoerg }
13747330f729Sjoerg }
13757330f729Sjoerg
13767330f729Sjoerg for (ExtPoint &EP : ExtPoints) {
13777330f729Sjoerg LiveInterval &LI = LIS.getInterval(Edit->get(EP.RegIdx));
13787330f729Sjoerg assert(LI.hasSubRanges());
13797330f729Sjoerg
1380*82d56013Sjoerg LiveIntervalCalc SubLIC;
13817330f729Sjoerg Register Reg = EP.MO.getReg(), Sub = EP.MO.getSubReg();
13827330f729Sjoerg LaneBitmask LM = Sub != 0 ? TRI.getSubRegIndexLaneMask(Sub)
13837330f729Sjoerg : MRI.getMaxLaneMaskForVReg(Reg);
13847330f729Sjoerg for (LiveInterval::SubRange &S : LI.subranges()) {
13857330f729Sjoerg if ((S.LaneMask & LM).none())
13867330f729Sjoerg continue;
13877330f729Sjoerg // The problem here can be that the new register may have been created
13887330f729Sjoerg // for a partially defined original register. For example:
13897330f729Sjoerg // %0:subreg_hireg<def,read-undef> = ...
13907330f729Sjoerg // ...
13917330f729Sjoerg // %1 = COPY %0
13927330f729Sjoerg if (S.empty())
13937330f729Sjoerg continue;
1394*82d56013Sjoerg SubLIC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
13957330f729Sjoerg &LIS.getVNInfoAllocator());
13967330f729Sjoerg SmallVector<SlotIndex, 4> Undefs;
13977330f729Sjoerg LI.computeSubRangeUndefs(Undefs, S.LaneMask, MRI, *LIS.getSlotIndexes());
1398*82d56013Sjoerg SubLIC.extend(S, EP.Next, 0, Undefs);
13997330f729Sjoerg }
14007330f729Sjoerg }
14017330f729Sjoerg
1402*82d56013Sjoerg for (Register R : *Edit) {
14037330f729Sjoerg LiveInterval &LI = LIS.getInterval(R);
14047330f729Sjoerg if (!LI.hasSubRanges())
14057330f729Sjoerg continue;
14067330f729Sjoerg LI.clear();
14077330f729Sjoerg LI.removeEmptySubRanges();
14087330f729Sjoerg LIS.constructMainRangeFromSubranges(LI);
14097330f729Sjoerg }
14107330f729Sjoerg }
14117330f729Sjoerg
deleteRematVictims()14127330f729Sjoerg void SplitEditor::deleteRematVictims() {
14137330f729Sjoerg SmallVector<MachineInstr*, 8> Dead;
1414*82d56013Sjoerg for (const Register &R : *Edit) {
1415*82d56013Sjoerg LiveInterval *LI = &LIS.getInterval(R);
14167330f729Sjoerg for (const LiveRange::Segment &S : LI->segments) {
14177330f729Sjoerg // Dead defs end at the dead slot.
14187330f729Sjoerg if (S.end != S.valno->def.getDeadSlot())
14197330f729Sjoerg continue;
14207330f729Sjoerg if (S.valno->isPHIDef())
14217330f729Sjoerg continue;
14227330f729Sjoerg MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def);
14237330f729Sjoerg assert(MI && "Missing instruction for dead def");
1424*82d56013Sjoerg MI->addRegisterDead(LI->reg(), &TRI);
14257330f729Sjoerg
14267330f729Sjoerg if (!MI->allDefsAreDead())
14277330f729Sjoerg continue;
14287330f729Sjoerg
14297330f729Sjoerg LLVM_DEBUG(dbgs() << "All defs dead: " << *MI);
14307330f729Sjoerg Dead.push_back(MI);
14317330f729Sjoerg }
14327330f729Sjoerg }
14337330f729Sjoerg
14347330f729Sjoerg if (Dead.empty())
14357330f729Sjoerg return;
14367330f729Sjoerg
14377330f729Sjoerg Edit->eliminateDeadDefs(Dead, None, &AA);
14387330f729Sjoerg }
14397330f729Sjoerg
forceRecomputeVNI(const VNInfo & ParentVNI)14407330f729Sjoerg void SplitEditor::forceRecomputeVNI(const VNInfo &ParentVNI) {
14417330f729Sjoerg // Fast-path for common case.
14427330f729Sjoerg if (!ParentVNI.isPHIDef()) {
14437330f729Sjoerg for (unsigned I = 0, E = Edit->size(); I != E; ++I)
14447330f729Sjoerg forceRecompute(I, ParentVNI);
14457330f729Sjoerg return;
14467330f729Sjoerg }
14477330f729Sjoerg
14487330f729Sjoerg // Trace value through phis.
14497330f729Sjoerg SmallPtrSet<const VNInfo *, 8> Visited; ///< whether VNI was/is in worklist.
14507330f729Sjoerg SmallVector<const VNInfo *, 4> WorkList;
14517330f729Sjoerg Visited.insert(&ParentVNI);
14527330f729Sjoerg WorkList.push_back(&ParentVNI);
14537330f729Sjoerg
14547330f729Sjoerg const LiveInterval &ParentLI = Edit->getParent();
14557330f729Sjoerg const SlotIndexes &Indexes = *LIS.getSlotIndexes();
14567330f729Sjoerg do {
14577330f729Sjoerg const VNInfo &VNI = *WorkList.back();
14587330f729Sjoerg WorkList.pop_back();
14597330f729Sjoerg for (unsigned I = 0, E = Edit->size(); I != E; ++I)
14607330f729Sjoerg forceRecompute(I, VNI);
14617330f729Sjoerg if (!VNI.isPHIDef())
14627330f729Sjoerg continue;
14637330f729Sjoerg
14647330f729Sjoerg MachineBasicBlock &MBB = *Indexes.getMBBFromIndex(VNI.def);
14657330f729Sjoerg for (const MachineBasicBlock *Pred : MBB.predecessors()) {
14667330f729Sjoerg SlotIndex PredEnd = Indexes.getMBBEndIdx(Pred);
14677330f729Sjoerg VNInfo *PredVNI = ParentLI.getVNInfoBefore(PredEnd);
14687330f729Sjoerg assert(PredVNI && "Value available in PhiVNI predecessor");
14697330f729Sjoerg if (Visited.insert(PredVNI).second)
14707330f729Sjoerg WorkList.push_back(PredVNI);
14717330f729Sjoerg }
14727330f729Sjoerg } while(!WorkList.empty());
14737330f729Sjoerg }
14747330f729Sjoerg
finish(SmallVectorImpl<unsigned> * LRMap)14757330f729Sjoerg void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
14767330f729Sjoerg ++NumFinished;
14777330f729Sjoerg
14787330f729Sjoerg // At this point, the live intervals in Edit contain VNInfos corresponding to
14797330f729Sjoerg // the inserted copies.
14807330f729Sjoerg
14817330f729Sjoerg // Add the original defs from the parent interval.
14827330f729Sjoerg for (const VNInfo *ParentVNI : Edit->getParent().valnos) {
14837330f729Sjoerg if (ParentVNI->isUnused())
14847330f729Sjoerg continue;
14857330f729Sjoerg unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
14867330f729Sjoerg defValue(RegIdx, ParentVNI, ParentVNI->def, true);
14877330f729Sjoerg
14887330f729Sjoerg // Force rematted values to be recomputed everywhere.
14897330f729Sjoerg // The new live ranges may be truncated.
14907330f729Sjoerg if (Edit->didRematerialize(ParentVNI))
14917330f729Sjoerg forceRecomputeVNI(*ParentVNI);
14927330f729Sjoerg }
14937330f729Sjoerg
14947330f729Sjoerg // Hoist back-copies to the complement interval when in spill mode.
14957330f729Sjoerg switch (SpillMode) {
14967330f729Sjoerg case SM_Partition:
14977330f729Sjoerg // Leave all back-copies as is.
14987330f729Sjoerg break;
14997330f729Sjoerg case SM_Size:
15007330f729Sjoerg case SM_Speed:
15017330f729Sjoerg // hoistCopies will behave differently between size and speed.
15027330f729Sjoerg hoistCopies();
15037330f729Sjoerg }
15047330f729Sjoerg
15057330f729Sjoerg // Transfer the simply mapped values, check if any are skipped.
15067330f729Sjoerg bool Skipped = transferValues();
15077330f729Sjoerg
15087330f729Sjoerg // Rewrite virtual registers, possibly extending ranges.
15097330f729Sjoerg rewriteAssigned(Skipped);
15107330f729Sjoerg
15117330f729Sjoerg if (Skipped)
15127330f729Sjoerg extendPHIKillRanges();
15137330f729Sjoerg else
15147330f729Sjoerg ++NumSimple;
15157330f729Sjoerg
15167330f729Sjoerg // Delete defs that were rematted everywhere.
15177330f729Sjoerg if (Skipped)
15187330f729Sjoerg deleteRematVictims();
15197330f729Sjoerg
15207330f729Sjoerg // Get rid of unused values and set phi-kill flags.
1521*82d56013Sjoerg for (Register Reg : *Edit) {
15227330f729Sjoerg LiveInterval &LI = LIS.getInterval(Reg);
15237330f729Sjoerg LI.removeEmptySubRanges();
15247330f729Sjoerg LI.RenumberValues();
15257330f729Sjoerg }
15267330f729Sjoerg
15277330f729Sjoerg // Provide a reverse mapping from original indices to Edit ranges.
15287330f729Sjoerg if (LRMap) {
15297330f729Sjoerg LRMap->clear();
15307330f729Sjoerg for (unsigned i = 0, e = Edit->size(); i != e; ++i)
15317330f729Sjoerg LRMap->push_back(i);
15327330f729Sjoerg }
15337330f729Sjoerg
15347330f729Sjoerg // Now check if any registers were separated into multiple components.
15357330f729Sjoerg ConnectedVNInfoEqClasses ConEQ(LIS);
15367330f729Sjoerg for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
15377330f729Sjoerg // Don't use iterators, they are invalidated by create() below.
1538*82d56013Sjoerg Register VReg = Edit->get(i);
15397330f729Sjoerg LiveInterval &LI = LIS.getInterval(VReg);
15407330f729Sjoerg SmallVector<LiveInterval*, 8> SplitLIs;
15417330f729Sjoerg LIS.splitSeparateComponents(LI, SplitLIs);
1542*82d56013Sjoerg Register Original = VRM.getOriginal(VReg);
15437330f729Sjoerg for (LiveInterval *SplitLI : SplitLIs)
1544*82d56013Sjoerg VRM.setIsSplitFromReg(SplitLI->reg(), Original);
15457330f729Sjoerg
15467330f729Sjoerg // The new intervals all map back to i.
15477330f729Sjoerg if (LRMap)
15487330f729Sjoerg LRMap->resize(Edit->size(), i);
15497330f729Sjoerg }
15507330f729Sjoerg
15517330f729Sjoerg // Calculate spill weight and allocation hints for new intervals.
1552*82d56013Sjoerg Edit->calculateRegClassAndHint(VRM.getMachineFunction(), VRAI);
15537330f729Sjoerg
15547330f729Sjoerg assert(!LRMap || LRMap->size() == Edit->size());
15557330f729Sjoerg }
15567330f729Sjoerg
15577330f729Sjoerg //===----------------------------------------------------------------------===//
15587330f729Sjoerg // Single Block Splitting
15597330f729Sjoerg //===----------------------------------------------------------------------===//
15607330f729Sjoerg
shouldSplitSingleBlock(const BlockInfo & BI,bool SingleInstrs) const15617330f729Sjoerg bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
15627330f729Sjoerg bool SingleInstrs) const {
15637330f729Sjoerg // Always split for multiple instructions.
15647330f729Sjoerg if (!BI.isOneInstr())
15657330f729Sjoerg return true;
15667330f729Sjoerg // Don't split for single instructions unless explicitly requested.
15677330f729Sjoerg if (!SingleInstrs)
15687330f729Sjoerg return false;
15697330f729Sjoerg // Splitting a live-through range always makes progress.
15707330f729Sjoerg if (BI.LiveIn && BI.LiveOut)
15717330f729Sjoerg return true;
15727330f729Sjoerg // No point in isolating a copy. It has no register class constraints.
15737330f729Sjoerg if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
15747330f729Sjoerg return false;
15757330f729Sjoerg // Finally, don't isolate an end point that was created by earlier splits.
15767330f729Sjoerg return isOriginalEndpoint(BI.FirstInstr);
15777330f729Sjoerg }
15787330f729Sjoerg
splitSingleBlock(const SplitAnalysis::BlockInfo & BI)15797330f729Sjoerg void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
15807330f729Sjoerg openIntv();
1581*82d56013Sjoerg SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB);
15827330f729Sjoerg SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
15837330f729Sjoerg LastSplitPoint));
15847330f729Sjoerg if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
15857330f729Sjoerg useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
15867330f729Sjoerg } else {
15877330f729Sjoerg // The last use is after the last valid split point.
15887330f729Sjoerg SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
15897330f729Sjoerg useIntv(SegStart, SegStop);
15907330f729Sjoerg overlapIntv(SegStop, BI.LastInstr);
15917330f729Sjoerg }
15927330f729Sjoerg }
15937330f729Sjoerg
15947330f729Sjoerg //===----------------------------------------------------------------------===//
15957330f729Sjoerg // Global Live Range Splitting Support
15967330f729Sjoerg //===----------------------------------------------------------------------===//
15977330f729Sjoerg
15987330f729Sjoerg // These methods support a method of global live range splitting that uses a
15997330f729Sjoerg // global algorithm to decide intervals for CFG edges. They will insert split
16007330f729Sjoerg // points and color intervals in basic blocks while avoiding interference.
16017330f729Sjoerg //
16027330f729Sjoerg // Note that splitSingleBlock is also useful for blocks where both CFG edges
16037330f729Sjoerg // are on the stack.
16047330f729Sjoerg
splitLiveThroughBlock(unsigned MBBNum,unsigned IntvIn,SlotIndex LeaveBefore,unsigned IntvOut,SlotIndex EnterAfter)16057330f729Sjoerg void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
16067330f729Sjoerg unsigned IntvIn, SlotIndex LeaveBefore,
16077330f729Sjoerg unsigned IntvOut, SlotIndex EnterAfter){
16087330f729Sjoerg SlotIndex Start, Stop;
16097330f729Sjoerg std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
16107330f729Sjoerg
16117330f729Sjoerg LLVM_DEBUG(dbgs() << "%bb." << MBBNum << " [" << Start << ';' << Stop
16127330f729Sjoerg << ") intf " << LeaveBefore << '-' << EnterAfter
16137330f729Sjoerg << ", live-through " << IntvIn << " -> " << IntvOut);
16147330f729Sjoerg
16157330f729Sjoerg assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
16167330f729Sjoerg
16177330f729Sjoerg assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
16187330f729Sjoerg assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
16197330f729Sjoerg assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
16207330f729Sjoerg
16217330f729Sjoerg MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
16227330f729Sjoerg
16237330f729Sjoerg if (!IntvOut) {
16247330f729Sjoerg LLVM_DEBUG(dbgs() << ", spill on entry.\n");
16257330f729Sjoerg //
16267330f729Sjoerg // <<<<<<<<< Possible LeaveBefore interference.
16277330f729Sjoerg // |-----------| Live through.
16287330f729Sjoerg // -____________ Spill on entry.
16297330f729Sjoerg //
16307330f729Sjoerg selectIntv(IntvIn);
16317330f729Sjoerg SlotIndex Idx = leaveIntvAtTop(*MBB);
16327330f729Sjoerg assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
16337330f729Sjoerg (void)Idx;
16347330f729Sjoerg return;
16357330f729Sjoerg }
16367330f729Sjoerg
16377330f729Sjoerg if (!IntvIn) {
16387330f729Sjoerg LLVM_DEBUG(dbgs() << ", reload on exit.\n");
16397330f729Sjoerg //
16407330f729Sjoerg // >>>>>>> Possible EnterAfter interference.
16417330f729Sjoerg // |-----------| Live through.
16427330f729Sjoerg // ___________-- Reload on exit.
16437330f729Sjoerg //
16447330f729Sjoerg selectIntv(IntvOut);
16457330f729Sjoerg SlotIndex Idx = enterIntvAtEnd(*MBB);
16467330f729Sjoerg assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
16477330f729Sjoerg (void)Idx;
16487330f729Sjoerg return;
16497330f729Sjoerg }
16507330f729Sjoerg
16517330f729Sjoerg if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
16527330f729Sjoerg LLVM_DEBUG(dbgs() << ", straight through.\n");
16537330f729Sjoerg //
16547330f729Sjoerg // |-----------| Live through.
16557330f729Sjoerg // ------------- Straight through, same intv, no interference.
16567330f729Sjoerg //
16577330f729Sjoerg selectIntv(IntvOut);
16587330f729Sjoerg useIntv(Start, Stop);
16597330f729Sjoerg return;
16607330f729Sjoerg }
16617330f729Sjoerg
16627330f729Sjoerg // We cannot legally insert splits after LSP.
16637330f729Sjoerg SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
16647330f729Sjoerg assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
16657330f729Sjoerg
16667330f729Sjoerg if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
16677330f729Sjoerg LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
16687330f729Sjoerg LLVM_DEBUG(dbgs() << ", switch avoiding interference.\n");
16697330f729Sjoerg //
16707330f729Sjoerg // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference.
16717330f729Sjoerg // |-----------| Live through.
16727330f729Sjoerg // ------======= Switch intervals between interference.
16737330f729Sjoerg //
16747330f729Sjoerg selectIntv(IntvOut);
16757330f729Sjoerg SlotIndex Idx;
16767330f729Sjoerg if (LeaveBefore && LeaveBefore < LSP) {
16777330f729Sjoerg Idx = enterIntvBefore(LeaveBefore);
16787330f729Sjoerg useIntv(Idx, Stop);
16797330f729Sjoerg } else {
16807330f729Sjoerg Idx = enterIntvAtEnd(*MBB);
16817330f729Sjoerg }
16827330f729Sjoerg selectIntv(IntvIn);
16837330f729Sjoerg useIntv(Start, Idx);
16847330f729Sjoerg assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
16857330f729Sjoerg assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
16867330f729Sjoerg return;
16877330f729Sjoerg }
16887330f729Sjoerg
16897330f729Sjoerg LLVM_DEBUG(dbgs() << ", create local intv for interference.\n");
16907330f729Sjoerg //
16917330f729Sjoerg // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference.
16927330f729Sjoerg // |-----------| Live through.
16937330f729Sjoerg // ==---------== Switch intervals before/after interference.
16947330f729Sjoerg //
16957330f729Sjoerg assert(LeaveBefore <= EnterAfter && "Missed case");
16967330f729Sjoerg
16977330f729Sjoerg selectIntv(IntvOut);
16987330f729Sjoerg SlotIndex Idx = enterIntvAfter(EnterAfter);
16997330f729Sjoerg useIntv(Idx, Stop);
17007330f729Sjoerg assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
17017330f729Sjoerg
17027330f729Sjoerg selectIntv(IntvIn);
17037330f729Sjoerg Idx = leaveIntvBefore(LeaveBefore);
17047330f729Sjoerg useIntv(Start, Idx);
17057330f729Sjoerg assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
17067330f729Sjoerg }
17077330f729Sjoerg
splitRegInBlock(const SplitAnalysis::BlockInfo & BI,unsigned IntvIn,SlotIndex LeaveBefore)17087330f729Sjoerg void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
17097330f729Sjoerg unsigned IntvIn, SlotIndex LeaveBefore) {
17107330f729Sjoerg SlotIndex Start, Stop;
17117330f729Sjoerg std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
17127330f729Sjoerg
17137330f729Sjoerg LLVM_DEBUG(dbgs() << printMBBReference(*BI.MBB) << " [" << Start << ';'
17147330f729Sjoerg << Stop << "), uses " << BI.FirstInstr << '-'
17157330f729Sjoerg << BI.LastInstr << ", reg-in " << IntvIn
17167330f729Sjoerg << ", leave before " << LeaveBefore
17177330f729Sjoerg << (BI.LiveOut ? ", stack-out" : ", killed in block"));
17187330f729Sjoerg
17197330f729Sjoerg assert(IntvIn && "Must have register in");
17207330f729Sjoerg assert(BI.LiveIn && "Must be live-in");
17217330f729Sjoerg assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
17227330f729Sjoerg
17237330f729Sjoerg if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
17247330f729Sjoerg LLVM_DEBUG(dbgs() << " before interference.\n");
17257330f729Sjoerg //
17267330f729Sjoerg // <<< Interference after kill.
17277330f729Sjoerg // |---o---x | Killed in block.
17287330f729Sjoerg // ========= Use IntvIn everywhere.
17297330f729Sjoerg //
17307330f729Sjoerg selectIntv(IntvIn);
17317330f729Sjoerg useIntv(Start, BI.LastInstr);
17327330f729Sjoerg return;
17337330f729Sjoerg }
17347330f729Sjoerg
1735*82d56013Sjoerg SlotIndex LSP = SA.getLastSplitPoint(BI.MBB);
17367330f729Sjoerg
17377330f729Sjoerg if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
17387330f729Sjoerg //
17397330f729Sjoerg // <<< Possible interference after last use.
17407330f729Sjoerg // |---o---o---| Live-out on stack.
17417330f729Sjoerg // =========____ Leave IntvIn after last use.
17427330f729Sjoerg //
17437330f729Sjoerg // < Interference after last use.
17447330f729Sjoerg // |---o---o--o| Live-out on stack, late last use.
17457330f729Sjoerg // ============ Copy to stack after LSP, overlap IntvIn.
17467330f729Sjoerg // \_____ Stack interval is live-out.
17477330f729Sjoerg //
17487330f729Sjoerg if (BI.LastInstr < LSP) {
17497330f729Sjoerg LLVM_DEBUG(dbgs() << ", spill after last use before interference.\n");
17507330f729Sjoerg selectIntv(IntvIn);
17517330f729Sjoerg SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
17527330f729Sjoerg useIntv(Start, Idx);
17537330f729Sjoerg assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
17547330f729Sjoerg } else {
17557330f729Sjoerg LLVM_DEBUG(dbgs() << ", spill before last split point.\n");
17567330f729Sjoerg selectIntv(IntvIn);
17577330f729Sjoerg SlotIndex Idx = leaveIntvBefore(LSP);
17587330f729Sjoerg overlapIntv(Idx, BI.LastInstr);
17597330f729Sjoerg useIntv(Start, Idx);
17607330f729Sjoerg assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
17617330f729Sjoerg }
17627330f729Sjoerg return;
17637330f729Sjoerg }
17647330f729Sjoerg
17657330f729Sjoerg // The interference is overlapping somewhere we wanted to use IntvIn. That
17667330f729Sjoerg // means we need to create a local interval that can be allocated a
17677330f729Sjoerg // different register.
17687330f729Sjoerg unsigned LocalIntv = openIntv();
17697330f729Sjoerg (void)LocalIntv;
17707330f729Sjoerg LLVM_DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
17717330f729Sjoerg
17727330f729Sjoerg if (!BI.LiveOut || BI.LastInstr < LSP) {
17737330f729Sjoerg //
17747330f729Sjoerg // <<<<<<< Interference overlapping uses.
17757330f729Sjoerg // |---o---o---| Live-out on stack.
17767330f729Sjoerg // =====----____ Leave IntvIn before interference, then spill.
17777330f729Sjoerg //
17787330f729Sjoerg SlotIndex To = leaveIntvAfter(BI.LastInstr);
17797330f729Sjoerg SlotIndex From = enterIntvBefore(LeaveBefore);
17807330f729Sjoerg useIntv(From, To);
17817330f729Sjoerg selectIntv(IntvIn);
17827330f729Sjoerg useIntv(Start, From);
17837330f729Sjoerg assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
17847330f729Sjoerg return;
17857330f729Sjoerg }
17867330f729Sjoerg
17877330f729Sjoerg // <<<<<<< Interference overlapping uses.
17887330f729Sjoerg // |---o---o--o| Live-out on stack, late last use.
17897330f729Sjoerg // =====------- Copy to stack before LSP, overlap LocalIntv.
17907330f729Sjoerg // \_____ Stack interval is live-out.
17917330f729Sjoerg //
17927330f729Sjoerg SlotIndex To = leaveIntvBefore(LSP);
17937330f729Sjoerg overlapIntv(To, BI.LastInstr);
17947330f729Sjoerg SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
17957330f729Sjoerg useIntv(From, To);
17967330f729Sjoerg selectIntv(IntvIn);
17977330f729Sjoerg useIntv(Start, From);
17987330f729Sjoerg assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
17997330f729Sjoerg }
18007330f729Sjoerg
splitRegOutBlock(const SplitAnalysis::BlockInfo & BI,unsigned IntvOut,SlotIndex EnterAfter)18017330f729Sjoerg void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
18027330f729Sjoerg unsigned IntvOut, SlotIndex EnterAfter) {
18037330f729Sjoerg SlotIndex Start, Stop;
18047330f729Sjoerg std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
18057330f729Sjoerg
18067330f729Sjoerg LLVM_DEBUG(dbgs() << printMBBReference(*BI.MBB) << " [" << Start << ';'
18077330f729Sjoerg << Stop << "), uses " << BI.FirstInstr << '-'
18087330f729Sjoerg << BI.LastInstr << ", reg-out " << IntvOut
18097330f729Sjoerg << ", enter after " << EnterAfter
18107330f729Sjoerg << (BI.LiveIn ? ", stack-in" : ", defined in block"));
18117330f729Sjoerg
1812*82d56013Sjoerg SlotIndex LSP = SA.getLastSplitPoint(BI.MBB);
18137330f729Sjoerg
18147330f729Sjoerg assert(IntvOut && "Must have register out");
18157330f729Sjoerg assert(BI.LiveOut && "Must be live-out");
18167330f729Sjoerg assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
18177330f729Sjoerg
18187330f729Sjoerg if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
18197330f729Sjoerg LLVM_DEBUG(dbgs() << " after interference.\n");
18207330f729Sjoerg //
18217330f729Sjoerg // >>>> Interference before def.
18227330f729Sjoerg // | o---o---| Defined in block.
18237330f729Sjoerg // ========= Use IntvOut everywhere.
18247330f729Sjoerg //
18257330f729Sjoerg selectIntv(IntvOut);
18267330f729Sjoerg useIntv(BI.FirstInstr, Stop);
18277330f729Sjoerg return;
18287330f729Sjoerg }
18297330f729Sjoerg
18307330f729Sjoerg if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
18317330f729Sjoerg LLVM_DEBUG(dbgs() << ", reload after interference.\n");
18327330f729Sjoerg //
18337330f729Sjoerg // >>>> Interference before def.
18347330f729Sjoerg // |---o---o---| Live-through, stack-in.
18357330f729Sjoerg // ____========= Enter IntvOut before first use.
18367330f729Sjoerg //
18377330f729Sjoerg selectIntv(IntvOut);
18387330f729Sjoerg SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
18397330f729Sjoerg useIntv(Idx, Stop);
18407330f729Sjoerg assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
18417330f729Sjoerg return;
18427330f729Sjoerg }
18437330f729Sjoerg
18447330f729Sjoerg // The interference is overlapping somewhere we wanted to use IntvOut. That
18457330f729Sjoerg // means we need to create a local interval that can be allocated a
18467330f729Sjoerg // different register.
18477330f729Sjoerg LLVM_DEBUG(dbgs() << ", interference overlaps uses.\n");
18487330f729Sjoerg //
18497330f729Sjoerg // >>>>>>> Interference overlapping uses.
18507330f729Sjoerg // |---o---o---| Live-through, stack-in.
18517330f729Sjoerg // ____---====== Create local interval for interference range.
18527330f729Sjoerg //
18537330f729Sjoerg selectIntv(IntvOut);
18547330f729Sjoerg SlotIndex Idx = enterIntvAfter(EnterAfter);
18557330f729Sjoerg useIntv(Idx, Stop);
18567330f729Sjoerg assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
18577330f729Sjoerg
18587330f729Sjoerg openIntv();
18597330f729Sjoerg SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
18607330f729Sjoerg useIntv(From, Idx);
18617330f729Sjoerg }
1862*82d56013Sjoerg
print(raw_ostream & OS) const1863*82d56013Sjoerg void SplitAnalysis::BlockInfo::print(raw_ostream &OS) const {
1864*82d56013Sjoerg OS << "{" << printMBBReference(*MBB) << ", "
1865*82d56013Sjoerg << "uses " << FirstInstr << " to " << LastInstr << ", "
1866*82d56013Sjoerg << "1st def " << FirstDef << ", "
1867*82d56013Sjoerg << (LiveIn ? "live in" : "dead in") << ", "
1868*82d56013Sjoerg << (LiveOut ? "live out" : "dead out") << "}";
1869*82d56013Sjoerg }
1870*82d56013Sjoerg
dump() const1871*82d56013Sjoerg void SplitAnalysis::BlockInfo::dump() const {
1872*82d56013Sjoerg print(dbgs());
1873*82d56013Sjoerg dbgs() << "\n";
1874*82d56013Sjoerg }
1875