12cab237bSDimitry Andric //===- SplitKit.cpp - Toolkit for splitting live ranges -------------------===//
2e580952dSDimitry Andric //
3e580952dSDimitry Andric // The LLVM Compiler Infrastructure
4e580952dSDimitry Andric //
5e580952dSDimitry Andric // This file is distributed under the University of Illinois Open Source
6e580952dSDimitry Andric // License. See LICENSE.TXT for details.
7e580952dSDimitry Andric //
8e580952dSDimitry Andric //===----------------------------------------------------------------------===//
9e580952dSDimitry Andric //
10e580952dSDimitry Andric // This file contains the SplitAnalysis class as well as mutator functions for
11e580952dSDimitry Andric // live range splitting.
12e580952dSDimitry Andric //
13e580952dSDimitry Andric //===----------------------------------------------------------------------===//
14e580952dSDimitry Andric
15e580952dSDimitry Andric #include "SplitKit.h"
162cab237bSDimitry Andric #include "LiveRangeCalc.h"
172cab237bSDimitry Andric #include "llvm/ADT/ArrayRef.h"
182cab237bSDimitry Andric #include "llvm/ADT/DenseSet.h"
192cab237bSDimitry Andric #include "llvm/ADT/None.h"
202cab237bSDimitry Andric #include "llvm/ADT/STLExtras.h"
212cab237bSDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
222cab237bSDimitry Andric #include "llvm/ADT/SmallVector.h"
233b0f4066SDimitry Andric #include "llvm/ADT/Statistic.h"
242cab237bSDimitry Andric #include "llvm/CodeGen/LiveInterval.h"
252cab237bSDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
26dff0c46cSDimitry Andric #include "llvm/CodeGen/LiveRangeEdit.h"
272cab237bSDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
283ca95b02SDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
292754fe60SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
302cab237bSDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
312cab237bSDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
32e580952dSDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
336122f3e6SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h"
342cab237bSDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
35e580952dSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
362cab237bSDimitry Andric #include "llvm/CodeGen/SlotIndexes.h"
372cab237bSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
382cab237bSDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h"
392cab237bSDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
402cab237bSDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
41139f7f9bSDimitry Andric #include "llvm/CodeGen/VirtRegMap.h"
42*4ba319b5SDimitry Andric #include "llvm/Config/llvm-config.h"
432cab237bSDimitry Andric #include "llvm/IR/DebugLoc.h"
442cab237bSDimitry Andric #include "llvm/MC/LaneBitmask.h"
452cab237bSDimitry Andric #include "llvm/Support/Allocator.h"
462cab237bSDimitry Andric #include "llvm/Support/BlockFrequency.h"
472cab237bSDimitry Andric #include "llvm/Support/Compiler.h"
48e580952dSDimitry Andric #include "llvm/Support/Debug.h"
492cab237bSDimitry Andric #include "llvm/Support/ErrorHandling.h"
50e580952dSDimitry Andric #include "llvm/Support/raw_ostream.h"
512cab237bSDimitry Andric #include <algorithm>
522cab237bSDimitry Andric #include <cassert>
532cab237bSDimitry Andric #include <iterator>
542cab237bSDimitry Andric #include <limits>
552cab237bSDimitry Andric #include <tuple>
562cab237bSDimitry Andric #include <utility>
57e580952dSDimitry Andric
58e580952dSDimitry Andric using namespace llvm;
59e580952dSDimitry Andric
6091bc56edSDimitry Andric #define DEBUG_TYPE "regalloc"
6191bc56edSDimitry Andric
623b0f4066SDimitry Andric STATISTIC(NumFinished, "Number of splits finished");
633b0f4066SDimitry Andric STATISTIC(NumSimple, "Number of splits that were simple");
64bd5abe19SDimitry Andric STATISTIC(NumCopies, "Number of copies inserted for splitting");
65bd5abe19SDimitry Andric STATISTIC(NumRemats, "Number of rematerialized defs for splitting");
66bd5abe19SDimitry Andric STATISTIC(NumRepairs, "Number of invalid live ranges repaired");
67e580952dSDimitry Andric
68e580952dSDimitry Andric //===----------------------------------------------------------------------===//
693ca95b02SDimitry Andric // Last Insert Point Analysis
703ca95b02SDimitry Andric //===----------------------------------------------------------------------===//
713ca95b02SDimitry Andric
InsertPointAnalysis(const LiveIntervals & lis,unsigned BBNum)723ca95b02SDimitry Andric InsertPointAnalysis::InsertPointAnalysis(const LiveIntervals &lis,
733ca95b02SDimitry Andric unsigned BBNum)
743ca95b02SDimitry Andric : LIS(lis), LastInsertPoint(BBNum) {}
753ca95b02SDimitry Andric
763ca95b02SDimitry Andric SlotIndex
computeLastInsertPoint(const LiveInterval & CurLI,const MachineBasicBlock & MBB)773ca95b02SDimitry Andric InsertPointAnalysis::computeLastInsertPoint(const LiveInterval &CurLI,
783ca95b02SDimitry Andric const MachineBasicBlock &MBB) {
793ca95b02SDimitry Andric unsigned Num = MBB.getNumber();
803ca95b02SDimitry Andric std::pair<SlotIndex, SlotIndex> &LIP = LastInsertPoint[Num];
813ca95b02SDimitry Andric SlotIndex MBBEnd = LIS.getMBBEndIdx(&MBB);
823ca95b02SDimitry Andric
83c4394386SDimitry Andric SmallVector<const MachineBasicBlock *, 1> EHPadSuccessors;
843ca95b02SDimitry Andric for (const MachineBasicBlock *SMBB : MBB.successors())
853ca95b02SDimitry Andric if (SMBB->isEHPad())
86c4394386SDimitry Andric EHPadSuccessors.push_back(SMBB);
873ca95b02SDimitry Andric
883ca95b02SDimitry Andric // Compute insert points on the first call. The pair is independent of the
893ca95b02SDimitry Andric // current live interval.
903ca95b02SDimitry Andric if (!LIP.first.isValid()) {
913ca95b02SDimitry Andric MachineBasicBlock::const_iterator FirstTerm = MBB.getFirstTerminator();
923ca95b02SDimitry Andric if (FirstTerm == MBB.end())
933ca95b02SDimitry Andric LIP.first = MBBEnd;
943ca95b02SDimitry Andric else
953ca95b02SDimitry Andric LIP.first = LIS.getInstructionIndex(*FirstTerm);
963ca95b02SDimitry Andric
973ca95b02SDimitry Andric // If there is a landing pad successor, also find the call instruction.
98c4394386SDimitry Andric if (EHPadSuccessors.empty())
993ca95b02SDimitry Andric return LIP.first;
1003ca95b02SDimitry Andric // There may not be a call instruction (?) in which case we ignore LPad.
1013ca95b02SDimitry Andric LIP.second = LIP.first;
1023ca95b02SDimitry Andric for (MachineBasicBlock::const_iterator I = MBB.end(), E = MBB.begin();
1033ca95b02SDimitry Andric I != E;) {
1043ca95b02SDimitry Andric --I;
1053ca95b02SDimitry Andric if (I->isCall()) {
1063ca95b02SDimitry Andric LIP.second = LIS.getInstructionIndex(*I);
1073ca95b02SDimitry Andric break;
1083ca95b02SDimitry Andric }
1093ca95b02SDimitry Andric }
1103ca95b02SDimitry Andric }
1113ca95b02SDimitry Andric
1123ca95b02SDimitry Andric // If CurLI is live into a landing pad successor, move the last insert point
1133ca95b02SDimitry Andric // back to the call that may throw.
1143ca95b02SDimitry Andric if (!LIP.second)
1153ca95b02SDimitry Andric return LIP.first;
1163ca95b02SDimitry Andric
117c4394386SDimitry Andric if (none_of(EHPadSuccessors, [&](const MachineBasicBlock *EHPad) {
1183ca95b02SDimitry Andric return LIS.isLiveInToMBB(CurLI, EHPad);
1193ca95b02SDimitry Andric }))
1203ca95b02SDimitry Andric return LIP.first;
1213ca95b02SDimitry Andric
1223ca95b02SDimitry Andric // Find the value leaving MBB.
1233ca95b02SDimitry Andric const VNInfo *VNI = CurLI.getVNInfoBefore(MBBEnd);
1243ca95b02SDimitry Andric if (!VNI)
1253ca95b02SDimitry Andric return LIP.first;
1263ca95b02SDimitry Andric
1273ca95b02SDimitry Andric // If the value leaving MBB was defined after the call in MBB, it can't
1283ca95b02SDimitry Andric // really be live-in to the landing pad. This can happen if the landing pad
1293ca95b02SDimitry Andric // has a PHI, and this register is undef on the exceptional edge.
1303ca95b02SDimitry Andric // <rdar://problem/10664933>
1313ca95b02SDimitry Andric if (!SlotIndex::isEarlierInstr(VNI->def, LIP.second) && VNI->def < MBBEnd)
1323ca95b02SDimitry Andric return LIP.first;
1333ca95b02SDimitry Andric
1343ca95b02SDimitry Andric // Value is properly live-in to the landing pad.
1353ca95b02SDimitry Andric // Only allow inserts before the call.
1363ca95b02SDimitry Andric return LIP.second;
1373ca95b02SDimitry Andric }
1383ca95b02SDimitry Andric
1393ca95b02SDimitry Andric MachineBasicBlock::iterator
getLastInsertPointIter(const LiveInterval & CurLI,MachineBasicBlock & MBB)1403ca95b02SDimitry Andric InsertPointAnalysis::getLastInsertPointIter(const LiveInterval &CurLI,
1413ca95b02SDimitry Andric MachineBasicBlock &MBB) {
1423ca95b02SDimitry Andric SlotIndex LIP = getLastInsertPoint(CurLI, MBB);
1433ca95b02SDimitry Andric if (LIP == LIS.getMBBEndIdx(&MBB))
1443ca95b02SDimitry Andric return MBB.end();
1453ca95b02SDimitry Andric return LIS.getInstructionFromIndex(LIP);
1463ca95b02SDimitry Andric }
1473ca95b02SDimitry Andric
1483ca95b02SDimitry Andric //===----------------------------------------------------------------------===//
149e580952dSDimitry Andric // Split Analysis
150e580952dSDimitry Andric //===----------------------------------------------------------------------===//
151e580952dSDimitry Andric
SplitAnalysis(const VirtRegMap & vrm,const LiveIntervals & lis,const MachineLoopInfo & mli)15239d628a0SDimitry Andric SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis,
153e580952dSDimitry Andric const MachineLoopInfo &mli)
15439d628a0SDimitry Andric : MF(vrm.getMachineFunction()), VRM(vrm), LIS(lis), Loops(mli),
1552cab237bSDimitry Andric TII(*MF.getSubtarget().getInstrInfo()), IPA(lis, MF.getNumBlockIDs()) {}
156e580952dSDimitry Andric
clear()157e580952dSDimitry Andric void SplitAnalysis::clear() {
1582754fe60SDimitry Andric UseSlots.clear();
1593b0f4066SDimitry Andric UseBlocks.clear();
1603b0f4066SDimitry Andric ThroughBlocks.clear();
16191bc56edSDimitry Andric CurLI = nullptr;
162bd5abe19SDimitry Andric DidRepairRange = false;
163e580952dSDimitry Andric }
164e580952dSDimitry Andric
1652754fe60SDimitry Andric /// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
analyzeUses()166e580952dSDimitry Andric void SplitAnalysis::analyzeUses() {
1673b0f4066SDimitry Andric assert(UseSlots.empty() && "Call clear first");
1683b0f4066SDimitry Andric
1693b0f4066SDimitry Andric // First get all the defs from the interval values. This provides the correct
1703b0f4066SDimitry Andric // slots for early clobbers.
17139d628a0SDimitry Andric for (const VNInfo *VNI : CurLI->valnos)
17239d628a0SDimitry Andric if (!VNI->isPHIDef() && !VNI->isUnused())
17339d628a0SDimitry Andric UseSlots.push_back(VNI->def);
1743b0f4066SDimitry Andric
1753b0f4066SDimitry Andric // Get use slots form the use-def chain.
1762754fe60SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo();
17791bc56edSDimitry Andric for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg))
17891bc56edSDimitry Andric if (!MO.isUndef())
1793ca95b02SDimitry Andric UseSlots.push_back(LIS.getInstructionIndex(*MO.getParent()).getRegSlot());
1803b0f4066SDimitry Andric
1812754fe60SDimitry Andric array_pod_sort(UseSlots.begin(), UseSlots.end());
1823b0f4066SDimitry Andric
1833b0f4066SDimitry Andric // Remove duplicates, keeping the smaller slot for each instruction.
1843b0f4066SDimitry Andric // That is what we want for early clobbers.
1853b0f4066SDimitry Andric UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
1863b0f4066SDimitry Andric SlotIndex::isSameInstr),
1873b0f4066SDimitry Andric UseSlots.end());
1883b0f4066SDimitry Andric
1893b0f4066SDimitry Andric // Compute per-live block info.
1903b0f4066SDimitry Andric if (!calcLiveBlockInfo()) {
1913b0f4066SDimitry Andric // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
19217a519f9SDimitry Andric // I am looking at you, RegisterCoalescer!
193bd5abe19SDimitry Andric DidRepairRange = true;
194bd5abe19SDimitry Andric ++NumRepairs;
195*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
1963b0f4066SDimitry Andric const_cast<LiveIntervals&>(LIS)
1973b0f4066SDimitry Andric .shrinkToUses(const_cast<LiveInterval*>(CurLI));
1983b0f4066SDimitry Andric UseBlocks.clear();
1993b0f4066SDimitry Andric ThroughBlocks.clear();
2003b0f4066SDimitry Andric bool fixed = calcLiveBlockInfo();
2013b0f4066SDimitry Andric (void)fixed;
2023b0f4066SDimitry Andric assert(fixed && "Couldn't fix broken live interval");
2033b0f4066SDimitry Andric }
2043b0f4066SDimitry Andric
205*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Analyze counted " << UseSlots.size() << " instrs in "
2063b0f4066SDimitry Andric << UseBlocks.size() << " blocks, through "
2073b0f4066SDimitry Andric << NumThroughBlocks << " blocks.\n");
208e580952dSDimitry Andric }
209e580952dSDimitry Andric
2102754fe60SDimitry Andric /// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
2112754fe60SDimitry Andric /// where CurLI is live.
calcLiveBlockInfo()2123b0f4066SDimitry Andric bool SplitAnalysis::calcLiveBlockInfo() {
2133b0f4066SDimitry Andric ThroughBlocks.resize(MF.getNumBlockIDs());
214bd5abe19SDimitry Andric NumThroughBlocks = NumGapBlocks = 0;
2152754fe60SDimitry Andric if (CurLI->empty())
2163b0f4066SDimitry Andric return true;
217e580952dSDimitry Andric
2182754fe60SDimitry Andric LiveInterval::const_iterator LVI = CurLI->begin();
2192754fe60SDimitry Andric LiveInterval::const_iterator LVE = CurLI->end();
220e580952dSDimitry Andric
2212754fe60SDimitry Andric SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
2222754fe60SDimitry Andric UseI = UseSlots.begin();
2232754fe60SDimitry Andric UseE = UseSlots.end();
2242754fe60SDimitry Andric
2252754fe60SDimitry Andric // Loop over basic blocks where CurLI is live.
2267d523365SDimitry Andric MachineFunction::iterator MFI =
2277d523365SDimitry Andric LIS.getMBBFromIndex(LVI->start)->getIterator();
2282cab237bSDimitry Andric while (true) {
2292754fe60SDimitry Andric BlockInfo BI;
2307d523365SDimitry Andric BI.MBB = &*MFI;
2312754fe60SDimitry Andric SlotIndex Start, Stop;
23291bc56edSDimitry Andric std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
2332754fe60SDimitry Andric
234bd5abe19SDimitry Andric // If the block contains no uses, the range must be live through. At one
23517a519f9SDimitry Andric // point, RegisterCoalescer could create dangling ranges that ended
236bd5abe19SDimitry Andric // mid-block.
237bd5abe19SDimitry Andric if (UseI == UseE || *UseI >= Stop) {
238bd5abe19SDimitry Andric ++NumThroughBlocks;
239bd5abe19SDimitry Andric ThroughBlocks.set(BI.MBB->getNumber());
240bd5abe19SDimitry Andric // The range shouldn't end mid-block if there are no uses. This shouldn't
241bd5abe19SDimitry Andric // happen.
242bd5abe19SDimitry Andric if (LVI->end < Stop)
243bd5abe19SDimitry Andric return false;
244bd5abe19SDimitry Andric } else {
245bd5abe19SDimitry Andric // This block has uses. Find the first and last uses in the block.
2466122f3e6SDimitry Andric BI.FirstInstr = *UseI;
2476122f3e6SDimitry Andric assert(BI.FirstInstr >= Start);
2482754fe60SDimitry Andric do ++UseI;
2492754fe60SDimitry Andric while (UseI != UseE && *UseI < Stop);
2506122f3e6SDimitry Andric BI.LastInstr = UseI[-1];
2516122f3e6SDimitry Andric assert(BI.LastInstr < Stop);
252bd5abe19SDimitry Andric
253bd5abe19SDimitry Andric // LVI is the first live segment overlapping MBB.
254bd5abe19SDimitry Andric BI.LiveIn = LVI->start <= Start;
255e580952dSDimitry Andric
2566122f3e6SDimitry Andric // When not live in, the first use should be a def.
2576122f3e6SDimitry Andric if (!BI.LiveIn) {
258f785676fSDimitry Andric assert(LVI->start == LVI->valno->def && "Dangling Segment start");
2596122f3e6SDimitry Andric assert(LVI->start == BI.FirstInstr && "First instr should be a def");
2606122f3e6SDimitry Andric BI.FirstDef = BI.FirstInstr;
2616122f3e6SDimitry Andric }
2626122f3e6SDimitry Andric
2632754fe60SDimitry Andric // Look for gaps in the live range.
2642754fe60SDimitry Andric BI.LiveOut = true;
2652754fe60SDimitry Andric while (LVI->end < Stop) {
2662754fe60SDimitry Andric SlotIndex LastStop = LVI->end;
2672754fe60SDimitry Andric if (++LVI == LVE || LVI->start >= Stop) {
2682754fe60SDimitry Andric BI.LiveOut = false;
2696122f3e6SDimitry Andric BI.LastInstr = LastStop;
270e580952dSDimitry Andric break;
271e580952dSDimitry Andric }
2726122f3e6SDimitry Andric
2732754fe60SDimitry Andric if (LastStop < LVI->start) {
274bd5abe19SDimitry Andric // There is a gap in the live range. Create duplicate entries for the
275bd5abe19SDimitry Andric // live-in snippet and the live-out snippet.
276bd5abe19SDimitry Andric ++NumGapBlocks;
277bd5abe19SDimitry Andric
278bd5abe19SDimitry Andric // Push the Live-in part.
279bd5abe19SDimitry Andric BI.LiveOut = false;
280bd5abe19SDimitry Andric UseBlocks.push_back(BI);
2816122f3e6SDimitry Andric UseBlocks.back().LastInstr = LastStop;
282bd5abe19SDimitry Andric
283bd5abe19SDimitry Andric // Set up BI for the live-out part.
284bd5abe19SDimitry Andric BI.LiveIn = false;
285bd5abe19SDimitry Andric BI.LiveOut = true;
2866122f3e6SDimitry Andric BI.FirstInstr = BI.FirstDef = LVI->start;
287e580952dSDimitry Andric }
288e580952dSDimitry Andric
289f785676fSDimitry Andric // A Segment that starts in the middle of the block must be a def.
290f785676fSDimitry Andric assert(LVI->start == LVI->valno->def && "Dangling Segment start");
2916122f3e6SDimitry Andric if (!BI.FirstDef)
2926122f3e6SDimitry Andric BI.FirstDef = LVI->start;
2936122f3e6SDimitry Andric }
2946122f3e6SDimitry Andric
2953b0f4066SDimitry Andric UseBlocks.push_back(BI);
296e580952dSDimitry Andric
2972754fe60SDimitry Andric // LVI is now at LVE or LVI->end >= Stop.
2982754fe60SDimitry Andric if (LVI == LVE)
2992754fe60SDimitry Andric break;
300bd5abe19SDimitry Andric }
3012754fe60SDimitry Andric
3022754fe60SDimitry Andric // Live segment ends exactly at Stop. Move to the next segment.
3032754fe60SDimitry Andric if (LVI->end == Stop && ++LVI == LVE)
3042754fe60SDimitry Andric break;
3052754fe60SDimitry Andric
3062754fe60SDimitry Andric // Pick the next basic block.
3072754fe60SDimitry Andric if (LVI->start < Stop)
3082754fe60SDimitry Andric ++MFI;
3092754fe60SDimitry Andric else
3107d523365SDimitry Andric MFI = LIS.getMBBFromIndex(LVI->start)->getIterator();
3112754fe60SDimitry Andric }
312bd5abe19SDimitry Andric
313bd5abe19SDimitry Andric assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
3143b0f4066SDimitry Andric return true;
3153b0f4066SDimitry Andric }
3163b0f4066SDimitry Andric
countLiveBlocks(const LiveInterval * cli) const3173b0f4066SDimitry Andric unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
3183b0f4066SDimitry Andric if (cli->empty())
3193b0f4066SDimitry Andric return 0;
3203b0f4066SDimitry Andric LiveInterval *li = const_cast<LiveInterval*>(cli);
3213b0f4066SDimitry Andric LiveInterval::iterator LVI = li->begin();
3223b0f4066SDimitry Andric LiveInterval::iterator LVE = li->end();
3233b0f4066SDimitry Andric unsigned Count = 0;
3243b0f4066SDimitry Andric
3253b0f4066SDimitry Andric // Loop over basic blocks where li is live.
3267d523365SDimitry Andric MachineFunction::const_iterator MFI =
3277d523365SDimitry Andric LIS.getMBBFromIndex(LVI->start)->getIterator();
3287d523365SDimitry Andric SlotIndex Stop = LIS.getMBBEndIdx(&*MFI);
3292cab237bSDimitry Andric while (true) {
3303b0f4066SDimitry Andric ++Count;
3313b0f4066SDimitry Andric LVI = li->advanceTo(LVI, Stop);
3323b0f4066SDimitry Andric if (LVI == LVE)
3333b0f4066SDimitry Andric return Count;
3343b0f4066SDimitry Andric do {
3353b0f4066SDimitry Andric ++MFI;
3367d523365SDimitry Andric Stop = LIS.getMBBEndIdx(&*MFI);
3373b0f4066SDimitry Andric } while (Stop <= LVI->start);
3383b0f4066SDimitry Andric }
339e580952dSDimitry Andric }
340e580952dSDimitry Andric
isOriginalEndpoint(SlotIndex Idx) const341dd6029ffSDimitry Andric bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
342dd6029ffSDimitry Andric unsigned OrigReg = VRM.getOriginal(CurLI->reg);
343dd6029ffSDimitry Andric const LiveInterval &Orig = LIS.getInterval(OrigReg);
344dd6029ffSDimitry Andric assert(!Orig.empty() && "Splitting empty interval?");
345dd6029ffSDimitry Andric LiveInterval::const_iterator I = Orig.find(Idx);
346dd6029ffSDimitry Andric
347dd6029ffSDimitry Andric // Range containing Idx should begin at Idx.
348dd6029ffSDimitry Andric if (I != Orig.end() && I->start <= Idx)
349dd6029ffSDimitry Andric return I->start == Idx;
350dd6029ffSDimitry Andric
351dd6029ffSDimitry Andric // Range does not contain Idx, previous must end at Idx.
352dd6029ffSDimitry Andric return I != Orig.begin() && (--I)->end == Idx;
353dd6029ffSDimitry Andric }
354dd6029ffSDimitry Andric
analyze(const LiveInterval * li)355e580952dSDimitry Andric void SplitAnalysis::analyze(const LiveInterval *li) {
356e580952dSDimitry Andric clear();
3572754fe60SDimitry Andric CurLI = li;
358e580952dSDimitry Andric analyzeUses();
359e580952dSDimitry Andric }
360e580952dSDimitry Andric
361e580952dSDimitry Andric //===----------------------------------------------------------------------===//
3623b0f4066SDimitry Andric // Split Editor
363e580952dSDimitry Andric //===----------------------------------------------------------------------===//
364e580952dSDimitry Andric
3653b0f4066SDimitry Andric /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
SplitEditor(SplitAnalysis & sa,AliasAnalysis & aa,LiveIntervals & lis,VirtRegMap & vrm,MachineDominatorTree & mdt,MachineBlockFrequencyInfo & mbfi)3663ca95b02SDimitry Andric SplitEditor::SplitEditor(SplitAnalysis &sa, AliasAnalysis &aa,
3673ca95b02SDimitry Andric LiveIntervals &lis, VirtRegMap &vrm,
368f785676fSDimitry Andric MachineDominatorTree &mdt,
369f785676fSDimitry Andric MachineBlockFrequencyInfo &mbfi)
3703ca95b02SDimitry Andric : SA(sa), AA(aa), LIS(lis), VRM(vrm),
3713ca95b02SDimitry Andric MRI(vrm.getMachineFunction().getRegInfo()), MDT(mdt),
3723ca95b02SDimitry Andric TII(*vrm.getMachineFunction().getSubtarget().getInstrInfo()),
37339d628a0SDimitry Andric TRI(*vrm.getMachineFunction().getSubtarget().getRegisterInfo()),
3742cab237bSDimitry Andric MBFI(mbfi), RegAssign(Allocator) {}
375e580952dSDimitry Andric
reset(LiveRangeEdit & LRE,ComplementSpillMode SM)3766122f3e6SDimitry Andric void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
3776122f3e6SDimitry Andric Edit = &LRE;
3786122f3e6SDimitry Andric SpillMode = SM;
3793b0f4066SDimitry Andric OpenIdx = 0;
3803b0f4066SDimitry Andric RegAssign.clear();
3812754fe60SDimitry Andric Values.clear();
3823b0f4066SDimitry Andric
3836122f3e6SDimitry Andric // Reset the LiveRangeCalc instances needed for this spill mode.
3847ae0e2c9SDimitry Andric LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
3857ae0e2c9SDimitry Andric &LIS.getVNInfoAllocator());
3866122f3e6SDimitry Andric if (SpillMode)
3877ae0e2c9SDimitry Andric LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
3887ae0e2c9SDimitry Andric &LIS.getVNInfoAllocator());
3893b0f4066SDimitry Andric
3903b0f4066SDimitry Andric // We don't need an AliasAnalysis since we will only be performing
3913b0f4066SDimitry Andric // cheap-as-a-copy remats anyway.
39291bc56edSDimitry Andric Edit->anyRematerializable(nullptr);
3932754fe60SDimitry Andric }
3942754fe60SDimitry Andric
3953861d79fSDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const3963ca95b02SDimitry Andric LLVM_DUMP_METHOD void SplitEditor::dump() const {
3973b0f4066SDimitry Andric if (RegAssign.empty()) {
3983b0f4066SDimitry Andric dbgs() << " empty\n";
3993b0f4066SDimitry Andric return;
4002754fe60SDimitry Andric }
4012754fe60SDimitry Andric
4023b0f4066SDimitry Andric for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
4033b0f4066SDimitry Andric dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
4043b0f4066SDimitry Andric dbgs() << '\n';
4053b0f4066SDimitry Andric }
4063861d79fSDimitry Andric #endif
4073b0f4066SDimitry Andric
getSubRangeForMask(LaneBitmask LM,LiveInterval & LI)408d88c1a5aSDimitry Andric LiveInterval::SubRange &SplitEditor::getSubRangeForMask(LaneBitmask LM,
409d88c1a5aSDimitry Andric LiveInterval &LI) {
410d88c1a5aSDimitry Andric for (LiveInterval::SubRange &S : LI.subranges())
411d88c1a5aSDimitry Andric if (S.LaneMask == LM)
412d88c1a5aSDimitry Andric return S;
413d88c1a5aSDimitry Andric llvm_unreachable("SubRange for this mask not found");
414d88c1a5aSDimitry Andric }
415d88c1a5aSDimitry Andric
addDeadDef(LiveInterval & LI,VNInfo * VNI,bool Original)416d88c1a5aSDimitry Andric void SplitEditor::addDeadDef(LiveInterval &LI, VNInfo *VNI, bool Original) {
417d88c1a5aSDimitry Andric if (!LI.hasSubRanges()) {
418d88c1a5aSDimitry Andric LI.createDeadDef(VNI);
419d88c1a5aSDimitry Andric return;
420d88c1a5aSDimitry Andric }
421d88c1a5aSDimitry Andric
422d88c1a5aSDimitry Andric SlotIndex Def = VNI->def;
423d88c1a5aSDimitry Andric if (Original) {
424d88c1a5aSDimitry Andric // If we are transferring a def from the original interval, make sure
425d88c1a5aSDimitry Andric // to only update the subranges for which the original subranges had
426d88c1a5aSDimitry Andric // a def at this location.
427d88c1a5aSDimitry Andric for (LiveInterval::SubRange &S : LI.subranges()) {
428d88c1a5aSDimitry Andric auto &PS = getSubRangeForMask(S.LaneMask, Edit->getParent());
429d88c1a5aSDimitry Andric VNInfo *PV = PS.getVNInfoAt(Def);
430d88c1a5aSDimitry Andric if (PV != nullptr && PV->def == Def)
431d88c1a5aSDimitry Andric S.createDeadDef(Def, LIS.getVNInfoAllocator());
432d88c1a5aSDimitry Andric }
433d88c1a5aSDimitry Andric } else {
434d88c1a5aSDimitry Andric // This is a new def: either from rematerialization, or from an inserted
435d88c1a5aSDimitry Andric // copy. Since rematerialization can regenerate a definition of a sub-
436d88c1a5aSDimitry Andric // register, we need to check which subranges need to be updated.
437d88c1a5aSDimitry Andric const MachineInstr *DefMI = LIS.getInstructionFromIndex(Def);
438d88c1a5aSDimitry Andric assert(DefMI != nullptr);
439d88c1a5aSDimitry Andric LaneBitmask LM;
440d88c1a5aSDimitry Andric for (const MachineOperand &DefOp : DefMI->defs()) {
441d88c1a5aSDimitry Andric unsigned R = DefOp.getReg();
442d88c1a5aSDimitry Andric if (R != LI.reg)
443d88c1a5aSDimitry Andric continue;
444d88c1a5aSDimitry Andric if (unsigned SR = DefOp.getSubReg())
445d88c1a5aSDimitry Andric LM |= TRI.getSubRegIndexLaneMask(SR);
446d88c1a5aSDimitry Andric else {
447d88c1a5aSDimitry Andric LM = MRI.getMaxLaneMaskForVReg(R);
448d88c1a5aSDimitry Andric break;
449d88c1a5aSDimitry Andric }
450d88c1a5aSDimitry Andric }
451d88c1a5aSDimitry Andric for (LiveInterval::SubRange &S : LI.subranges())
452d88c1a5aSDimitry Andric if ((S.LaneMask & LM).any())
453d88c1a5aSDimitry Andric S.createDeadDef(Def, LIS.getVNInfoAllocator());
454d88c1a5aSDimitry Andric }
455d88c1a5aSDimitry Andric }
456d88c1a5aSDimitry Andric
defValue(unsigned RegIdx,const VNInfo * ParentVNI,SlotIndex Idx,bool Original)4573b0f4066SDimitry Andric VNInfo *SplitEditor::defValue(unsigned RegIdx,
4583b0f4066SDimitry Andric const VNInfo *ParentVNI,
459d88c1a5aSDimitry Andric SlotIndex Idx,
460d88c1a5aSDimitry Andric bool Original) {
461e580952dSDimitry Andric assert(ParentVNI && "Mapping NULL value");
462e580952dSDimitry Andric assert(Idx.isValid() && "Invalid SlotIndex");
4633b0f4066SDimitry Andric assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
464f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
4652754fe60SDimitry Andric
4662754fe60SDimitry Andric // Create a new value.
467dff0c46cSDimitry Andric VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
4682754fe60SDimitry Andric
469d88c1a5aSDimitry Andric bool Force = LI->hasSubRanges();
470d88c1a5aSDimitry Andric ValueForcePair FP(Force ? nullptr : VNI, Force);
471e580952dSDimitry Andric // Use insert for lookup, so we can add missing values with a second lookup.
472e580952dSDimitry Andric std::pair<ValueMap::iterator, bool> InsP =
473d88c1a5aSDimitry Andric Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), FP));
4742754fe60SDimitry Andric
475d88c1a5aSDimitry Andric // This was the first time (RegIdx, ParentVNI) was mapped, and it is not
476d88c1a5aSDimitry Andric // forced. Keep it as a simple def without any liveness.
477d88c1a5aSDimitry Andric if (!Force && InsP.second)
4783b0f4066SDimitry Andric return VNI;
4793b0f4066SDimitry Andric
4803b0f4066SDimitry Andric // If the previous value was a simple mapping, add liveness for it now.
4816122f3e6SDimitry Andric if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
482d88c1a5aSDimitry Andric addDeadDef(*LI, OldVNI, Original);
483d88c1a5aSDimitry Andric
484d88c1a5aSDimitry Andric // No longer a simple mapping. Switch to a complex mapping. If the
485d88c1a5aSDimitry Andric // interval has subranges, make it a forced mapping.
486d88c1a5aSDimitry Andric InsP.first->second = ValueForcePair(nullptr, Force);
4873b0f4066SDimitry Andric }
4883b0f4066SDimitry Andric
4893b0f4066SDimitry Andric // This is a complex mapping, add liveness for VNI
490d88c1a5aSDimitry Andric addDeadDef(*LI, VNI, Original);
4912754fe60SDimitry Andric return VNI;
4922754fe60SDimitry Andric }
4932754fe60SDimitry Andric
forceRecompute(unsigned RegIdx,const VNInfo & ParentVNI)494954b921dSDimitry Andric void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo &ParentVNI) {
495954b921dSDimitry Andric ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI.id)];
4966122f3e6SDimitry Andric VNInfo *VNI = VFP.getPointer();
4973b0f4066SDimitry Andric
4986122f3e6SDimitry Andric // ParentVNI was either unmapped or already complex mapped. Either way, just
4996122f3e6SDimitry Andric // set the force bit.
5006122f3e6SDimitry Andric if (!VNI) {
5016122f3e6SDimitry Andric VFP.setInt(true);
5023b0f4066SDimitry Andric return;
5036122f3e6SDimitry Andric }
5043b0f4066SDimitry Andric
5053b0f4066SDimitry Andric // This was previously a single mapping. Make sure the old def is represented
5063b0f4066SDimitry Andric // by a trivial live range.
507d88c1a5aSDimitry Andric addDeadDef(LIS.getInterval(Edit->get(RegIdx)), VNI, false);
508d88c1a5aSDimitry Andric
5096122f3e6SDimitry Andric // Mark as complex mapped, forced.
51091bc56edSDimitry Andric VFP = ValueForcePair(nullptr, true);
511e580952dSDimitry Andric }
512e580952dSDimitry Andric
buildSingleSubRegCopy(unsigned FromReg,unsigned ToReg,MachineBasicBlock & MBB,MachineBasicBlock::iterator InsertBefore,unsigned SubIdx,LiveInterval & DestLI,bool Late,SlotIndex Def)5137a7e6055SDimitry Andric SlotIndex SplitEditor::buildSingleSubRegCopy(unsigned FromReg, unsigned ToReg,
5147a7e6055SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
5157a7e6055SDimitry Andric unsigned SubIdx, LiveInterval &DestLI, bool Late, SlotIndex Def) {
5167a7e6055SDimitry Andric const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY);
5177a7e6055SDimitry Andric bool FirstCopy = !Def.isValid();
5187a7e6055SDimitry Andric MachineInstr *CopyMI = BuildMI(MBB, InsertBefore, DebugLoc(), Desc)
5197a7e6055SDimitry Andric .addReg(ToReg, RegState::Define | getUndefRegState(FirstCopy)
5207a7e6055SDimitry Andric | getInternalReadRegState(!FirstCopy), SubIdx)
5217a7e6055SDimitry Andric .addReg(FromReg, 0, SubIdx);
5227a7e6055SDimitry Andric
5237a7e6055SDimitry Andric BumpPtrAllocator &Allocator = LIS.getVNInfoAllocator();
5247a7e6055SDimitry Andric if (FirstCopy) {
5257a7e6055SDimitry Andric SlotIndexes &Indexes = *LIS.getSlotIndexes();
5267a7e6055SDimitry Andric Def = Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot();
5277a7e6055SDimitry Andric } else {
5287a7e6055SDimitry Andric CopyMI->bundleWithPred();
5297a7e6055SDimitry Andric }
5307a7e6055SDimitry Andric LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubIdx);
5317a7e6055SDimitry Andric DestLI.refineSubRanges(Allocator, LaneMask,
5327a7e6055SDimitry Andric [Def, &Allocator](LiveInterval::SubRange& SR) {
5337a7e6055SDimitry Andric SR.createDeadDef(Def, Allocator);
5347a7e6055SDimitry Andric });
5357a7e6055SDimitry Andric return Def;
5367a7e6055SDimitry Andric }
5377a7e6055SDimitry Andric
buildCopy(unsigned FromReg,unsigned ToReg,LaneBitmask LaneMask,MachineBasicBlock & MBB,MachineBasicBlock::iterator InsertBefore,bool Late,unsigned RegIdx)5387a7e6055SDimitry Andric SlotIndex SplitEditor::buildCopy(unsigned FromReg, unsigned ToReg,
5397a7e6055SDimitry Andric LaneBitmask LaneMask, MachineBasicBlock &MBB,
5407a7e6055SDimitry Andric MachineBasicBlock::iterator InsertBefore, bool Late, unsigned RegIdx) {
5417a7e6055SDimitry Andric const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY);
5427a7e6055SDimitry Andric if (LaneMask.all() || LaneMask == MRI.getMaxLaneMaskForVReg(FromReg)) {
5437a7e6055SDimitry Andric // The full vreg is copied.
5447a7e6055SDimitry Andric MachineInstr *CopyMI =
5457a7e6055SDimitry Andric BuildMI(MBB, InsertBefore, DebugLoc(), Desc, ToReg).addReg(FromReg);
5467a7e6055SDimitry Andric SlotIndexes &Indexes = *LIS.getSlotIndexes();
5477a7e6055SDimitry Andric return Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot();
5487a7e6055SDimitry Andric }
5497a7e6055SDimitry Andric
5507a7e6055SDimitry Andric // Only a subset of lanes needs to be copied. The following is a simple
5517a7e6055SDimitry Andric // heuristic to construct a sequence of COPYs. We could add a target
5527a7e6055SDimitry Andric // specific callback if this turns out to be suboptimal.
5537a7e6055SDimitry Andric LiveInterval &DestLI = LIS.getInterval(Edit->get(RegIdx));
5547a7e6055SDimitry Andric
5557a7e6055SDimitry Andric // First pass: Try to find a perfectly matching subregister index. If none
5567a7e6055SDimitry Andric // exists find the one covering the most lanemask bits.
5577a7e6055SDimitry Andric SmallVector<unsigned, 8> PossibleIndexes;
5587a7e6055SDimitry Andric unsigned BestIdx = 0;
5597a7e6055SDimitry Andric unsigned BestCover = 0;
5607a7e6055SDimitry Andric const TargetRegisterClass *RC = MRI.getRegClass(FromReg);
5617a7e6055SDimitry Andric assert(RC == MRI.getRegClass(ToReg) && "Should have same reg class");
5627a7e6055SDimitry Andric for (unsigned Idx = 1, E = TRI.getNumSubRegIndices(); Idx < E; ++Idx) {
5637a7e6055SDimitry Andric // Is this index even compatible with the given class?
5647a7e6055SDimitry Andric if (TRI.getSubClassWithSubReg(RC, Idx) != RC)
5657a7e6055SDimitry Andric continue;
5667a7e6055SDimitry Andric LaneBitmask SubRegMask = TRI.getSubRegIndexLaneMask(Idx);
5677a7e6055SDimitry Andric // Early exit if we found a perfect match.
5687a7e6055SDimitry Andric if (SubRegMask == LaneMask) {
5697a7e6055SDimitry Andric BestIdx = Idx;
5707a7e6055SDimitry Andric break;
5717a7e6055SDimitry Andric }
5727a7e6055SDimitry Andric
5737a7e6055SDimitry Andric // The index must not cover any lanes outside \p LaneMask.
5747a7e6055SDimitry Andric if ((SubRegMask & ~LaneMask).any())
5757a7e6055SDimitry Andric continue;
5767a7e6055SDimitry Andric
5772cab237bSDimitry Andric unsigned PopCount = SubRegMask.getNumLanes();
5787a7e6055SDimitry Andric PossibleIndexes.push_back(Idx);
5797a7e6055SDimitry Andric if (PopCount > BestCover) {
5807a7e6055SDimitry Andric BestCover = PopCount;
5817a7e6055SDimitry Andric BestIdx = Idx;
5827a7e6055SDimitry Andric }
5837a7e6055SDimitry Andric }
5847a7e6055SDimitry Andric
5857a7e6055SDimitry Andric // Abort if we cannot possibly implement the COPY with the given indexes.
5867a7e6055SDimitry Andric if (BestIdx == 0)
5877a7e6055SDimitry Andric report_fatal_error("Impossible to implement partial COPY");
5887a7e6055SDimitry Andric
5897a7e6055SDimitry Andric SlotIndex Def = buildSingleSubRegCopy(FromReg, ToReg, MBB, InsertBefore,
5907a7e6055SDimitry Andric BestIdx, DestLI, Late, SlotIndex());
5917a7e6055SDimitry Andric
5927a7e6055SDimitry Andric // Greedy heuristic: Keep iterating keeping the best covering subreg index
5937a7e6055SDimitry Andric // each time.
59424d58133SDimitry Andric LaneBitmask LanesLeft = LaneMask & ~(TRI.getSubRegIndexLaneMask(BestIdx));
5957a7e6055SDimitry Andric while (LanesLeft.any()) {
5967a7e6055SDimitry Andric unsigned BestIdx = 0;
5972cab237bSDimitry Andric int BestCover = std::numeric_limits<int>::min();
5987a7e6055SDimitry Andric for (unsigned Idx : PossibleIndexes) {
5997a7e6055SDimitry Andric LaneBitmask SubRegMask = TRI.getSubRegIndexLaneMask(Idx);
6007a7e6055SDimitry Andric // Early exit if we found a perfect match.
6017a7e6055SDimitry Andric if (SubRegMask == LanesLeft) {
6027a7e6055SDimitry Andric BestIdx = Idx;
6037a7e6055SDimitry Andric break;
6047a7e6055SDimitry Andric }
6057a7e6055SDimitry Andric
6067a7e6055SDimitry Andric // Try to cover as much of the remaining lanes as possible but
6077a7e6055SDimitry Andric // as few of the already covered lanes as possible.
6082cab237bSDimitry Andric int Cover = (SubRegMask & LanesLeft).getNumLanes()
6092cab237bSDimitry Andric - (SubRegMask & ~LanesLeft).getNumLanes();
6107a7e6055SDimitry Andric if (Cover > BestCover) {
6117a7e6055SDimitry Andric BestCover = Cover;
6127a7e6055SDimitry Andric BestIdx = Idx;
6137a7e6055SDimitry Andric }
6147a7e6055SDimitry Andric }
6157a7e6055SDimitry Andric
6167a7e6055SDimitry Andric if (BestIdx == 0)
6177a7e6055SDimitry Andric report_fatal_error("Impossible to implement partial COPY");
6187a7e6055SDimitry Andric
6197a7e6055SDimitry Andric buildSingleSubRegCopy(FromReg, ToReg, MBB, InsertBefore, BestIdx,
6207a7e6055SDimitry Andric DestLI, Late, Def);
6217a7e6055SDimitry Andric LanesLeft &= ~TRI.getSubRegIndexLaneMask(BestIdx);
6227a7e6055SDimitry Andric }
6237a7e6055SDimitry Andric
6247a7e6055SDimitry Andric return Def;
6257a7e6055SDimitry Andric }
6267a7e6055SDimitry Andric
defFromParent(unsigned RegIdx,VNInfo * ParentVNI,SlotIndex UseIdx,MachineBasicBlock & MBB,MachineBasicBlock::iterator I)6272754fe60SDimitry Andric VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
6282754fe60SDimitry Andric VNInfo *ParentVNI,
6292754fe60SDimitry Andric SlotIndex UseIdx,
630e580952dSDimitry Andric MachineBasicBlock &MBB,
631e580952dSDimitry Andric MachineBasicBlock::iterator I) {
6322754fe60SDimitry Andric SlotIndex Def;
633f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
6343b0f4066SDimitry Andric
6353b0f4066SDimitry Andric // We may be trying to avoid interference that ends at a deleted instruction,
6363b0f4066SDimitry Andric // so always begin RegIdx 0 early and all others late.
6373b0f4066SDimitry Andric bool Late = RegIdx != 0;
6382754fe60SDimitry Andric
6392754fe60SDimitry Andric // Attempt cheap-as-a-copy rematerialization.
6403ca95b02SDimitry Andric unsigned Original = VRM.getOriginal(Edit->get(RegIdx));
6413ca95b02SDimitry Andric LiveInterval &OrigLI = LIS.getInterval(Original);
6423ca95b02SDimitry Andric VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx);
643d88c1a5aSDimitry Andric
6447a7e6055SDimitry Andric unsigned Reg = LI->reg;
645d88c1a5aSDimitry Andric bool DidRemat = false;
646d88c1a5aSDimitry Andric if (OrigVNI) {
6472754fe60SDimitry Andric LiveRangeEdit::Remat RM(ParentVNI);
6483ca95b02SDimitry Andric RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def);
6493ca95b02SDimitry Andric if (Edit->canRematerializeAt(RM, OrigVNI, UseIdx, true)) {
6507a7e6055SDimitry Andric Def = Edit->rematerializeAt(MBB, I, Reg, RM, TRI, Late);
651bd5abe19SDimitry Andric ++NumRemats;
652d88c1a5aSDimitry Andric DidRemat = true;
653d88c1a5aSDimitry Andric }
654d88c1a5aSDimitry Andric }
655d88c1a5aSDimitry Andric if (!DidRemat) {
6567a7e6055SDimitry Andric LaneBitmask LaneMask;
6577a7e6055SDimitry Andric if (LI->hasSubRanges()) {
6587a7e6055SDimitry Andric LaneMask = LaneBitmask::getNone();
6597a7e6055SDimitry Andric for (LiveInterval::SubRange &S : LI->subranges())
6607a7e6055SDimitry Andric LaneMask |= S.LaneMask;
6617a7e6055SDimitry Andric } else {
6627a7e6055SDimitry Andric LaneMask = LaneBitmask::getAll();
6637a7e6055SDimitry Andric }
6647a7e6055SDimitry Andric
665bd5abe19SDimitry Andric ++NumCopies;
6667a7e6055SDimitry Andric Def = buildCopy(Edit->getReg(), Reg, LaneMask, MBB, I, Late, RegIdx);
6672754fe60SDimitry Andric }
6682754fe60SDimitry Andric
6692754fe60SDimitry Andric // Define the value in Reg.
670d88c1a5aSDimitry Andric return defValue(RegIdx, ParentVNI, Def, false);
671e580952dSDimitry Andric }
672e580952dSDimitry Andric
673e580952dSDimitry Andric /// Create a new virtual register and live interval.
openIntv()6743b0f4066SDimitry Andric unsigned SplitEditor::openIntv() {
6752754fe60SDimitry Andric // Create the complement as index 0.
6763b0f4066SDimitry Andric if (Edit->empty())
677f785676fSDimitry Andric Edit->createEmptyInterval();
678e580952dSDimitry Andric
6792754fe60SDimitry Andric // Create the open interval.
6803b0f4066SDimitry Andric OpenIdx = Edit->size();
681f785676fSDimitry Andric Edit->createEmptyInterval();
6823b0f4066SDimitry Andric return OpenIdx;
6833b0f4066SDimitry Andric }
6843b0f4066SDimitry Andric
selectIntv(unsigned Idx)6853b0f4066SDimitry Andric void SplitEditor::selectIntv(unsigned Idx) {
6863b0f4066SDimitry Andric assert(Idx != 0 && "Cannot select the complement interval");
6873b0f4066SDimitry Andric assert(Idx < Edit->size() && "Can only select previously opened interval");
688*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n');
6893b0f4066SDimitry Andric OpenIdx = Idx;
6902754fe60SDimitry Andric }
691e580952dSDimitry Andric
enterIntvBefore(SlotIndex Idx)6922754fe60SDimitry Andric SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
6932754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before enterIntvBefore");
694*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " enterIntvBefore " << Idx);
6952754fe60SDimitry Andric Idx = Idx.getBaseIndex();
6963b0f4066SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
6972754fe60SDimitry Andric if (!ParentVNI) {
698*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ": not live\n");
6992754fe60SDimitry Andric return Idx;
7002754fe60SDimitry Andric }
701*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
7022754fe60SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
703e580952dSDimitry Andric assert(MI && "enterIntvBefore called with invalid index");
704e580952dSDimitry Andric
7052754fe60SDimitry Andric VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
7062754fe60SDimitry Andric return VNI->def;
707e580952dSDimitry Andric }
708e580952dSDimitry Andric
enterIntvAfter(SlotIndex Idx)70917a519f9SDimitry Andric SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
71017a519f9SDimitry Andric assert(OpenIdx && "openIntv not called before enterIntvAfter");
711*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " enterIntvAfter " << Idx);
71217a519f9SDimitry Andric Idx = Idx.getBoundaryIndex();
71317a519f9SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
71417a519f9SDimitry Andric if (!ParentVNI) {
715*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ": not live\n");
71617a519f9SDimitry Andric return Idx;
71717a519f9SDimitry Andric }
718*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
71917a519f9SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
72017a519f9SDimitry Andric assert(MI && "enterIntvAfter called with invalid index");
72117a519f9SDimitry Andric
72217a519f9SDimitry Andric VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
72391bc56edSDimitry Andric std::next(MachineBasicBlock::iterator(MI)));
72417a519f9SDimitry Andric return VNI->def;
72517a519f9SDimitry Andric }
72617a519f9SDimitry Andric
enterIntvAtEnd(MachineBasicBlock & MBB)7272754fe60SDimitry Andric SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
7282754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
7292754fe60SDimitry Andric SlotIndex End = LIS.getMBBEndIdx(&MBB);
7302754fe60SDimitry Andric SlotIndex Last = End.getPrevSlot();
731*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " enterIntvAtEnd " << printMBBReference(MBB) << ", "
7322cab237bSDimitry Andric << Last);
7333b0f4066SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
7342754fe60SDimitry Andric if (!ParentVNI) {
735*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ": not live\n");
7362754fe60SDimitry Andric return End;
7372754fe60SDimitry Andric }
738*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id);
7392754fe60SDimitry Andric VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
740dff0c46cSDimitry Andric SA.getLastSplitPointIter(&MBB));
7412754fe60SDimitry Andric RegAssign.insert(VNI->def, End, OpenIdx);
742*4ba319b5SDimitry Andric LLVM_DEBUG(dump());
7432754fe60SDimitry Andric return VNI->def;
744e580952dSDimitry Andric }
745e580952dSDimitry Andric
7462754fe60SDimitry Andric /// useIntv - indicate that all instructions in MBB should use OpenLI.
useIntv(const MachineBasicBlock & MBB)747e580952dSDimitry Andric void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
7482754fe60SDimitry Andric useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
749e580952dSDimitry Andric }
750e580952dSDimitry Andric
useIntv(SlotIndex Start,SlotIndex End)751e580952dSDimitry Andric void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
7522754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before useIntv");
753*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
7542754fe60SDimitry Andric RegAssign.insert(Start, End, OpenIdx);
755*4ba319b5SDimitry Andric LLVM_DEBUG(dump());
756e580952dSDimitry Andric }
757e580952dSDimitry Andric
leaveIntvAfter(SlotIndex Idx)7582754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
7592754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before leaveIntvAfter");
760*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " leaveIntvAfter " << Idx);
7612754fe60SDimitry Andric
7622754fe60SDimitry Andric // The interval must be live beyond the instruction at Idx.
7636122f3e6SDimitry Andric SlotIndex Boundary = Idx.getBoundaryIndex();
7646122f3e6SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
7652754fe60SDimitry Andric if (!ParentVNI) {
766*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ": not live\n");
7676122f3e6SDimitry Andric return Boundary.getNextSlot();
7682754fe60SDimitry Andric }
769*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
7706122f3e6SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
7712754fe60SDimitry Andric assert(MI && "No instruction at index");
7726122f3e6SDimitry Andric
7736122f3e6SDimitry Andric // In spill mode, make live ranges as short as possible by inserting the copy
7746122f3e6SDimitry Andric // before MI. This is only possible if that instruction doesn't redefine the
7756122f3e6SDimitry Andric // value. The inserted COPY is not a kill, and we don't need to recompute
7766122f3e6SDimitry Andric // the source live range. The spiller also won't try to hoist this copy.
7776122f3e6SDimitry Andric if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
7786122f3e6SDimitry Andric MI->readsVirtualRegister(Edit->getReg())) {
779954b921dSDimitry Andric forceRecompute(0, *ParentVNI);
7806122f3e6SDimitry Andric defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
7816122f3e6SDimitry Andric return Idx;
7826122f3e6SDimitry Andric }
7836122f3e6SDimitry Andric
7846122f3e6SDimitry Andric VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
78591bc56edSDimitry Andric std::next(MachineBasicBlock::iterator(MI)));
7862754fe60SDimitry Andric return VNI->def;
787e580952dSDimitry Andric }
788e580952dSDimitry Andric
leaveIntvBefore(SlotIndex Idx)7892754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
7902754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before leaveIntvBefore");
791*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " leaveIntvBefore " << Idx);
792e580952dSDimitry Andric
7932754fe60SDimitry Andric // The interval must be live into the instruction at Idx.
7946122f3e6SDimitry Andric Idx = Idx.getBaseIndex();
7953b0f4066SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
7962754fe60SDimitry Andric if (!ParentVNI) {
797*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ": not live\n");
7982754fe60SDimitry Andric return Idx.getNextSlot();
7992754fe60SDimitry Andric }
800*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
8012754fe60SDimitry Andric
8022754fe60SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
8032754fe60SDimitry Andric assert(MI && "No instruction at index");
8042754fe60SDimitry Andric VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
8052754fe60SDimitry Andric return VNI->def;
806e580952dSDimitry Andric }
807e580952dSDimitry Andric
leaveIntvAtTop(MachineBasicBlock & MBB)8082754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
8092754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
8102754fe60SDimitry Andric SlotIndex Start = LIS.getMBBStartIdx(&MBB);
811*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " leaveIntvAtTop " << printMBBReference(MBB) << ", "
8122cab237bSDimitry Andric << Start);
8132754fe60SDimitry Andric
8143b0f4066SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
8152754fe60SDimitry Andric if (!ParentVNI) {
816*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ": not live\n");
8172754fe60SDimitry Andric return Start;
818e580952dSDimitry Andric }
819e580952dSDimitry Andric
8202754fe60SDimitry Andric VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
821d88c1a5aSDimitry Andric MBB.SkipPHIsLabelsAndDebug(MBB.begin()));
8222754fe60SDimitry Andric RegAssign.insert(Start, VNI->def, OpenIdx);
823*4ba319b5SDimitry Andric LLVM_DEBUG(dump());
8242754fe60SDimitry Andric return VNI->def;
825e580952dSDimitry Andric }
826e580952dSDimitry Andric
overlapIntv(SlotIndex Start,SlotIndex End)8272754fe60SDimitry Andric void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
8282754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before overlapIntv");
8293b0f4066SDimitry Andric const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
830dff0c46cSDimitry Andric assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
8312754fe60SDimitry Andric "Parent changes value in extended range");
8322754fe60SDimitry Andric assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
8332754fe60SDimitry Andric "Range cannot span basic blocks");
834e580952dSDimitry Andric
8356122f3e6SDimitry Andric // The complement interval will be extended as needed by LRCalc.extend().
8363b0f4066SDimitry Andric if (ParentVNI)
837954b921dSDimitry Andric forceRecompute(0, *ParentVNI);
838*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
8392754fe60SDimitry Andric RegAssign.insert(Start, End, OpenIdx);
840*4ba319b5SDimitry Andric LLVM_DEBUG(dump());
841e580952dSDimitry Andric }
842e580952dSDimitry Andric
8436122f3e6SDimitry Andric //===----------------------------------------------------------------------===//
8446122f3e6SDimitry Andric // Spill modes
8456122f3e6SDimitry Andric //===----------------------------------------------------------------------===//
8466122f3e6SDimitry Andric
removeBackCopies(SmallVectorImpl<VNInfo * > & Copies)8476122f3e6SDimitry Andric void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
848f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(0));
849*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
8506122f3e6SDimitry Andric RegAssignMap::iterator AssignI;
8516122f3e6SDimitry Andric AssignI.setMap(RegAssign);
8526122f3e6SDimitry Andric
8536122f3e6SDimitry Andric for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
854ff0cc061SDimitry Andric SlotIndex Def = Copies[i]->def;
8556122f3e6SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Def);
8566122f3e6SDimitry Andric assert(MI && "No instruction for back-copy");
8576122f3e6SDimitry Andric
8586122f3e6SDimitry Andric MachineBasicBlock *MBB = MI->getParent();
8596122f3e6SDimitry Andric MachineBasicBlock::iterator MBBI(MI);
8606122f3e6SDimitry Andric bool AtBegin;
8616122f3e6SDimitry Andric do AtBegin = MBBI == MBB->begin();
862*4ba319b5SDimitry Andric while (!AtBegin && (--MBBI)->isDebugInstr());
8636122f3e6SDimitry Andric
864*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
865ff0cc061SDimitry Andric LIS.removeVRegDefAt(*LI, Def);
8663ca95b02SDimitry Andric LIS.RemoveMachineInstrFromMaps(*MI);
8676122f3e6SDimitry Andric MI->eraseFromParent();
8686122f3e6SDimitry Andric
869ff0cc061SDimitry Andric // Adjust RegAssign if a register assignment is killed at Def. We want to
870ff0cc061SDimitry Andric // avoid calculating the live range of the source register if possible.
8717ae0e2c9SDimitry Andric AssignI.find(Def.getPrevSlot());
8726122f3e6SDimitry Andric if (!AssignI.valid() || AssignI.start() >= Def)
8736122f3e6SDimitry Andric continue;
8746122f3e6SDimitry Andric // If MI doesn't kill the assigned register, just leave it.
8756122f3e6SDimitry Andric if (AssignI.stop() != Def)
8766122f3e6SDimitry Andric continue;
8776122f3e6SDimitry Andric unsigned RegIdx = AssignI.value();
8786122f3e6SDimitry Andric if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) {
879*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx
880*4ba319b5SDimitry Andric << '\n');
881954b921dSDimitry Andric forceRecompute(RegIdx, *Edit->getParent().getVNInfoAt(Def));
8826122f3e6SDimitry Andric } else {
8833ca95b02SDimitry Andric SlotIndex Kill = LIS.getInstructionIndex(*MBBI).getRegSlot();
884*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI);
8856122f3e6SDimitry Andric AssignI.setStop(Kill);
8866122f3e6SDimitry Andric }
8876122f3e6SDimitry Andric }
8886122f3e6SDimitry Andric }
8896122f3e6SDimitry Andric
8906122f3e6SDimitry Andric MachineBasicBlock*
findShallowDominator(MachineBasicBlock * MBB,MachineBasicBlock * DefMBB)8916122f3e6SDimitry Andric SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
8926122f3e6SDimitry Andric MachineBasicBlock *DefMBB) {
8936122f3e6SDimitry Andric if (MBB == DefMBB)
8946122f3e6SDimitry Andric return MBB;
8956122f3e6SDimitry Andric assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
8966122f3e6SDimitry Andric
8976122f3e6SDimitry Andric const MachineLoopInfo &Loops = SA.Loops;
8986122f3e6SDimitry Andric const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
8996122f3e6SDimitry Andric MachineDomTreeNode *DefDomNode = MDT[DefMBB];
9006122f3e6SDimitry Andric
9016122f3e6SDimitry Andric // Best candidate so far.
9026122f3e6SDimitry Andric MachineBasicBlock *BestMBB = MBB;
9032cab237bSDimitry Andric unsigned BestDepth = std::numeric_limits<unsigned>::max();
9046122f3e6SDimitry Andric
9052cab237bSDimitry Andric while (true) {
9066122f3e6SDimitry Andric const MachineLoop *Loop = Loops.getLoopFor(MBB);
9076122f3e6SDimitry Andric
9086122f3e6SDimitry Andric // MBB isn't in a loop, it doesn't get any better. All dominators have a
9096122f3e6SDimitry Andric // higher frequency by definition.
9106122f3e6SDimitry Andric if (!Loop) {
911*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
912*4ba319b5SDimitry Andric << " dominates " << printMBBReference(*MBB)
913*4ba319b5SDimitry Andric << " at depth 0\n");
9146122f3e6SDimitry Andric return MBB;
9156122f3e6SDimitry Andric }
9166122f3e6SDimitry Andric
9176122f3e6SDimitry Andric // We'll never be able to exit the DefLoop.
9186122f3e6SDimitry Andric if (Loop == DefLoop) {
919*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
920*4ba319b5SDimitry Andric << " dominates " << printMBBReference(*MBB)
921*4ba319b5SDimitry Andric << " in the same loop\n");
9226122f3e6SDimitry Andric return MBB;
9236122f3e6SDimitry Andric }
9246122f3e6SDimitry Andric
9256122f3e6SDimitry Andric // Least busy dominator seen so far.
9266122f3e6SDimitry Andric unsigned Depth = Loop->getLoopDepth();
9276122f3e6SDimitry Andric if (Depth < BestDepth) {
9286122f3e6SDimitry Andric BestMBB = MBB;
9296122f3e6SDimitry Andric BestDepth = Depth;
930*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
931*4ba319b5SDimitry Andric << " dominates " << printMBBReference(*MBB)
932*4ba319b5SDimitry Andric << " at depth " << Depth << '\n');
9336122f3e6SDimitry Andric }
9346122f3e6SDimitry Andric
9356122f3e6SDimitry Andric // Leave loop by going to the immediate dominator of the loop header.
9366122f3e6SDimitry Andric // This is a bigger stride than simply walking up the dominator tree.
9376122f3e6SDimitry Andric MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
9386122f3e6SDimitry Andric
9396122f3e6SDimitry Andric // Too far up the dominator tree?
9406122f3e6SDimitry Andric if (!IDom || !MDT.dominates(DefDomNode, IDom))
9416122f3e6SDimitry Andric return BestMBB;
9426122f3e6SDimitry Andric
9436122f3e6SDimitry Andric MBB = IDom->getBlock();
9446122f3e6SDimitry Andric }
9456122f3e6SDimitry Andric }
9466122f3e6SDimitry Andric
computeRedundantBackCopies(DenseSet<unsigned> & NotToHoistSet,SmallVectorImpl<VNInfo * > & BackCopies)9473ca95b02SDimitry Andric void SplitEditor::computeRedundantBackCopies(
9483ca95b02SDimitry Andric DenseSet<unsigned> &NotToHoistSet, SmallVectorImpl<VNInfo *> &BackCopies) {
9493ca95b02SDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(0));
9503ca95b02SDimitry Andric LiveInterval *Parent = &Edit->getParent();
9513ca95b02SDimitry Andric SmallVector<SmallPtrSet<VNInfo *, 8>, 8> EqualVNs(Parent->getNumValNums());
9523ca95b02SDimitry Andric SmallPtrSet<VNInfo *, 8> DominatedVNIs;
9533ca95b02SDimitry Andric
9543ca95b02SDimitry Andric // Aggregate VNIs having the same value as ParentVNI.
9553ca95b02SDimitry Andric for (VNInfo *VNI : LI->valnos) {
9563ca95b02SDimitry Andric if (VNI->isUnused())
9573ca95b02SDimitry Andric continue;
9583ca95b02SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
9593ca95b02SDimitry Andric EqualVNs[ParentVNI->id].insert(VNI);
9603ca95b02SDimitry Andric }
9613ca95b02SDimitry Andric
9623ca95b02SDimitry Andric // For VNI aggregation of each ParentVNI, collect dominated, i.e.,
9633ca95b02SDimitry Andric // redundant VNIs to BackCopies.
9643ca95b02SDimitry Andric for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
9653ca95b02SDimitry Andric VNInfo *ParentVNI = Parent->getValNumInfo(i);
9663ca95b02SDimitry Andric if (!NotToHoistSet.count(ParentVNI->id))
9673ca95b02SDimitry Andric continue;
9683ca95b02SDimitry Andric SmallPtrSetIterator<VNInfo *> It1 = EqualVNs[ParentVNI->id].begin();
9693ca95b02SDimitry Andric SmallPtrSetIterator<VNInfo *> It2 = It1;
9703ca95b02SDimitry Andric for (; It1 != EqualVNs[ParentVNI->id].end(); ++It1) {
9713ca95b02SDimitry Andric It2 = It1;
9723ca95b02SDimitry Andric for (++It2; It2 != EqualVNs[ParentVNI->id].end(); ++It2) {
9733ca95b02SDimitry Andric if (DominatedVNIs.count(*It1) || DominatedVNIs.count(*It2))
9743ca95b02SDimitry Andric continue;
9753ca95b02SDimitry Andric
9763ca95b02SDimitry Andric MachineBasicBlock *MBB1 = LIS.getMBBFromIndex((*It1)->def);
9773ca95b02SDimitry Andric MachineBasicBlock *MBB2 = LIS.getMBBFromIndex((*It2)->def);
9783ca95b02SDimitry Andric if (MBB1 == MBB2) {
9793ca95b02SDimitry Andric DominatedVNIs.insert((*It1)->def < (*It2)->def ? (*It2) : (*It1));
9803ca95b02SDimitry Andric } else if (MDT.dominates(MBB1, MBB2)) {
9813ca95b02SDimitry Andric DominatedVNIs.insert(*It2);
9823ca95b02SDimitry Andric } else if (MDT.dominates(MBB2, MBB1)) {
9833ca95b02SDimitry Andric DominatedVNIs.insert(*It1);
9843ca95b02SDimitry Andric }
9853ca95b02SDimitry Andric }
9863ca95b02SDimitry Andric }
9873ca95b02SDimitry Andric if (!DominatedVNIs.empty()) {
988954b921dSDimitry Andric forceRecompute(0, *ParentVNI);
9893ca95b02SDimitry Andric for (auto VNI : DominatedVNIs) {
9903ca95b02SDimitry Andric BackCopies.push_back(VNI);
9913ca95b02SDimitry Andric }
9923ca95b02SDimitry Andric DominatedVNIs.clear();
9933ca95b02SDimitry Andric }
9943ca95b02SDimitry Andric }
9953ca95b02SDimitry Andric }
9963ca95b02SDimitry Andric
9973ca95b02SDimitry Andric /// For SM_Size mode, find a common dominator for all the back-copies for
9983ca95b02SDimitry Andric /// the same ParentVNI and hoist the backcopies to the dominator BB.
9993ca95b02SDimitry Andric /// For SM_Speed mode, if the common dominator is hot and it is not beneficial
10003ca95b02SDimitry Andric /// to do the hoisting, simply remove the dominated backcopies for the same
10013ca95b02SDimitry Andric /// ParentVNI.
hoistCopies()10023ca95b02SDimitry Andric void SplitEditor::hoistCopies() {
10036122f3e6SDimitry Andric // Get the complement interval, always RegIdx 0.
1004f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(0));
10056122f3e6SDimitry Andric LiveInterval *Parent = &Edit->getParent();
10066122f3e6SDimitry Andric
10076122f3e6SDimitry Andric // Track the nearest common dominator for all back-copies for each ParentVNI,
10086122f3e6SDimitry Andric // indexed by ParentVNI->id.
10092cab237bSDimitry Andric using DomPair = std::pair<MachineBasicBlock *, SlotIndex>;
10106122f3e6SDimitry Andric SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
10113ca95b02SDimitry Andric // The total cost of all the back-copies for each ParentVNI.
10123ca95b02SDimitry Andric SmallVector<BlockFrequency, 8> Costs(Parent->getNumValNums());
10133ca95b02SDimitry Andric // The ParentVNI->id set for which hoisting back-copies are not beneficial
10143ca95b02SDimitry Andric // for Speed.
10153ca95b02SDimitry Andric DenseSet<unsigned> NotToHoistSet;
10166122f3e6SDimitry Andric
10176122f3e6SDimitry Andric // Find the nearest common dominator for parent values with multiple
10186122f3e6SDimitry Andric // back-copies. If a single back-copy dominates, put it in DomPair.second.
101939d628a0SDimitry Andric for (VNInfo *VNI : LI->valnos) {
10207ae0e2c9SDimitry Andric if (VNI->isUnused())
10217ae0e2c9SDimitry Andric continue;
10226122f3e6SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
10236122f3e6SDimitry Andric assert(ParentVNI && "Parent not live at complement def");
10246122f3e6SDimitry Andric
10256122f3e6SDimitry Andric // Don't hoist remats. The complement is probably going to disappear
10266122f3e6SDimitry Andric // completely anyway.
10276122f3e6SDimitry Andric if (Edit->didRematerialize(ParentVNI))
10286122f3e6SDimitry Andric continue;
10296122f3e6SDimitry Andric
10306122f3e6SDimitry Andric MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
10313ca95b02SDimitry Andric
10326122f3e6SDimitry Andric DomPair &Dom = NearestDom[ParentVNI->id];
10336122f3e6SDimitry Andric
10346122f3e6SDimitry Andric // Keep directly defined parent values. This is either a PHI or an
10356122f3e6SDimitry Andric // instruction in the complement range. All other copies of ParentVNI
10366122f3e6SDimitry Andric // should be eliminated.
10376122f3e6SDimitry Andric if (VNI->def == ParentVNI->def) {
1038*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
10396122f3e6SDimitry Andric Dom = DomPair(ValMBB, VNI->def);
10406122f3e6SDimitry Andric continue;
10416122f3e6SDimitry Andric }
10426122f3e6SDimitry Andric // Skip the singly mapped values. There is nothing to gain from hoisting a
10436122f3e6SDimitry Andric // single back-copy.
10446122f3e6SDimitry Andric if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
1045*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
10466122f3e6SDimitry Andric continue;
10476122f3e6SDimitry Andric }
10486122f3e6SDimitry Andric
10496122f3e6SDimitry Andric if (!Dom.first) {
10506122f3e6SDimitry Andric // First time we see ParentVNI. VNI dominates itself.
10516122f3e6SDimitry Andric Dom = DomPair(ValMBB, VNI->def);
10526122f3e6SDimitry Andric } else if (Dom.first == ValMBB) {
10536122f3e6SDimitry Andric // Two defs in the same block. Pick the earlier def.
10546122f3e6SDimitry Andric if (!Dom.second.isValid() || VNI->def < Dom.second)
10556122f3e6SDimitry Andric Dom.second = VNI->def;
10566122f3e6SDimitry Andric } else {
10576122f3e6SDimitry Andric // Different basic blocks. Check if one dominates.
10586122f3e6SDimitry Andric MachineBasicBlock *Near =
10596122f3e6SDimitry Andric MDT.findNearestCommonDominator(Dom.first, ValMBB);
10606122f3e6SDimitry Andric if (Near == ValMBB)
10616122f3e6SDimitry Andric // Def ValMBB dominates.
10626122f3e6SDimitry Andric Dom = DomPair(ValMBB, VNI->def);
10636122f3e6SDimitry Andric else if (Near != Dom.first)
10646122f3e6SDimitry Andric // None dominate. Hoist to common dominator, need new def.
10656122f3e6SDimitry Andric Dom = DomPair(Near, SlotIndex());
10663ca95b02SDimitry Andric Costs[ParentVNI->id] += MBFI.getBlockFreq(ValMBB);
10676122f3e6SDimitry Andric }
10686122f3e6SDimitry Andric
1069*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@'
1070*4ba319b5SDimitry Andric << VNI->def << " for parent " << ParentVNI->id << '@'
1071*4ba319b5SDimitry Andric << ParentVNI->def << " hoist to "
1072*4ba319b5SDimitry Andric << printMBBReference(*Dom.first) << ' ' << Dom.second
1073*4ba319b5SDimitry Andric << '\n');
10746122f3e6SDimitry Andric }
10756122f3e6SDimitry Andric
10766122f3e6SDimitry Andric // Insert the hoisted copies.
10776122f3e6SDimitry Andric for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
10786122f3e6SDimitry Andric DomPair &Dom = NearestDom[i];
10796122f3e6SDimitry Andric if (!Dom.first || Dom.second.isValid())
10806122f3e6SDimitry Andric continue;
10816122f3e6SDimitry Andric // This value needs a hoisted copy inserted at the end of Dom.first.
10826122f3e6SDimitry Andric VNInfo *ParentVNI = Parent->getValNumInfo(i);
10836122f3e6SDimitry Andric MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
10846122f3e6SDimitry Andric // Get a less loopy dominator than Dom.first.
10856122f3e6SDimitry Andric Dom.first = findShallowDominator(Dom.first, DefMBB);
10863ca95b02SDimitry Andric if (SpillMode == SM_Speed &&
10873ca95b02SDimitry Andric MBFI.getBlockFreq(Dom.first) > Costs[ParentVNI->id]) {
10883ca95b02SDimitry Andric NotToHoistSet.insert(ParentVNI->id);
10893ca95b02SDimitry Andric continue;
10903ca95b02SDimitry Andric }
10916122f3e6SDimitry Andric SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot();
10926122f3e6SDimitry Andric Dom.second =
10936122f3e6SDimitry Andric defFromParent(0, ParentVNI, Last, *Dom.first,
1094dff0c46cSDimitry Andric SA.getLastSplitPointIter(Dom.first))->def;
10956122f3e6SDimitry Andric }
10966122f3e6SDimitry Andric
10976122f3e6SDimitry Andric // Remove redundant back-copies that are now known to be dominated by another
10986122f3e6SDimitry Andric // def with the same value.
10996122f3e6SDimitry Andric SmallVector<VNInfo*, 8> BackCopies;
110039d628a0SDimitry Andric for (VNInfo *VNI : LI->valnos) {
11017ae0e2c9SDimitry Andric if (VNI->isUnused())
11027ae0e2c9SDimitry Andric continue;
11036122f3e6SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
11046122f3e6SDimitry Andric const DomPair &Dom = NearestDom[ParentVNI->id];
11053ca95b02SDimitry Andric if (!Dom.first || Dom.second == VNI->def ||
11063ca95b02SDimitry Andric NotToHoistSet.count(ParentVNI->id))
11076122f3e6SDimitry Andric continue;
11086122f3e6SDimitry Andric BackCopies.push_back(VNI);
1109954b921dSDimitry Andric forceRecompute(0, *ParentVNI);
11106122f3e6SDimitry Andric }
11113ca95b02SDimitry Andric
11123ca95b02SDimitry Andric // If it is not beneficial to hoist all the BackCopies, simply remove
11133ca95b02SDimitry Andric // redundant BackCopies in speed mode.
11143ca95b02SDimitry Andric if (SpillMode == SM_Speed && !NotToHoistSet.empty())
11153ca95b02SDimitry Andric computeRedundantBackCopies(NotToHoistSet, BackCopies);
11163ca95b02SDimitry Andric
11176122f3e6SDimitry Andric removeBackCopies(BackCopies);
11186122f3e6SDimitry Andric }
11196122f3e6SDimitry Andric
11203b0f4066SDimitry Andric /// transferValues - Transfer all possible values to the new live ranges.
11216122f3e6SDimitry Andric /// Values that were rematerialized are left alone, they need LRCalc.extend().
transferValues()11223b0f4066SDimitry Andric bool SplitEditor::transferValues() {
11233b0f4066SDimitry Andric bool Skipped = false;
11243b0f4066SDimitry Andric RegAssignMap::const_iterator AssignI = RegAssign.begin();
112539d628a0SDimitry Andric for (const LiveRange::Segment &S : Edit->getParent()) {
1126*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " blit " << S << ':');
112739d628a0SDimitry Andric VNInfo *ParentVNI = S.valno;
11283b0f4066SDimitry Andric // RegAssign has holes where RegIdx 0 should be used.
112939d628a0SDimitry Andric SlotIndex Start = S.start;
11303b0f4066SDimitry Andric AssignI.advanceTo(Start);
11313b0f4066SDimitry Andric do {
11323b0f4066SDimitry Andric unsigned RegIdx;
113339d628a0SDimitry Andric SlotIndex End = S.end;
11343b0f4066SDimitry Andric if (!AssignI.valid()) {
11353b0f4066SDimitry Andric RegIdx = 0;
11363b0f4066SDimitry Andric } else if (AssignI.start() <= Start) {
11373b0f4066SDimitry Andric RegIdx = AssignI.value();
11383b0f4066SDimitry Andric if (AssignI.stop() < End) {
11393b0f4066SDimitry Andric End = AssignI.stop();
11403b0f4066SDimitry Andric ++AssignI;
11413b0f4066SDimitry Andric }
11423b0f4066SDimitry Andric } else {
11433b0f4066SDimitry Andric RegIdx = 0;
11443b0f4066SDimitry Andric End = std::min(End, AssignI.start());
1145e580952dSDimitry Andric }
1146e580952dSDimitry Andric
11473b0f4066SDimitry Andric // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
1148*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx << '('
1149*4ba319b5SDimitry Andric << printReg(Edit->get(RegIdx)) << ')');
1150d88c1a5aSDimitry Andric LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
11513b0f4066SDimitry Andric
11523b0f4066SDimitry Andric // Check for a simply defined value that can be blitted directly.
11536122f3e6SDimitry Andric ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
11546122f3e6SDimitry Andric if (VNInfo *VNI = VFP.getPointer()) {
1155*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ':' << VNI->id);
1156d88c1a5aSDimitry Andric LI.addSegment(LiveInterval::Segment(Start, End, VNI));
11573b0f4066SDimitry Andric Start = End;
11583b0f4066SDimitry Andric continue;
11593b0f4066SDimitry Andric }
11603b0f4066SDimitry Andric
11616122f3e6SDimitry Andric // Skip values with forced recomputation.
11626122f3e6SDimitry Andric if (VFP.getInt()) {
1163*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "(recalc)");
11643b0f4066SDimitry Andric Skipped = true;
11653b0f4066SDimitry Andric Start = End;
11663b0f4066SDimitry Andric continue;
11673b0f4066SDimitry Andric }
11683b0f4066SDimitry Andric
11696122f3e6SDimitry Andric LiveRangeCalc &LRC = getLRCalc(RegIdx);
11703b0f4066SDimitry Andric
11713b0f4066SDimitry Andric // This value has multiple defs in RegIdx, but it wasn't rematerialized,
11723b0f4066SDimitry Andric // so the live range is accurate. Add live-in blocks in [Start;End) to the
11733b0f4066SDimitry Andric // LiveInBlocks.
11747d523365SDimitry Andric MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
11753b0f4066SDimitry Andric SlotIndex BlockStart, BlockEnd;
11767d523365SDimitry Andric std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(&*MBB);
11773b0f4066SDimitry Andric
11783b0f4066SDimitry Andric // The first block may be live-in, or it may have its own def.
11793b0f4066SDimitry Andric if (Start != BlockStart) {
1180d88c1a5aSDimitry Andric VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
11813b0f4066SDimitry Andric assert(VNI && "Missing def for complex mapped value");
1182*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ':' << VNI->id << "*" << printMBBReference(*MBB));
11833b0f4066SDimitry Andric // MBB has its own def. Is it also live-out?
11846122f3e6SDimitry Andric if (BlockEnd <= End)
11857d523365SDimitry Andric LRC.setLiveOutValue(&*MBB, VNI);
11866122f3e6SDimitry Andric
11873b0f4066SDimitry Andric // Skip to the next block for live-in.
11883b0f4066SDimitry Andric ++MBB;
11893b0f4066SDimitry Andric BlockStart = BlockEnd;
11903b0f4066SDimitry Andric }
11913b0f4066SDimitry Andric
11923b0f4066SDimitry Andric // Handle the live-in blocks covered by [Start;End).
11933b0f4066SDimitry Andric assert(Start <= BlockStart && "Expected live-in block");
11943b0f4066SDimitry Andric while (BlockStart < End) {
1195*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ">" << printMBBReference(*MBB));
11967d523365SDimitry Andric BlockEnd = LIS.getMBBEndIdx(&*MBB);
11973b0f4066SDimitry Andric if (BlockStart == ParentVNI->def) {
11983b0f4066SDimitry Andric // This block has the def of a parent PHI, so it isn't live-in.
11993b0f4066SDimitry Andric assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
1200d88c1a5aSDimitry Andric VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
12013b0f4066SDimitry Andric assert(VNI && "Missing def for complex mapped parent PHI");
12026122f3e6SDimitry Andric if (End >= BlockEnd)
12037d523365SDimitry Andric LRC.setLiveOutValue(&*MBB, VNI); // Live-out as well.
12043b0f4066SDimitry Andric } else {
12056122f3e6SDimitry Andric // This block needs a live-in value. The last block covered may not
12066122f3e6SDimitry Andric // be live-out.
12073b0f4066SDimitry Andric if (End < BlockEnd)
1208d88c1a5aSDimitry Andric LRC.addLiveInBlock(LI, MDT[&*MBB], End);
12093b0f4066SDimitry Andric else {
12106122f3e6SDimitry Andric // Live-through, and we don't know the value.
1211d88c1a5aSDimitry Andric LRC.addLiveInBlock(LI, MDT[&*MBB]);
12127d523365SDimitry Andric LRC.setLiveOutValue(&*MBB, nullptr);
12133b0f4066SDimitry Andric }
12143b0f4066SDimitry Andric }
12153b0f4066SDimitry Andric BlockStart = BlockEnd;
12163b0f4066SDimitry Andric ++MBB;
12173b0f4066SDimitry Andric }
12183b0f4066SDimitry Andric Start = End;
121939d628a0SDimitry Andric } while (Start != S.end);
1220*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << '\n');
12213b0f4066SDimitry Andric }
12223b0f4066SDimitry Andric
12237ae0e2c9SDimitry Andric LRCalc[0].calculateValues();
12246122f3e6SDimitry Andric if (SpillMode)
12257ae0e2c9SDimitry Andric LRCalc[1].calculateValues();
12263b0f4066SDimitry Andric
12273b0f4066SDimitry Andric return Skipped;
12283b0f4066SDimitry Andric }
12293b0f4066SDimitry Andric
removeDeadSegment(SlotIndex Def,LiveRange & LR)1230d88c1a5aSDimitry Andric static bool removeDeadSegment(SlotIndex Def, LiveRange &LR) {
1231d88c1a5aSDimitry Andric const LiveRange::Segment *Seg = LR.getSegmentContaining(Def);
1232d88c1a5aSDimitry Andric if (Seg == nullptr)
1233d88c1a5aSDimitry Andric return true;
1234d88c1a5aSDimitry Andric if (Seg->end != Def.getDeadSlot())
1235d88c1a5aSDimitry Andric return false;
12363ca95b02SDimitry Andric // This is a dead PHI. Remove it.
1237d88c1a5aSDimitry Andric LR.removeSegment(*Seg, true);
1238d88c1a5aSDimitry Andric return true;
12393ca95b02SDimitry Andric }
12403ca95b02SDimitry Andric
extendPHIRange(MachineBasicBlock & B,LiveRangeCalc & LRC,LiveRange & LR,LaneBitmask LM,ArrayRef<SlotIndex> Undefs)1241d88c1a5aSDimitry Andric void SplitEditor::extendPHIRange(MachineBasicBlock &B, LiveRangeCalc &LRC,
1242d88c1a5aSDimitry Andric LiveRange &LR, LaneBitmask LM,
1243d88c1a5aSDimitry Andric ArrayRef<SlotIndex> Undefs) {
1244d88c1a5aSDimitry Andric for (MachineBasicBlock *P : B.predecessors()) {
1245d88c1a5aSDimitry Andric SlotIndex End = LIS.getMBBEndIdx(P);
12466122f3e6SDimitry Andric SlotIndex LastUse = End.getPrevSlot();
12473b0f4066SDimitry Andric // The predecessor may not have a live-out value. That is OK, like an
12483b0f4066SDimitry Andric // undef PHI operand.
1249d88c1a5aSDimitry Andric LiveInterval &PLI = Edit->getParent();
1250d88c1a5aSDimitry Andric // Need the cast because the inputs to ?: would otherwise be deemed
1251d88c1a5aSDimitry Andric // "incompatible": SubRange vs LiveInterval.
1252d88c1a5aSDimitry Andric LiveRange &PSR = !LM.all() ? getSubRangeForMask(LM, PLI)
1253d88c1a5aSDimitry Andric : static_cast<LiveRange&>(PLI);
1254d88c1a5aSDimitry Andric if (PSR.liveAt(LastUse))
1255d88c1a5aSDimitry Andric LRC.extend(LR, End, /*PhysReg=*/0, Undefs);
12563b0f4066SDimitry Andric }
12573b0f4066SDimitry Andric }
1258d88c1a5aSDimitry Andric
extendPHIKillRanges()1259d88c1a5aSDimitry Andric void SplitEditor::extendPHIKillRanges() {
1260d88c1a5aSDimitry Andric // Extend live ranges to be live-out for successor PHI values.
1261d88c1a5aSDimitry Andric
1262d88c1a5aSDimitry Andric // Visit each PHI def slot in the parent live interval. If the def is dead,
1263d88c1a5aSDimitry Andric // remove it. Otherwise, extend the live interval to reach the end indexes
1264d88c1a5aSDimitry Andric // of all predecessor blocks.
1265d88c1a5aSDimitry Andric
1266d88c1a5aSDimitry Andric LiveInterval &ParentLI = Edit->getParent();
1267d88c1a5aSDimitry Andric for (const VNInfo *V : ParentLI.valnos) {
1268d88c1a5aSDimitry Andric if (V->isUnused() || !V->isPHIDef())
1269d88c1a5aSDimitry Andric continue;
1270d88c1a5aSDimitry Andric
1271d88c1a5aSDimitry Andric unsigned RegIdx = RegAssign.lookup(V->def);
1272d88c1a5aSDimitry Andric LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1273d88c1a5aSDimitry Andric LiveRangeCalc &LRC = getLRCalc(RegIdx);
1274d88c1a5aSDimitry Andric MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
1275d88c1a5aSDimitry Andric if (!removeDeadSegment(V->def, LI))
1276d88c1a5aSDimitry Andric extendPHIRange(B, LRC, LI, LaneBitmask::getAll(), /*Undefs=*/{});
1277d88c1a5aSDimitry Andric }
1278d88c1a5aSDimitry Andric
1279d88c1a5aSDimitry Andric SmallVector<SlotIndex, 4> Undefs;
1280d88c1a5aSDimitry Andric LiveRangeCalc SubLRC;
1281d88c1a5aSDimitry Andric
1282d88c1a5aSDimitry Andric for (LiveInterval::SubRange &PS : ParentLI.subranges()) {
1283d88c1a5aSDimitry Andric for (const VNInfo *V : PS.valnos) {
1284d88c1a5aSDimitry Andric if (V->isUnused() || !V->isPHIDef())
1285d88c1a5aSDimitry Andric continue;
1286d88c1a5aSDimitry Andric unsigned RegIdx = RegAssign.lookup(V->def);
1287d88c1a5aSDimitry Andric LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1288d88c1a5aSDimitry Andric LiveInterval::SubRange &S = getSubRangeForMask(PS.LaneMask, LI);
1289d88c1a5aSDimitry Andric if (removeDeadSegment(V->def, S))
1290d88c1a5aSDimitry Andric continue;
1291d88c1a5aSDimitry Andric
1292d88c1a5aSDimitry Andric MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
1293d88c1a5aSDimitry Andric SubLRC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
1294d88c1a5aSDimitry Andric &LIS.getVNInfoAllocator());
1295d88c1a5aSDimitry Andric Undefs.clear();
1296d88c1a5aSDimitry Andric LI.computeSubRangeUndefs(Undefs, PS.LaneMask, MRI, *LIS.getSlotIndexes());
1297d88c1a5aSDimitry Andric extendPHIRange(B, SubLRC, S, PS.LaneMask, Undefs);
1298d88c1a5aSDimitry Andric }
12993b0f4066SDimitry Andric }
13003b0f4066SDimitry Andric }
13013b0f4066SDimitry Andric
13023b0f4066SDimitry Andric /// rewriteAssigned - Rewrite all uses of Edit->getReg().
rewriteAssigned(bool ExtendRanges)13033b0f4066SDimitry Andric void SplitEditor::rewriteAssigned(bool ExtendRanges) {
1304d88c1a5aSDimitry Andric struct ExtPoint {
1305d88c1a5aSDimitry Andric ExtPoint(const MachineOperand &O, unsigned R, SlotIndex N)
1306d88c1a5aSDimitry Andric : MO(O), RegIdx(R), Next(N) {}
13072cab237bSDimitry Andric
1308d88c1a5aSDimitry Andric MachineOperand MO;
1309d88c1a5aSDimitry Andric unsigned RegIdx;
1310d88c1a5aSDimitry Andric SlotIndex Next;
1311d88c1a5aSDimitry Andric };
1312d88c1a5aSDimitry Andric
1313d88c1a5aSDimitry Andric SmallVector<ExtPoint,4> ExtPoints;
1314d88c1a5aSDimitry Andric
13153b0f4066SDimitry Andric for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
13162754fe60SDimitry Andric RE = MRI.reg_end(); RI != RE;) {
131791bc56edSDimitry Andric MachineOperand &MO = *RI;
1318e580952dSDimitry Andric MachineInstr *MI = MO.getParent();
1319e580952dSDimitry Andric ++RI;
13202754fe60SDimitry Andric // LiveDebugVariables should have handled all DBG_VALUE instructions.
1321e580952dSDimitry Andric if (MI->isDebugValue()) {
1322*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Zapping " << *MI);
1323e580952dSDimitry Andric MO.setReg(0);
1324e580952dSDimitry Andric continue;
1325e580952dSDimitry Andric }
13262754fe60SDimitry Andric
13276122f3e6SDimitry Andric // <undef> operands don't really read the register, so it doesn't matter
13286122f3e6SDimitry Andric // which register we choose. When the use operand is tied to a def, we must
13296122f3e6SDimitry Andric // use the same register as the def, so just do that always.
13303ca95b02SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(*MI);
13316122f3e6SDimitry Andric if (MO.isDef() || MO.isUndef())
1332dff0c46cSDimitry Andric Idx = Idx.getRegSlot(MO.isEarlyClobber());
13332754fe60SDimitry Andric
13342754fe60SDimitry Andric // Rewrite to the mapped register at Idx.
13352754fe60SDimitry Andric unsigned RegIdx = RegAssign.lookup(Idx);
1336d88c1a5aSDimitry Andric LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1337d88c1a5aSDimitry Andric MO.setReg(LI.reg);
1338*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " rewr " << printMBBReference(*MI->getParent())
1339*4ba319b5SDimitry Andric << '\t' << Idx << ':' << RegIdx << '\t' << *MI);
13402754fe60SDimitry Andric
13413b0f4066SDimitry Andric // Extend liveness to Idx if the instruction reads reg.
13426122f3e6SDimitry Andric if (!ExtendRanges || MO.isUndef())
13432754fe60SDimitry Andric continue;
13443b0f4066SDimitry Andric
13453b0f4066SDimitry Andric // Skip instructions that don't read Reg.
13463b0f4066SDimitry Andric if (MO.isDef()) {
13473b0f4066SDimitry Andric if (!MO.getSubReg() && !MO.isEarlyClobber())
13483b0f4066SDimitry Andric continue;
1349d88c1a5aSDimitry Andric // We may want to extend a live range for a partial redef, or for a use
13503b0f4066SDimitry Andric // tied to an early clobber.
13513b0f4066SDimitry Andric Idx = Idx.getPrevSlot();
13523b0f4066SDimitry Andric if (!Edit->getParent().liveAt(Idx))
13533b0f4066SDimitry Andric continue;
13543b0f4066SDimitry Andric } else
1355dff0c46cSDimitry Andric Idx = Idx.getRegSlot(true);
13563b0f4066SDimitry Andric
1357d88c1a5aSDimitry Andric SlotIndex Next = Idx.getNextSlot();
1358d88c1a5aSDimitry Andric if (LI.hasSubRanges()) {
1359d88c1a5aSDimitry Andric // We have to delay extending subranges until we have seen all operands
1360d88c1a5aSDimitry Andric // defining the register. This is because a <def,read-undef> operand
1361d88c1a5aSDimitry Andric // will create an "undef" point, and we cannot extend any subranges
1362d88c1a5aSDimitry Andric // until all of them have been accounted for.
1363d88c1a5aSDimitry Andric if (MO.isUse())
1364d88c1a5aSDimitry Andric ExtPoints.push_back(ExtPoint(MO, RegIdx, Next));
1365d88c1a5aSDimitry Andric } else {
1366d88c1a5aSDimitry Andric LiveRangeCalc &LRC = getLRCalc(RegIdx);
1367d88c1a5aSDimitry Andric LRC.extend(LI, Next, 0, ArrayRef<SlotIndex>());
1368d88c1a5aSDimitry Andric }
1369d88c1a5aSDimitry Andric }
1370d88c1a5aSDimitry Andric
1371d88c1a5aSDimitry Andric for (ExtPoint &EP : ExtPoints) {
1372d88c1a5aSDimitry Andric LiveInterval &LI = LIS.getInterval(Edit->get(EP.RegIdx));
1373d88c1a5aSDimitry Andric assert(LI.hasSubRanges());
1374d88c1a5aSDimitry Andric
1375d88c1a5aSDimitry Andric LiveRangeCalc SubLRC;
1376d88c1a5aSDimitry Andric unsigned Reg = EP.MO.getReg(), Sub = EP.MO.getSubReg();
1377d88c1a5aSDimitry Andric LaneBitmask LM = Sub != 0 ? TRI.getSubRegIndexLaneMask(Sub)
1378d88c1a5aSDimitry Andric : MRI.getMaxLaneMaskForVReg(Reg);
1379d88c1a5aSDimitry Andric for (LiveInterval::SubRange &S : LI.subranges()) {
1380d88c1a5aSDimitry Andric if ((S.LaneMask & LM).none())
1381d88c1a5aSDimitry Andric continue;
1382d88c1a5aSDimitry Andric // The problem here can be that the new register may have been created
1383d88c1a5aSDimitry Andric // for a partially defined original register. For example:
13842cab237bSDimitry Andric // %0:subreg_hireg<def,read-undef> = ...
1385d88c1a5aSDimitry Andric // ...
13862cab237bSDimitry Andric // %1 = COPY %0
1387d88c1a5aSDimitry Andric if (S.empty())
1388d88c1a5aSDimitry Andric continue;
1389d88c1a5aSDimitry Andric SubLRC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
1390d88c1a5aSDimitry Andric &LIS.getVNInfoAllocator());
1391d88c1a5aSDimitry Andric SmallVector<SlotIndex, 4> Undefs;
1392d88c1a5aSDimitry Andric LI.computeSubRangeUndefs(Undefs, S.LaneMask, MRI, *LIS.getSlotIndexes());
1393d88c1a5aSDimitry Andric SubLRC.extend(S, EP.Next, 0, Undefs);
1394d88c1a5aSDimitry Andric }
1395d88c1a5aSDimitry Andric }
1396d88c1a5aSDimitry Andric
1397d88c1a5aSDimitry Andric for (unsigned R : *Edit) {
1398d88c1a5aSDimitry Andric LiveInterval &LI = LIS.getInterval(R);
1399d88c1a5aSDimitry Andric if (!LI.hasSubRanges())
1400d88c1a5aSDimitry Andric continue;
1401d88c1a5aSDimitry Andric LI.clear();
1402d88c1a5aSDimitry Andric LI.removeEmptySubRanges();
1403d88c1a5aSDimitry Andric LIS.constructMainRangeFromSubranges(LI);
1404e580952dSDimitry Andric }
1405e580952dSDimitry Andric }
1406e580952dSDimitry Andric
deleteRematVictims()14073b0f4066SDimitry Andric void SplitEditor::deleteRematVictims() {
14083b0f4066SDimitry Andric SmallVector<MachineInstr*, 8> Dead;
14093b0f4066SDimitry Andric for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
1410f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(*I);
141139d628a0SDimitry Andric for (const LiveRange::Segment &S : LI->segments) {
1412dff0c46cSDimitry Andric // Dead defs end at the dead slot.
141339d628a0SDimitry Andric if (S.end != S.valno->def.getDeadSlot())
14143b0f4066SDimitry Andric continue;
14153ca95b02SDimitry Andric if (S.valno->isPHIDef())
14163ca95b02SDimitry Andric continue;
141739d628a0SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def);
14183b0f4066SDimitry Andric assert(MI && "Missing instruction for dead def");
14193b0f4066SDimitry Andric MI->addRegisterDead(LI->reg, &TRI);
14203b0f4066SDimitry Andric
14213b0f4066SDimitry Andric if (!MI->allDefsAreDead())
14223b0f4066SDimitry Andric continue;
14233b0f4066SDimitry Andric
1424*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "All defs dead: " << *MI);
14253b0f4066SDimitry Andric Dead.push_back(MI);
14263b0f4066SDimitry Andric }
14273b0f4066SDimitry Andric }
14283b0f4066SDimitry Andric
14293b0f4066SDimitry Andric if (Dead.empty())
14303b0f4066SDimitry Andric return;
14313b0f4066SDimitry Andric
14323ca95b02SDimitry Andric Edit->eliminateDeadDefs(Dead, None, &AA);
14333b0f4066SDimitry Andric }
14343b0f4066SDimitry Andric
forceRecomputeVNI(const VNInfo & ParentVNI)1435954b921dSDimitry Andric void SplitEditor::forceRecomputeVNI(const VNInfo &ParentVNI) {
1436954b921dSDimitry Andric // Fast-path for common case.
1437954b921dSDimitry Andric if (!ParentVNI.isPHIDef()) {
1438954b921dSDimitry Andric for (unsigned I = 0, E = Edit->size(); I != E; ++I)
1439954b921dSDimitry Andric forceRecompute(I, ParentVNI);
1440954b921dSDimitry Andric return;
1441954b921dSDimitry Andric }
1442954b921dSDimitry Andric
1443954b921dSDimitry Andric // Trace value through phis.
1444954b921dSDimitry Andric SmallPtrSet<const VNInfo *, 8> Visited; ///< whether VNI was/is in worklist.
1445954b921dSDimitry Andric SmallVector<const VNInfo *, 4> WorkList;
1446954b921dSDimitry Andric Visited.insert(&ParentVNI);
1447954b921dSDimitry Andric WorkList.push_back(&ParentVNI);
1448954b921dSDimitry Andric
1449954b921dSDimitry Andric const LiveInterval &ParentLI = Edit->getParent();
1450954b921dSDimitry Andric const SlotIndexes &Indexes = *LIS.getSlotIndexes();
1451954b921dSDimitry Andric do {
1452954b921dSDimitry Andric const VNInfo &VNI = *WorkList.back();
1453954b921dSDimitry Andric WorkList.pop_back();
1454954b921dSDimitry Andric for (unsigned I = 0, E = Edit->size(); I != E; ++I)
1455954b921dSDimitry Andric forceRecompute(I, VNI);
1456954b921dSDimitry Andric if (!VNI.isPHIDef())
1457954b921dSDimitry Andric continue;
1458954b921dSDimitry Andric
1459954b921dSDimitry Andric MachineBasicBlock &MBB = *Indexes.getMBBFromIndex(VNI.def);
1460954b921dSDimitry Andric for (const MachineBasicBlock *Pred : MBB.predecessors()) {
1461954b921dSDimitry Andric SlotIndex PredEnd = Indexes.getMBBEndIdx(Pred);
1462954b921dSDimitry Andric VNInfo *PredVNI = ParentLI.getVNInfoBefore(PredEnd);
1463954b921dSDimitry Andric assert(PredVNI && "Value available in PhiVNI predecessor");
1464954b921dSDimitry Andric if (Visited.insert(PredVNI).second)
1465954b921dSDimitry Andric WorkList.push_back(PredVNI);
1466954b921dSDimitry Andric }
1467954b921dSDimitry Andric } while(!WorkList.empty());
1468954b921dSDimitry Andric }
1469954b921dSDimitry Andric
finish(SmallVectorImpl<unsigned> * LRMap)14703b0f4066SDimitry Andric void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
14713b0f4066SDimitry Andric ++NumFinished;
14722754fe60SDimitry Andric
14732754fe60SDimitry Andric // At this point, the live intervals in Edit contain VNInfos corresponding to
14742754fe60SDimitry Andric // the inserted copies.
14752754fe60SDimitry Andric
14762754fe60SDimitry Andric // Add the original defs from the parent interval.
147739d628a0SDimitry Andric for (const VNInfo *ParentVNI : Edit->getParent().valnos) {
14782754fe60SDimitry Andric if (ParentVNI->isUnused())
14792754fe60SDimitry Andric continue;
14803b0f4066SDimitry Andric unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
1481d88c1a5aSDimitry Andric defValue(RegIdx, ParentVNI, ParentVNI->def, true);
14823b0f4066SDimitry Andric
14836122f3e6SDimitry Andric // Force rematted values to be recomputed everywhere.
14843b0f4066SDimitry Andric // The new live ranges may be truncated.
14853b0f4066SDimitry Andric if (Edit->didRematerialize(ParentVNI))
1486954b921dSDimitry Andric forceRecomputeVNI(*ParentVNI);
14876122f3e6SDimitry Andric }
14886122f3e6SDimitry Andric
14896122f3e6SDimitry Andric // Hoist back-copies to the complement interval when in spill mode.
14906122f3e6SDimitry Andric switch (SpillMode) {
14916122f3e6SDimitry Andric case SM_Partition:
14926122f3e6SDimitry Andric // Leave all back-copies as is.
14936122f3e6SDimitry Andric break;
14946122f3e6SDimitry Andric case SM_Size:
14956122f3e6SDimitry Andric case SM_Speed:
14963ca95b02SDimitry Andric // hoistCopies will behave differently between size and speed.
14973ca95b02SDimitry Andric hoistCopies();
14982754fe60SDimitry Andric }
14992754fe60SDimitry Andric
15003b0f4066SDimitry Andric // Transfer the simply mapped values, check if any are skipped.
15013b0f4066SDimitry Andric bool Skipped = transferValues();
15023ca95b02SDimitry Andric
15033ca95b02SDimitry Andric // Rewrite virtual registers, possibly extending ranges.
15043ca95b02SDimitry Andric rewriteAssigned(Skipped);
15053ca95b02SDimitry Andric
15063b0f4066SDimitry Andric if (Skipped)
15073b0f4066SDimitry Andric extendPHIKillRanges();
15082754fe60SDimitry Andric else
15093b0f4066SDimitry Andric ++NumSimple;
15102754fe60SDimitry Andric
15113b0f4066SDimitry Andric // Delete defs that were rematted everywhere.
15123b0f4066SDimitry Andric if (Skipped)
15133b0f4066SDimitry Andric deleteRematVictims();
15142754fe60SDimitry Andric
15152754fe60SDimitry Andric // Get rid of unused values and set phi-kill flags.
1516d88c1a5aSDimitry Andric for (unsigned Reg : *Edit) {
1517d88c1a5aSDimitry Andric LiveInterval &LI = LIS.getInterval(Reg);
1518d88c1a5aSDimitry Andric LI.removeEmptySubRanges();
1519f785676fSDimitry Andric LI.RenumberValues();
1520f785676fSDimitry Andric }
15212754fe60SDimitry Andric
15223b0f4066SDimitry Andric // Provide a reverse mapping from original indices to Edit ranges.
15233b0f4066SDimitry Andric if (LRMap) {
15243b0f4066SDimitry Andric LRMap->clear();
15253b0f4066SDimitry Andric for (unsigned i = 0, e = Edit->size(); i != e; ++i)
15263b0f4066SDimitry Andric LRMap->push_back(i);
15273b0f4066SDimitry Andric }
15283b0f4066SDimitry Andric
15292754fe60SDimitry Andric // Now check if any registers were separated into multiple components.
15302754fe60SDimitry Andric ConnectedVNInfoEqClasses ConEQ(LIS);
15313b0f4066SDimitry Andric for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
15322754fe60SDimitry Andric // Don't use iterators, they are invalidated by create() below.
15337d523365SDimitry Andric unsigned VReg = Edit->get(i);
15347d523365SDimitry Andric LiveInterval &LI = LIS.getInterval(VReg);
15357d523365SDimitry Andric SmallVector<LiveInterval*, 8> SplitLIs;
15367d523365SDimitry Andric LIS.splitSeparateComponents(LI, SplitLIs);
15377d523365SDimitry Andric unsigned Original = VRM.getOriginal(VReg);
15387d523365SDimitry Andric for (LiveInterval *SplitLI : SplitLIs)
15397d523365SDimitry Andric VRM.setIsSplitFromReg(SplitLI->reg, Original);
15407d523365SDimitry Andric
15413b0f4066SDimitry Andric // The new intervals all map back to i.
15423b0f4066SDimitry Andric if (LRMap)
15433b0f4066SDimitry Andric LRMap->resize(Edit->size(), i);
15442754fe60SDimitry Andric }
15452754fe60SDimitry Andric
1546e580952dSDimitry Andric // Calculate spill weight and allocation hints for new intervals.
1547f785676fSDimitry Andric Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI);
15483b0f4066SDimitry Andric
15493b0f4066SDimitry Andric assert(!LRMap || LRMap->size() == Edit->size());
1550e580952dSDimitry Andric }
1551e580952dSDimitry Andric
1552e580952dSDimitry Andric //===----------------------------------------------------------------------===//
1553e580952dSDimitry Andric // Single Block Splitting
1554e580952dSDimitry Andric //===----------------------------------------------------------------------===//
1555e580952dSDimitry Andric
shouldSplitSingleBlock(const BlockInfo & BI,bool SingleInstrs) const15566122f3e6SDimitry Andric bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
15576122f3e6SDimitry Andric bool SingleInstrs) const {
15586122f3e6SDimitry Andric // Always split for multiple instructions.
15596122f3e6SDimitry Andric if (!BI.isOneInstr())
15606122f3e6SDimitry Andric return true;
15616122f3e6SDimitry Andric // Don't split for single instructions unless explicitly requested.
15626122f3e6SDimitry Andric if (!SingleInstrs)
15632754fe60SDimitry Andric return false;
15646122f3e6SDimitry Andric // Splitting a live-through range always makes progress.
15656122f3e6SDimitry Andric if (BI.LiveIn && BI.LiveOut)
15666122f3e6SDimitry Andric return true;
15676122f3e6SDimitry Andric // No point in isolating a copy. It has no register class constraints.
15686122f3e6SDimitry Andric if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
15696122f3e6SDimitry Andric return false;
15706122f3e6SDimitry Andric // Finally, don't isolate an end point that was created by earlier splits.
15716122f3e6SDimitry Andric return isOriginalEndpoint(BI.FirstInstr);
1572e580952dSDimitry Andric }
1573e580952dSDimitry Andric
splitSingleBlock(const SplitAnalysis::BlockInfo & BI)15743b0f4066SDimitry Andric void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
15753b0f4066SDimitry Andric openIntv();
15763b0f4066SDimitry Andric SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber());
15776122f3e6SDimitry Andric SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
15783b0f4066SDimitry Andric LastSplitPoint));
15796122f3e6SDimitry Andric if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
15806122f3e6SDimitry Andric useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
15813b0f4066SDimitry Andric } else {
15823b0f4066SDimitry Andric // The last use is after the last valid split point.
15833b0f4066SDimitry Andric SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
15843b0f4066SDimitry Andric useIntv(SegStart, SegStop);
15856122f3e6SDimitry Andric overlapIntv(SegStop, BI.LastInstr);
15863b0f4066SDimitry Andric }
15873b0f4066SDimitry Andric }
15883b0f4066SDimitry Andric
158917a519f9SDimitry Andric //===----------------------------------------------------------------------===//
159017a519f9SDimitry Andric // Global Live Range Splitting Support
159117a519f9SDimitry Andric //===----------------------------------------------------------------------===//
159217a519f9SDimitry Andric
159317a519f9SDimitry Andric // These methods support a method of global live range splitting that uses a
159417a519f9SDimitry Andric // global algorithm to decide intervals for CFG edges. They will insert split
159517a519f9SDimitry Andric // points and color intervals in basic blocks while avoiding interference.
159617a519f9SDimitry Andric //
159717a519f9SDimitry Andric // Note that splitSingleBlock is also useful for blocks where both CFG edges
159817a519f9SDimitry Andric // are on the stack.
159917a519f9SDimitry Andric
splitLiveThroughBlock(unsigned MBBNum,unsigned IntvIn,SlotIndex LeaveBefore,unsigned IntvOut,SlotIndex EnterAfter)160017a519f9SDimitry Andric void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
160117a519f9SDimitry Andric unsigned IntvIn, SlotIndex LeaveBefore,
160217a519f9SDimitry Andric unsigned IntvOut, SlotIndex EnterAfter){
160317a519f9SDimitry Andric SlotIndex Start, Stop;
160491bc56edSDimitry Andric std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
160517a519f9SDimitry Andric
1606*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "%bb." << MBBNum << " [" << Start << ';' << Stop
1607*4ba319b5SDimitry Andric << ") intf " << LeaveBefore << '-' << EnterAfter
1608*4ba319b5SDimitry Andric << ", live-through " << IntvIn << " -> " << IntvOut);
160917a519f9SDimitry Andric
161017a519f9SDimitry Andric assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
161117a519f9SDimitry Andric
16126122f3e6SDimitry Andric assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
16136122f3e6SDimitry Andric assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
16146122f3e6SDimitry Andric assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
16156122f3e6SDimitry Andric
16166122f3e6SDimitry Andric MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
16176122f3e6SDimitry Andric
161817a519f9SDimitry Andric if (!IntvOut) {
1619*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ", spill on entry.\n");
162017a519f9SDimitry Andric //
162117a519f9SDimitry Andric // <<<<<<<<< Possible LeaveBefore interference.
162217a519f9SDimitry Andric // |-----------| Live through.
162317a519f9SDimitry Andric // -____________ Spill on entry.
162417a519f9SDimitry Andric //
162517a519f9SDimitry Andric selectIntv(IntvIn);
162617a519f9SDimitry Andric SlotIndex Idx = leaveIntvAtTop(*MBB);
162717a519f9SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
162817a519f9SDimitry Andric (void)Idx;
162917a519f9SDimitry Andric return;
163017a519f9SDimitry Andric }
163117a519f9SDimitry Andric
163217a519f9SDimitry Andric if (!IntvIn) {
1633*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ", reload on exit.\n");
163417a519f9SDimitry Andric //
163517a519f9SDimitry Andric // >>>>>>> Possible EnterAfter interference.
163617a519f9SDimitry Andric // |-----------| Live through.
163717a519f9SDimitry Andric // ___________-- Reload on exit.
163817a519f9SDimitry Andric //
163917a519f9SDimitry Andric selectIntv(IntvOut);
164017a519f9SDimitry Andric SlotIndex Idx = enterIntvAtEnd(*MBB);
164117a519f9SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
164217a519f9SDimitry Andric (void)Idx;
164317a519f9SDimitry Andric return;
164417a519f9SDimitry Andric }
164517a519f9SDimitry Andric
164617a519f9SDimitry Andric if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
1647*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ", straight through.\n");
164817a519f9SDimitry Andric //
164917a519f9SDimitry Andric // |-----------| Live through.
165017a519f9SDimitry Andric // ------------- Straight through, same intv, no interference.
165117a519f9SDimitry Andric //
165217a519f9SDimitry Andric selectIntv(IntvOut);
165317a519f9SDimitry Andric useIntv(Start, Stop);
165417a519f9SDimitry Andric return;
165517a519f9SDimitry Andric }
165617a519f9SDimitry Andric
165717a519f9SDimitry Andric // We cannot legally insert splits after LSP.
165817a519f9SDimitry Andric SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
16596122f3e6SDimitry Andric assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
166017a519f9SDimitry Andric
166117a519f9SDimitry Andric if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
166217a519f9SDimitry Andric LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
1663*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ", switch avoiding interference.\n");
166417a519f9SDimitry Andric //
166517a519f9SDimitry Andric // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference.
166617a519f9SDimitry Andric // |-----------| Live through.
166717a519f9SDimitry Andric // ------======= Switch intervals between interference.
166817a519f9SDimitry Andric //
166917a519f9SDimitry Andric selectIntv(IntvOut);
16706122f3e6SDimitry Andric SlotIndex Idx;
16716122f3e6SDimitry Andric if (LeaveBefore && LeaveBefore < LSP) {
16726122f3e6SDimitry Andric Idx = enterIntvBefore(LeaveBefore);
167317a519f9SDimitry Andric useIntv(Idx, Stop);
16746122f3e6SDimitry Andric } else {
16756122f3e6SDimitry Andric Idx = enterIntvAtEnd(*MBB);
16766122f3e6SDimitry Andric }
167717a519f9SDimitry Andric selectIntv(IntvIn);
167817a519f9SDimitry Andric useIntv(Start, Idx);
167917a519f9SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
168017a519f9SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
168117a519f9SDimitry Andric return;
168217a519f9SDimitry Andric }
168317a519f9SDimitry Andric
1684*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ", create local intv for interference.\n");
168517a519f9SDimitry Andric //
168617a519f9SDimitry Andric // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference.
168717a519f9SDimitry Andric // |-----------| Live through.
168817a519f9SDimitry Andric // ==---------== Switch intervals before/after interference.
168917a519f9SDimitry Andric //
169017a519f9SDimitry Andric assert(LeaveBefore <= EnterAfter && "Missed case");
169117a519f9SDimitry Andric
169217a519f9SDimitry Andric selectIntv(IntvOut);
169317a519f9SDimitry Andric SlotIndex Idx = enterIntvAfter(EnterAfter);
169417a519f9SDimitry Andric useIntv(Idx, Stop);
169517a519f9SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
169617a519f9SDimitry Andric
169717a519f9SDimitry Andric selectIntv(IntvIn);
169817a519f9SDimitry Andric Idx = leaveIntvBefore(LeaveBefore);
169917a519f9SDimitry Andric useIntv(Start, Idx);
170017a519f9SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
170117a519f9SDimitry Andric }
170217a519f9SDimitry Andric
splitRegInBlock(const SplitAnalysis::BlockInfo & BI,unsigned IntvIn,SlotIndex LeaveBefore)170317a519f9SDimitry Andric void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
170417a519f9SDimitry Andric unsigned IntvIn, SlotIndex LeaveBefore) {
170517a519f9SDimitry Andric SlotIndex Start, Stop;
170691bc56edSDimitry Andric std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
170717a519f9SDimitry Andric
1708*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << printMBBReference(*BI.MBB) << " [" << Start << ';'
1709*4ba319b5SDimitry Andric << Stop << "), uses " << BI.FirstInstr << '-'
1710*4ba319b5SDimitry Andric << BI.LastInstr << ", reg-in " << IntvIn
1711*4ba319b5SDimitry Andric << ", leave before " << LeaveBefore
171217a519f9SDimitry Andric << (BI.LiveOut ? ", stack-out" : ", killed in block"));
171317a519f9SDimitry Andric
171417a519f9SDimitry Andric assert(IntvIn && "Must have register in");
171517a519f9SDimitry Andric assert(BI.LiveIn && "Must be live-in");
171617a519f9SDimitry Andric assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
171717a519f9SDimitry Andric
17186122f3e6SDimitry Andric if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
1719*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " before interference.\n");
172017a519f9SDimitry Andric //
172117a519f9SDimitry Andric // <<< Interference after kill.
172217a519f9SDimitry Andric // |---o---x | Killed in block.
172317a519f9SDimitry Andric // ========= Use IntvIn everywhere.
172417a519f9SDimitry Andric //
172517a519f9SDimitry Andric selectIntv(IntvIn);
17266122f3e6SDimitry Andric useIntv(Start, BI.LastInstr);
172717a519f9SDimitry Andric return;
172817a519f9SDimitry Andric }
172917a519f9SDimitry Andric
173017a519f9SDimitry Andric SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
173117a519f9SDimitry Andric
17326122f3e6SDimitry Andric if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
173317a519f9SDimitry Andric //
173417a519f9SDimitry Andric // <<< Possible interference after last use.
173517a519f9SDimitry Andric // |---o---o---| Live-out on stack.
173617a519f9SDimitry Andric // =========____ Leave IntvIn after last use.
173717a519f9SDimitry Andric //
173817a519f9SDimitry Andric // < Interference after last use.
173917a519f9SDimitry Andric // |---o---o--o| Live-out on stack, late last use.
174017a519f9SDimitry Andric // ============ Copy to stack after LSP, overlap IntvIn.
174117a519f9SDimitry Andric // \_____ Stack interval is live-out.
174217a519f9SDimitry Andric //
17436122f3e6SDimitry Andric if (BI.LastInstr < LSP) {
1744*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ", spill after last use before interference.\n");
174517a519f9SDimitry Andric selectIntv(IntvIn);
17466122f3e6SDimitry Andric SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
174717a519f9SDimitry Andric useIntv(Start, Idx);
174817a519f9SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
174917a519f9SDimitry Andric } else {
1750*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ", spill before last split point.\n");
175117a519f9SDimitry Andric selectIntv(IntvIn);
175217a519f9SDimitry Andric SlotIndex Idx = leaveIntvBefore(LSP);
17536122f3e6SDimitry Andric overlapIntv(Idx, BI.LastInstr);
175417a519f9SDimitry Andric useIntv(Start, Idx);
175517a519f9SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
175617a519f9SDimitry Andric }
175717a519f9SDimitry Andric return;
175817a519f9SDimitry Andric }
175917a519f9SDimitry Andric
176017a519f9SDimitry Andric // The interference is overlapping somewhere we wanted to use IntvIn. That
176117a519f9SDimitry Andric // means we need to create a local interval that can be allocated a
176217a519f9SDimitry Andric // different register.
176317a519f9SDimitry Andric unsigned LocalIntv = openIntv();
176417a519f9SDimitry Andric (void)LocalIntv;
1765*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
176617a519f9SDimitry Andric
17676122f3e6SDimitry Andric if (!BI.LiveOut || BI.LastInstr < LSP) {
176817a519f9SDimitry Andric //
176917a519f9SDimitry Andric // <<<<<<< Interference overlapping uses.
177017a519f9SDimitry Andric // |---o---o---| Live-out on stack.
177117a519f9SDimitry Andric // =====----____ Leave IntvIn before interference, then spill.
177217a519f9SDimitry Andric //
17736122f3e6SDimitry Andric SlotIndex To = leaveIntvAfter(BI.LastInstr);
177417a519f9SDimitry Andric SlotIndex From = enterIntvBefore(LeaveBefore);
177517a519f9SDimitry Andric useIntv(From, To);
177617a519f9SDimitry Andric selectIntv(IntvIn);
177717a519f9SDimitry Andric useIntv(Start, From);
177817a519f9SDimitry Andric assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
177917a519f9SDimitry Andric return;
178017a519f9SDimitry Andric }
178117a519f9SDimitry Andric
178217a519f9SDimitry Andric // <<<<<<< Interference overlapping uses.
178317a519f9SDimitry Andric // |---o---o--o| Live-out on stack, late last use.
178417a519f9SDimitry Andric // =====------- Copy to stack before LSP, overlap LocalIntv.
178517a519f9SDimitry Andric // \_____ Stack interval is live-out.
178617a519f9SDimitry Andric //
178717a519f9SDimitry Andric SlotIndex To = leaveIntvBefore(LSP);
17886122f3e6SDimitry Andric overlapIntv(To, BI.LastInstr);
178917a519f9SDimitry Andric SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
179017a519f9SDimitry Andric useIntv(From, To);
179117a519f9SDimitry Andric selectIntv(IntvIn);
179217a519f9SDimitry Andric useIntv(Start, From);
179317a519f9SDimitry Andric assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
179417a519f9SDimitry Andric }
179517a519f9SDimitry Andric
splitRegOutBlock(const SplitAnalysis::BlockInfo & BI,unsigned IntvOut,SlotIndex EnterAfter)179617a519f9SDimitry Andric void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
179717a519f9SDimitry Andric unsigned IntvOut, SlotIndex EnterAfter) {
179817a519f9SDimitry Andric SlotIndex Start, Stop;
179991bc56edSDimitry Andric std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
180017a519f9SDimitry Andric
1801*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << printMBBReference(*BI.MBB) << " [" << Start << ';'
1802*4ba319b5SDimitry Andric << Stop << "), uses " << BI.FirstInstr << '-'
1803*4ba319b5SDimitry Andric << BI.LastInstr << ", reg-out " << IntvOut
1804*4ba319b5SDimitry Andric << ", enter after " << EnterAfter
180517a519f9SDimitry Andric << (BI.LiveIn ? ", stack-in" : ", defined in block"));
180617a519f9SDimitry Andric
180717a519f9SDimitry Andric SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
180817a519f9SDimitry Andric
180917a519f9SDimitry Andric assert(IntvOut && "Must have register out");
181017a519f9SDimitry Andric assert(BI.LiveOut && "Must be live-out");
181117a519f9SDimitry Andric assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
181217a519f9SDimitry Andric
18136122f3e6SDimitry Andric if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
1814*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " after interference.\n");
181517a519f9SDimitry Andric //
181617a519f9SDimitry Andric // >>>> Interference before def.
181717a519f9SDimitry Andric // | o---o---| Defined in block.
181817a519f9SDimitry Andric // ========= Use IntvOut everywhere.
181917a519f9SDimitry Andric //
182017a519f9SDimitry Andric selectIntv(IntvOut);
18216122f3e6SDimitry Andric useIntv(BI.FirstInstr, Stop);
182217a519f9SDimitry Andric return;
182317a519f9SDimitry Andric }
182417a519f9SDimitry Andric
18256122f3e6SDimitry Andric if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
1826*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ", reload after interference.\n");
182717a519f9SDimitry Andric //
182817a519f9SDimitry Andric // >>>> Interference before def.
182917a519f9SDimitry Andric // |---o---o---| Live-through, stack-in.
183017a519f9SDimitry Andric // ____========= Enter IntvOut before first use.
183117a519f9SDimitry Andric //
183217a519f9SDimitry Andric selectIntv(IntvOut);
18336122f3e6SDimitry Andric SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
183417a519f9SDimitry Andric useIntv(Idx, Stop);
183517a519f9SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
183617a519f9SDimitry Andric return;
183717a519f9SDimitry Andric }
183817a519f9SDimitry Andric
183917a519f9SDimitry Andric // The interference is overlapping somewhere we wanted to use IntvOut. That
184017a519f9SDimitry Andric // means we need to create a local interval that can be allocated a
184117a519f9SDimitry Andric // different register.
1842*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ", interference overlaps uses.\n");
184317a519f9SDimitry Andric //
184417a519f9SDimitry Andric // >>>>>>> Interference overlapping uses.
184517a519f9SDimitry Andric // |---o---o---| Live-through, stack-in.
184617a519f9SDimitry Andric // ____---====== Create local interval for interference range.
184717a519f9SDimitry Andric //
184817a519f9SDimitry Andric selectIntv(IntvOut);
184917a519f9SDimitry Andric SlotIndex Idx = enterIntvAfter(EnterAfter);
185017a519f9SDimitry Andric useIntv(Idx, Stop);
185117a519f9SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
185217a519f9SDimitry Andric
185317a519f9SDimitry Andric openIntv();
18546122f3e6SDimitry Andric SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
185517a519f9SDimitry Andric useIntv(From, Idx);
185617a519f9SDimitry Andric }
1857