10b57cec5SDimitry Andric //===- SplitKit.cpp - Toolkit for splitting live ranges -------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains the SplitAnalysis class as well as mutator functions for
100b57cec5SDimitry Andric // live range splitting.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #include "SplitKit.h"
150b57cec5SDimitry Andric #include "llvm/ADT/None.h"
160b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
170b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
185ffd83dbSDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/LiveRangeEdit.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/VirtRegMap.h"
320b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
330b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h"
340b57cec5SDimitry Andric #include "llvm/Support/Allocator.h"
350b57cec5SDimitry Andric #include "llvm/Support/BlockFrequency.h"
360b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
370b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
380b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
390b57cec5SDimitry Andric #include <algorithm>
400b57cec5SDimitry Andric #include <cassert>
410b57cec5SDimitry Andric #include <iterator>
420b57cec5SDimitry Andric #include <limits>
430b57cec5SDimitry Andric #include <tuple>
440b57cec5SDimitry Andric
450b57cec5SDimitry Andric using namespace llvm;
460b57cec5SDimitry Andric
470b57cec5SDimitry Andric #define DEBUG_TYPE "regalloc"
480b57cec5SDimitry Andric
490b57cec5SDimitry Andric STATISTIC(NumFinished, "Number of splits finished");
500b57cec5SDimitry Andric STATISTIC(NumSimple, "Number of splits that were simple");
510b57cec5SDimitry Andric STATISTIC(NumCopies, "Number of copies inserted for splitting");
520b57cec5SDimitry Andric STATISTIC(NumRemats, "Number of rematerialized defs for splitting");
530b57cec5SDimitry Andric STATISTIC(NumRepairs, "Number of invalid live ranges repaired");
540b57cec5SDimitry Andric
550b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
560b57cec5SDimitry Andric // Last Insert Point Analysis
570b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
580b57cec5SDimitry Andric
InsertPointAnalysis(const LiveIntervals & lis,unsigned BBNum)590b57cec5SDimitry Andric InsertPointAnalysis::InsertPointAnalysis(const LiveIntervals &lis,
600b57cec5SDimitry Andric unsigned BBNum)
610b57cec5SDimitry Andric : LIS(lis), LastInsertPoint(BBNum) {}
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric SlotIndex
computeLastInsertPoint(const LiveInterval & CurLI,const MachineBasicBlock & MBB)640b57cec5SDimitry Andric InsertPointAnalysis::computeLastInsertPoint(const LiveInterval &CurLI,
650b57cec5SDimitry Andric const MachineBasicBlock &MBB) {
660b57cec5SDimitry Andric unsigned Num = MBB.getNumber();
670b57cec5SDimitry Andric std::pair<SlotIndex, SlotIndex> &LIP = LastInsertPoint[Num];
680b57cec5SDimitry Andric SlotIndex MBBEnd = LIS.getMBBEndIdx(&MBB);
690b57cec5SDimitry Andric
705ffd83dbSDimitry Andric SmallVector<const MachineBasicBlock *, 1> ExceptionalSuccessors;
715ffd83dbSDimitry Andric bool EHPadSuccessor = false;
725ffd83dbSDimitry Andric for (const MachineBasicBlock *SMBB : MBB.successors()) {
735ffd83dbSDimitry Andric if (SMBB->isEHPad()) {
745ffd83dbSDimitry Andric ExceptionalSuccessors.push_back(SMBB);
755ffd83dbSDimitry Andric EHPadSuccessor = true;
765ffd83dbSDimitry Andric } else if (SMBB->isInlineAsmBrIndirectTarget())
775ffd83dbSDimitry Andric ExceptionalSuccessors.push_back(SMBB);
785ffd83dbSDimitry Andric }
790b57cec5SDimitry Andric
800b57cec5SDimitry Andric // Compute insert points on the first call. The pair is independent of the
810b57cec5SDimitry Andric // current live interval.
820b57cec5SDimitry Andric if (!LIP.first.isValid()) {
830b57cec5SDimitry Andric MachineBasicBlock::const_iterator FirstTerm = MBB.getFirstTerminator();
840b57cec5SDimitry Andric if (FirstTerm == MBB.end())
850b57cec5SDimitry Andric LIP.first = MBBEnd;
860b57cec5SDimitry Andric else
870b57cec5SDimitry Andric LIP.first = LIS.getInstructionIndex(*FirstTerm);
880b57cec5SDimitry Andric
895ffd83dbSDimitry Andric // If there is a landing pad or inlineasm_br successor, also find the
905ffd83dbSDimitry Andric // instruction. If there is no such instruction, we don't need to do
915ffd83dbSDimitry Andric // anything special. We assume there cannot be multiple instructions that
925ffd83dbSDimitry Andric // are Calls with EHPad successors or INLINEASM_BR in a block. Further, we
935ffd83dbSDimitry Andric // assume that if there are any, they will be after any other call
945ffd83dbSDimitry Andric // instructions in the block.
955ffd83dbSDimitry Andric if (ExceptionalSuccessors.empty())
960b57cec5SDimitry Andric return LIP.first;
97*5f7ddb14SDimitry Andric for (const MachineInstr &MI : llvm::reverse(MBB)) {
98*5f7ddb14SDimitry Andric if ((EHPadSuccessor && MI.isCall()) ||
99*5f7ddb14SDimitry Andric MI.getOpcode() == TargetOpcode::INLINEASM_BR) {
100*5f7ddb14SDimitry Andric LIP.second = LIS.getInstructionIndex(MI);
1010b57cec5SDimitry Andric break;
1020b57cec5SDimitry Andric }
1030b57cec5SDimitry Andric }
1040b57cec5SDimitry Andric }
1050b57cec5SDimitry Andric
1060b57cec5SDimitry Andric // If CurLI is live into a landing pad successor, move the last insert point
1070b57cec5SDimitry Andric // back to the call that may throw.
1080b57cec5SDimitry Andric if (!LIP.second)
1090b57cec5SDimitry Andric return LIP.first;
1100b57cec5SDimitry Andric
1115ffd83dbSDimitry Andric if (none_of(ExceptionalSuccessors, [&](const MachineBasicBlock *EHPad) {
1120b57cec5SDimitry Andric return LIS.isLiveInToMBB(CurLI, EHPad);
1130b57cec5SDimitry Andric }))
1140b57cec5SDimitry Andric return LIP.first;
1150b57cec5SDimitry Andric
1160b57cec5SDimitry Andric // Find the value leaving MBB.
1170b57cec5SDimitry Andric const VNInfo *VNI = CurLI.getVNInfoBefore(MBBEnd);
1180b57cec5SDimitry Andric if (!VNI)
1190b57cec5SDimitry Andric return LIP.first;
1200b57cec5SDimitry Andric
121*5f7ddb14SDimitry Andric // The def of statepoint instruction is a gc relocation and it should be alive
122*5f7ddb14SDimitry Andric // in landing pad. So we cannot split interval after statepoint instruction.
123*5f7ddb14SDimitry Andric if (SlotIndex::isSameInstr(VNI->def, LIP.second))
124*5f7ddb14SDimitry Andric if (auto *I = LIS.getInstructionFromIndex(LIP.second))
125*5f7ddb14SDimitry Andric if (I->getOpcode() == TargetOpcode::STATEPOINT)
126*5f7ddb14SDimitry Andric return LIP.second;
127*5f7ddb14SDimitry Andric
1280b57cec5SDimitry Andric // If the value leaving MBB was defined after the call in MBB, it can't
1290b57cec5SDimitry Andric // really be live-in to the landing pad. This can happen if the landing pad
1300b57cec5SDimitry Andric // has a PHI, and this register is undef on the exceptional edge.
1310b57cec5SDimitry Andric // <rdar://problem/10664933>
1320b57cec5SDimitry Andric if (!SlotIndex::isEarlierInstr(VNI->def, LIP.second) && VNI->def < MBBEnd)
1330b57cec5SDimitry Andric return LIP.first;
1340b57cec5SDimitry Andric
1350b57cec5SDimitry Andric // Value is properly live-in to the landing pad.
1360b57cec5SDimitry Andric // Only allow inserts before the call.
1370b57cec5SDimitry Andric return LIP.second;
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric
1400b57cec5SDimitry Andric MachineBasicBlock::iterator
getLastInsertPointIter(const LiveInterval & CurLI,MachineBasicBlock & MBB)1410b57cec5SDimitry Andric InsertPointAnalysis::getLastInsertPointIter(const LiveInterval &CurLI,
1420b57cec5SDimitry Andric MachineBasicBlock &MBB) {
1430b57cec5SDimitry Andric SlotIndex LIP = getLastInsertPoint(CurLI, MBB);
1440b57cec5SDimitry Andric if (LIP == LIS.getMBBEndIdx(&MBB))
1450b57cec5SDimitry Andric return MBB.end();
1460b57cec5SDimitry Andric return LIS.getInstructionFromIndex(LIP);
1470b57cec5SDimitry Andric }
1480b57cec5SDimitry Andric
1490b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1500b57cec5SDimitry Andric // Split Analysis
1510b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1520b57cec5SDimitry Andric
SplitAnalysis(const VirtRegMap & vrm,const LiveIntervals & lis,const MachineLoopInfo & mli)1530b57cec5SDimitry Andric SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis,
1540b57cec5SDimitry Andric const MachineLoopInfo &mli)
1550b57cec5SDimitry Andric : MF(vrm.getMachineFunction()), VRM(vrm), LIS(lis), Loops(mli),
1560b57cec5SDimitry Andric TII(*MF.getSubtarget().getInstrInfo()), IPA(lis, MF.getNumBlockIDs()) {}
1570b57cec5SDimitry Andric
clear()1580b57cec5SDimitry Andric void SplitAnalysis::clear() {
1590b57cec5SDimitry Andric UseSlots.clear();
1600b57cec5SDimitry Andric UseBlocks.clear();
1610b57cec5SDimitry Andric ThroughBlocks.clear();
1620b57cec5SDimitry Andric CurLI = nullptr;
1630b57cec5SDimitry Andric DidRepairRange = false;
1640b57cec5SDimitry Andric }
1650b57cec5SDimitry Andric
1660b57cec5SDimitry Andric /// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
analyzeUses()1670b57cec5SDimitry Andric void SplitAnalysis::analyzeUses() {
1680b57cec5SDimitry Andric assert(UseSlots.empty() && "Call clear first");
1690b57cec5SDimitry Andric
1700b57cec5SDimitry Andric // First get all the defs from the interval values. This provides the correct
1710b57cec5SDimitry Andric // slots for early clobbers.
1720b57cec5SDimitry Andric for (const VNInfo *VNI : CurLI->valnos)
1730b57cec5SDimitry Andric if (!VNI->isPHIDef() && !VNI->isUnused())
1740b57cec5SDimitry Andric UseSlots.push_back(VNI->def);
1750b57cec5SDimitry Andric
1760b57cec5SDimitry Andric // Get use slots form the use-def chain.
1770b57cec5SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo();
178af732203SDimitry Andric for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg()))
1790b57cec5SDimitry Andric if (!MO.isUndef())
1800b57cec5SDimitry Andric UseSlots.push_back(LIS.getInstructionIndex(*MO.getParent()).getRegSlot());
1810b57cec5SDimitry Andric
1820b57cec5SDimitry Andric array_pod_sort(UseSlots.begin(), UseSlots.end());
1830b57cec5SDimitry Andric
1840b57cec5SDimitry Andric // Remove duplicates, keeping the smaller slot for each instruction.
1850b57cec5SDimitry Andric // That is what we want for early clobbers.
1860b57cec5SDimitry Andric UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
1870b57cec5SDimitry Andric SlotIndex::isSameInstr),
1880b57cec5SDimitry Andric UseSlots.end());
1890b57cec5SDimitry Andric
1900b57cec5SDimitry Andric // Compute per-live block info.
1910b57cec5SDimitry Andric if (!calcLiveBlockInfo()) {
1920b57cec5SDimitry Andric // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
1930b57cec5SDimitry Andric // I am looking at you, RegisterCoalescer!
1940b57cec5SDimitry Andric DidRepairRange = true;
1950b57cec5SDimitry Andric ++NumRepairs;
1960b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
1970b57cec5SDimitry Andric const_cast<LiveIntervals&>(LIS)
1980b57cec5SDimitry Andric .shrinkToUses(const_cast<LiveInterval*>(CurLI));
1990b57cec5SDimitry Andric UseBlocks.clear();
2000b57cec5SDimitry Andric ThroughBlocks.clear();
2010b57cec5SDimitry Andric bool fixed = calcLiveBlockInfo();
2020b57cec5SDimitry Andric (void)fixed;
2030b57cec5SDimitry Andric assert(fixed && "Couldn't fix broken live interval");
2040b57cec5SDimitry Andric }
2050b57cec5SDimitry Andric
2060b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Analyze counted " << UseSlots.size() << " instrs in "
2070b57cec5SDimitry Andric << UseBlocks.size() << " blocks, through "
2080b57cec5SDimitry Andric << NumThroughBlocks << " blocks.\n");
2090b57cec5SDimitry Andric }
2100b57cec5SDimitry Andric
2110b57cec5SDimitry Andric /// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
2120b57cec5SDimitry Andric /// where CurLI is live.
calcLiveBlockInfo()2130b57cec5SDimitry Andric bool SplitAnalysis::calcLiveBlockInfo() {
2140b57cec5SDimitry Andric ThroughBlocks.resize(MF.getNumBlockIDs());
2150b57cec5SDimitry Andric NumThroughBlocks = NumGapBlocks = 0;
2160b57cec5SDimitry Andric if (CurLI->empty())
2170b57cec5SDimitry Andric return true;
2180b57cec5SDimitry Andric
2190b57cec5SDimitry Andric LiveInterval::const_iterator LVI = CurLI->begin();
2200b57cec5SDimitry Andric LiveInterval::const_iterator LVE = CurLI->end();
2210b57cec5SDimitry Andric
2220b57cec5SDimitry Andric SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
2230b57cec5SDimitry Andric UseI = UseSlots.begin();
2240b57cec5SDimitry Andric UseE = UseSlots.end();
2250b57cec5SDimitry Andric
2260b57cec5SDimitry Andric // Loop over basic blocks where CurLI is live.
2270b57cec5SDimitry Andric MachineFunction::iterator MFI =
2280b57cec5SDimitry Andric LIS.getMBBFromIndex(LVI->start)->getIterator();
2290b57cec5SDimitry Andric while (true) {
2300b57cec5SDimitry Andric BlockInfo BI;
2310b57cec5SDimitry Andric BI.MBB = &*MFI;
2320b57cec5SDimitry Andric SlotIndex Start, Stop;
2330b57cec5SDimitry Andric std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
2340b57cec5SDimitry Andric
2350b57cec5SDimitry Andric // If the block contains no uses, the range must be live through. At one
2360b57cec5SDimitry Andric // point, RegisterCoalescer could create dangling ranges that ended
2370b57cec5SDimitry Andric // mid-block.
2380b57cec5SDimitry Andric if (UseI == UseE || *UseI >= Stop) {
2390b57cec5SDimitry Andric ++NumThroughBlocks;
2400b57cec5SDimitry Andric ThroughBlocks.set(BI.MBB->getNumber());
2410b57cec5SDimitry Andric // The range shouldn't end mid-block if there are no uses. This shouldn't
2420b57cec5SDimitry Andric // happen.
2430b57cec5SDimitry Andric if (LVI->end < Stop)
2440b57cec5SDimitry Andric return false;
2450b57cec5SDimitry Andric } else {
2460b57cec5SDimitry Andric // This block has uses. Find the first and last uses in the block.
2470b57cec5SDimitry Andric BI.FirstInstr = *UseI;
2480b57cec5SDimitry Andric assert(BI.FirstInstr >= Start);
2490b57cec5SDimitry Andric do ++UseI;
2500b57cec5SDimitry Andric while (UseI != UseE && *UseI < Stop);
2510b57cec5SDimitry Andric BI.LastInstr = UseI[-1];
2520b57cec5SDimitry Andric assert(BI.LastInstr < Stop);
2530b57cec5SDimitry Andric
2540b57cec5SDimitry Andric // LVI is the first live segment overlapping MBB.
2550b57cec5SDimitry Andric BI.LiveIn = LVI->start <= Start;
2560b57cec5SDimitry Andric
2570b57cec5SDimitry Andric // When not live in, the first use should be a def.
2580b57cec5SDimitry Andric if (!BI.LiveIn) {
2590b57cec5SDimitry Andric assert(LVI->start == LVI->valno->def && "Dangling Segment start");
2600b57cec5SDimitry Andric assert(LVI->start == BI.FirstInstr && "First instr should be a def");
2610b57cec5SDimitry Andric BI.FirstDef = BI.FirstInstr;
2620b57cec5SDimitry Andric }
2630b57cec5SDimitry Andric
2640b57cec5SDimitry Andric // Look for gaps in the live range.
2650b57cec5SDimitry Andric BI.LiveOut = true;
2660b57cec5SDimitry Andric while (LVI->end < Stop) {
2670b57cec5SDimitry Andric SlotIndex LastStop = LVI->end;
2680b57cec5SDimitry Andric if (++LVI == LVE || LVI->start >= Stop) {
2690b57cec5SDimitry Andric BI.LiveOut = false;
2700b57cec5SDimitry Andric BI.LastInstr = LastStop;
2710b57cec5SDimitry Andric break;
2720b57cec5SDimitry Andric }
2730b57cec5SDimitry Andric
2740b57cec5SDimitry Andric if (LastStop < LVI->start) {
2750b57cec5SDimitry Andric // There is a gap in the live range. Create duplicate entries for the
2760b57cec5SDimitry Andric // live-in snippet and the live-out snippet.
2770b57cec5SDimitry Andric ++NumGapBlocks;
2780b57cec5SDimitry Andric
2790b57cec5SDimitry Andric // Push the Live-in part.
2800b57cec5SDimitry Andric BI.LiveOut = false;
2810b57cec5SDimitry Andric UseBlocks.push_back(BI);
2820b57cec5SDimitry Andric UseBlocks.back().LastInstr = LastStop;
2830b57cec5SDimitry Andric
2840b57cec5SDimitry Andric // Set up BI for the live-out part.
2850b57cec5SDimitry Andric BI.LiveIn = false;
2860b57cec5SDimitry Andric BI.LiveOut = true;
2870b57cec5SDimitry Andric BI.FirstInstr = BI.FirstDef = LVI->start;
2880b57cec5SDimitry Andric }
2890b57cec5SDimitry Andric
2900b57cec5SDimitry Andric // A Segment that starts in the middle of the block must be a def.
2910b57cec5SDimitry Andric assert(LVI->start == LVI->valno->def && "Dangling Segment start");
2920b57cec5SDimitry Andric if (!BI.FirstDef)
2930b57cec5SDimitry Andric BI.FirstDef = LVI->start;
2940b57cec5SDimitry Andric }
2950b57cec5SDimitry Andric
2960b57cec5SDimitry Andric UseBlocks.push_back(BI);
2970b57cec5SDimitry Andric
2980b57cec5SDimitry Andric // LVI is now at LVE or LVI->end >= Stop.
2990b57cec5SDimitry Andric if (LVI == LVE)
3000b57cec5SDimitry Andric break;
3010b57cec5SDimitry Andric }
3020b57cec5SDimitry Andric
3030b57cec5SDimitry Andric // Live segment ends exactly at Stop. Move to the next segment.
3040b57cec5SDimitry Andric if (LVI->end == Stop && ++LVI == LVE)
3050b57cec5SDimitry Andric break;
3060b57cec5SDimitry Andric
3070b57cec5SDimitry Andric // Pick the next basic block.
3080b57cec5SDimitry Andric if (LVI->start < Stop)
3090b57cec5SDimitry Andric ++MFI;
3100b57cec5SDimitry Andric else
3110b57cec5SDimitry Andric MFI = LIS.getMBBFromIndex(LVI->start)->getIterator();
3120b57cec5SDimitry Andric }
3130b57cec5SDimitry Andric
3140b57cec5SDimitry Andric assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
3150b57cec5SDimitry Andric return true;
3160b57cec5SDimitry Andric }
3170b57cec5SDimitry Andric
countLiveBlocks(const LiveInterval * cli) const3180b57cec5SDimitry Andric unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
3190b57cec5SDimitry Andric if (cli->empty())
3200b57cec5SDimitry Andric return 0;
3210b57cec5SDimitry Andric LiveInterval *li = const_cast<LiveInterval*>(cli);
3220b57cec5SDimitry Andric LiveInterval::iterator LVI = li->begin();
3230b57cec5SDimitry Andric LiveInterval::iterator LVE = li->end();
3240b57cec5SDimitry Andric unsigned Count = 0;
3250b57cec5SDimitry Andric
3260b57cec5SDimitry Andric // Loop over basic blocks where li is live.
3270b57cec5SDimitry Andric MachineFunction::const_iterator MFI =
3280b57cec5SDimitry Andric LIS.getMBBFromIndex(LVI->start)->getIterator();
3290b57cec5SDimitry Andric SlotIndex Stop = LIS.getMBBEndIdx(&*MFI);
3300b57cec5SDimitry Andric while (true) {
3310b57cec5SDimitry Andric ++Count;
3320b57cec5SDimitry Andric LVI = li->advanceTo(LVI, Stop);
3330b57cec5SDimitry Andric if (LVI == LVE)
3340b57cec5SDimitry Andric return Count;
3350b57cec5SDimitry Andric do {
3360b57cec5SDimitry Andric ++MFI;
3370b57cec5SDimitry Andric Stop = LIS.getMBBEndIdx(&*MFI);
3380b57cec5SDimitry Andric } while (Stop <= LVI->start);
3390b57cec5SDimitry Andric }
3400b57cec5SDimitry Andric }
3410b57cec5SDimitry Andric
isOriginalEndpoint(SlotIndex Idx) const3420b57cec5SDimitry Andric bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
343af732203SDimitry Andric unsigned OrigReg = VRM.getOriginal(CurLI->reg());
3440b57cec5SDimitry Andric const LiveInterval &Orig = LIS.getInterval(OrigReg);
3450b57cec5SDimitry Andric assert(!Orig.empty() && "Splitting empty interval?");
3460b57cec5SDimitry Andric LiveInterval::const_iterator I = Orig.find(Idx);
3470b57cec5SDimitry Andric
3480b57cec5SDimitry Andric // Range containing Idx should begin at Idx.
3490b57cec5SDimitry Andric if (I != Orig.end() && I->start <= Idx)
3500b57cec5SDimitry Andric return I->start == Idx;
3510b57cec5SDimitry Andric
3520b57cec5SDimitry Andric // Range does not contain Idx, previous must end at Idx.
3530b57cec5SDimitry Andric return I != Orig.begin() && (--I)->end == Idx;
3540b57cec5SDimitry Andric }
3550b57cec5SDimitry Andric
analyze(const LiveInterval * li)3560b57cec5SDimitry Andric void SplitAnalysis::analyze(const LiveInterval *li) {
3570b57cec5SDimitry Andric clear();
3580b57cec5SDimitry Andric CurLI = li;
3590b57cec5SDimitry Andric analyzeUses();
3600b57cec5SDimitry Andric }
3610b57cec5SDimitry Andric
3620b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3630b57cec5SDimitry Andric // Split Editor
3640b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3650b57cec5SDimitry Andric
3660b57cec5SDimitry 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,VirtRegAuxInfo & VRAI)367*5f7ddb14SDimitry Andric SplitEditor::SplitEditor(SplitAnalysis &SA, AliasAnalysis &AA,
368*5f7ddb14SDimitry Andric LiveIntervals &LIS, VirtRegMap &VRM,
369*5f7ddb14SDimitry Andric MachineDominatorTree &MDT,
370*5f7ddb14SDimitry Andric MachineBlockFrequencyInfo &MBFI, VirtRegAuxInfo &VRAI)
371*5f7ddb14SDimitry Andric : SA(SA), AA(AA), LIS(LIS), VRM(VRM),
372*5f7ddb14SDimitry Andric MRI(VRM.getMachineFunction().getRegInfo()), MDT(MDT),
373*5f7ddb14SDimitry Andric TII(*VRM.getMachineFunction().getSubtarget().getInstrInfo()),
374*5f7ddb14SDimitry Andric TRI(*VRM.getMachineFunction().getSubtarget().getRegisterInfo()),
375*5f7ddb14SDimitry Andric MBFI(MBFI), VRAI(VRAI), RegAssign(Allocator) {}
3760b57cec5SDimitry Andric
reset(LiveRangeEdit & LRE,ComplementSpillMode SM)3770b57cec5SDimitry Andric void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
3780b57cec5SDimitry Andric Edit = &LRE;
3790b57cec5SDimitry Andric SpillMode = SM;
3800b57cec5SDimitry Andric OpenIdx = 0;
3810b57cec5SDimitry Andric RegAssign.clear();
3820b57cec5SDimitry Andric Values.clear();
3830b57cec5SDimitry Andric
3845ffd83dbSDimitry Andric // Reset the LiveIntervalCalc instances needed for this spill mode.
3855ffd83dbSDimitry Andric LICalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
3860b57cec5SDimitry Andric &LIS.getVNInfoAllocator());
3870b57cec5SDimitry Andric if (SpillMode)
3885ffd83dbSDimitry Andric LICalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
3890b57cec5SDimitry Andric &LIS.getVNInfoAllocator());
3900b57cec5SDimitry Andric
3910b57cec5SDimitry Andric // We don't need an AliasAnalysis since we will only be performing
3920b57cec5SDimitry Andric // cheap-as-a-copy remats anyway.
3930b57cec5SDimitry Andric Edit->anyRematerializable(nullptr);
3940b57cec5SDimitry Andric }
3950b57cec5SDimitry Andric
3960b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const3970b57cec5SDimitry Andric LLVM_DUMP_METHOD void SplitEditor::dump() const {
3980b57cec5SDimitry Andric if (RegAssign.empty()) {
3990b57cec5SDimitry Andric dbgs() << " empty\n";
4000b57cec5SDimitry Andric return;
4010b57cec5SDimitry Andric }
4020b57cec5SDimitry Andric
4030b57cec5SDimitry Andric for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
4040b57cec5SDimitry Andric dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
4050b57cec5SDimitry Andric dbgs() << '\n';
4060b57cec5SDimitry Andric }
4070b57cec5SDimitry Andric #endif
4080b57cec5SDimitry Andric
getSubRangeForMaskExact(LaneBitmask LM,LiveInterval & LI)409af732203SDimitry Andric LiveInterval::SubRange &SplitEditor::getSubRangeForMaskExact(LaneBitmask LM,
4100b57cec5SDimitry Andric LiveInterval &LI) {
4110b57cec5SDimitry Andric for (LiveInterval::SubRange &S : LI.subranges())
4120b57cec5SDimitry Andric if (S.LaneMask == LM)
4130b57cec5SDimitry Andric return S;
4140b57cec5SDimitry Andric llvm_unreachable("SubRange for this mask not found");
4150b57cec5SDimitry Andric }
4160b57cec5SDimitry Andric
getSubRangeForMask(LaneBitmask LM,LiveInterval & LI)417af732203SDimitry Andric LiveInterval::SubRange &SplitEditor::getSubRangeForMask(LaneBitmask LM,
418af732203SDimitry Andric LiveInterval &LI) {
419af732203SDimitry Andric for (LiveInterval::SubRange &S : LI.subranges())
420af732203SDimitry Andric if ((S.LaneMask & LM) == LM)
421af732203SDimitry Andric return S;
422af732203SDimitry Andric llvm_unreachable("SubRange for this mask not found");
423af732203SDimitry Andric }
424af732203SDimitry Andric
addDeadDef(LiveInterval & LI,VNInfo * VNI,bool Original)4250b57cec5SDimitry Andric void SplitEditor::addDeadDef(LiveInterval &LI, VNInfo *VNI, bool Original) {
4260b57cec5SDimitry Andric if (!LI.hasSubRanges()) {
4270b57cec5SDimitry Andric LI.createDeadDef(VNI);
4280b57cec5SDimitry Andric return;
4290b57cec5SDimitry Andric }
4300b57cec5SDimitry Andric
4310b57cec5SDimitry Andric SlotIndex Def = VNI->def;
4320b57cec5SDimitry Andric if (Original) {
4330b57cec5SDimitry Andric // If we are transferring a def from the original interval, make sure
4340b57cec5SDimitry Andric // to only update the subranges for which the original subranges had
4350b57cec5SDimitry Andric // a def at this location.
4360b57cec5SDimitry Andric for (LiveInterval::SubRange &S : LI.subranges()) {
4370b57cec5SDimitry Andric auto &PS = getSubRangeForMask(S.LaneMask, Edit->getParent());
4380b57cec5SDimitry Andric VNInfo *PV = PS.getVNInfoAt(Def);
4390b57cec5SDimitry Andric if (PV != nullptr && PV->def == Def)
4400b57cec5SDimitry Andric S.createDeadDef(Def, LIS.getVNInfoAllocator());
4410b57cec5SDimitry Andric }
4420b57cec5SDimitry Andric } else {
4430b57cec5SDimitry Andric // This is a new def: either from rematerialization, or from an inserted
4440b57cec5SDimitry Andric // copy. Since rematerialization can regenerate a definition of a sub-
4450b57cec5SDimitry Andric // register, we need to check which subranges need to be updated.
4460b57cec5SDimitry Andric const MachineInstr *DefMI = LIS.getInstructionFromIndex(Def);
4470b57cec5SDimitry Andric assert(DefMI != nullptr);
4480b57cec5SDimitry Andric LaneBitmask LM;
4490b57cec5SDimitry Andric for (const MachineOperand &DefOp : DefMI->defs()) {
4508bcb0991SDimitry Andric Register R = DefOp.getReg();
451af732203SDimitry Andric if (R != LI.reg())
4520b57cec5SDimitry Andric continue;
4530b57cec5SDimitry Andric if (unsigned SR = DefOp.getSubReg())
4540b57cec5SDimitry Andric LM |= TRI.getSubRegIndexLaneMask(SR);
4550b57cec5SDimitry Andric else {
4560b57cec5SDimitry Andric LM = MRI.getMaxLaneMaskForVReg(R);
4570b57cec5SDimitry Andric break;
4580b57cec5SDimitry Andric }
4590b57cec5SDimitry Andric }
4600b57cec5SDimitry Andric for (LiveInterval::SubRange &S : LI.subranges())
4610b57cec5SDimitry Andric if ((S.LaneMask & LM).any())
4620b57cec5SDimitry Andric S.createDeadDef(Def, LIS.getVNInfoAllocator());
4630b57cec5SDimitry Andric }
4640b57cec5SDimitry Andric }
4650b57cec5SDimitry Andric
defValue(unsigned RegIdx,const VNInfo * ParentVNI,SlotIndex Idx,bool Original)4660b57cec5SDimitry Andric VNInfo *SplitEditor::defValue(unsigned RegIdx,
4670b57cec5SDimitry Andric const VNInfo *ParentVNI,
4680b57cec5SDimitry Andric SlotIndex Idx,
4690b57cec5SDimitry Andric bool Original) {
4700b57cec5SDimitry Andric assert(ParentVNI && "Mapping NULL value");
4710b57cec5SDimitry Andric assert(Idx.isValid() && "Invalid SlotIndex");
4720b57cec5SDimitry Andric assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
4730b57cec5SDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
4740b57cec5SDimitry Andric
4750b57cec5SDimitry Andric // Create a new value.
4760b57cec5SDimitry Andric VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
4770b57cec5SDimitry Andric
4780b57cec5SDimitry Andric bool Force = LI->hasSubRanges();
4790b57cec5SDimitry Andric ValueForcePair FP(Force ? nullptr : VNI, Force);
4800b57cec5SDimitry Andric // Use insert for lookup, so we can add missing values with a second lookup.
4810b57cec5SDimitry Andric std::pair<ValueMap::iterator, bool> InsP =
4820b57cec5SDimitry Andric Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), FP));
4830b57cec5SDimitry Andric
4840b57cec5SDimitry Andric // This was the first time (RegIdx, ParentVNI) was mapped, and it is not
4850b57cec5SDimitry Andric // forced. Keep it as a simple def without any liveness.
4860b57cec5SDimitry Andric if (!Force && InsP.second)
4870b57cec5SDimitry Andric return VNI;
4880b57cec5SDimitry Andric
4890b57cec5SDimitry Andric // If the previous value was a simple mapping, add liveness for it now.
4900b57cec5SDimitry Andric if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
4910b57cec5SDimitry Andric addDeadDef(*LI, OldVNI, Original);
4920b57cec5SDimitry Andric
4930b57cec5SDimitry Andric // No longer a simple mapping. Switch to a complex mapping. If the
4940b57cec5SDimitry Andric // interval has subranges, make it a forced mapping.
4950b57cec5SDimitry Andric InsP.first->second = ValueForcePair(nullptr, Force);
4960b57cec5SDimitry Andric }
4970b57cec5SDimitry Andric
4980b57cec5SDimitry Andric // This is a complex mapping, add liveness for VNI
4990b57cec5SDimitry Andric addDeadDef(*LI, VNI, Original);
5000b57cec5SDimitry Andric return VNI;
5010b57cec5SDimitry Andric }
5020b57cec5SDimitry Andric
forceRecompute(unsigned RegIdx,const VNInfo & ParentVNI)5030b57cec5SDimitry Andric void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo &ParentVNI) {
5040b57cec5SDimitry Andric ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI.id)];
5050b57cec5SDimitry Andric VNInfo *VNI = VFP.getPointer();
5060b57cec5SDimitry Andric
5070b57cec5SDimitry Andric // ParentVNI was either unmapped or already complex mapped. Either way, just
5080b57cec5SDimitry Andric // set the force bit.
5090b57cec5SDimitry Andric if (!VNI) {
5100b57cec5SDimitry Andric VFP.setInt(true);
5110b57cec5SDimitry Andric return;
5120b57cec5SDimitry Andric }
5130b57cec5SDimitry Andric
5140b57cec5SDimitry Andric // This was previously a single mapping. Make sure the old def is represented
5150b57cec5SDimitry Andric // by a trivial live range.
5160b57cec5SDimitry Andric addDeadDef(LIS.getInterval(Edit->get(RegIdx)), VNI, false);
5170b57cec5SDimitry Andric
5180b57cec5SDimitry Andric // Mark as complex mapped, forced.
5190b57cec5SDimitry Andric VFP = ValueForcePair(nullptr, true);
5200b57cec5SDimitry Andric }
5210b57cec5SDimitry Andric
buildSingleSubRegCopy(Register FromReg,Register ToReg,MachineBasicBlock & MBB,MachineBasicBlock::iterator InsertBefore,unsigned SubIdx,LiveInterval & DestLI,bool Late,SlotIndex Def)522af732203SDimitry Andric SlotIndex SplitEditor::buildSingleSubRegCopy(Register FromReg, Register ToReg,
5230b57cec5SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
5240b57cec5SDimitry Andric unsigned SubIdx, LiveInterval &DestLI, bool Late, SlotIndex Def) {
5250b57cec5SDimitry Andric const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY);
5260b57cec5SDimitry Andric bool FirstCopy = !Def.isValid();
5270b57cec5SDimitry Andric MachineInstr *CopyMI = BuildMI(MBB, InsertBefore, DebugLoc(), Desc)
5280b57cec5SDimitry Andric .addReg(ToReg, RegState::Define | getUndefRegState(FirstCopy)
5290b57cec5SDimitry Andric | getInternalReadRegState(!FirstCopy), SubIdx)
5300b57cec5SDimitry Andric .addReg(FromReg, 0, SubIdx);
5310b57cec5SDimitry Andric
5320b57cec5SDimitry Andric BumpPtrAllocator &Allocator = LIS.getVNInfoAllocator();
5330b57cec5SDimitry Andric SlotIndexes &Indexes = *LIS.getSlotIndexes();
5340b57cec5SDimitry Andric if (FirstCopy) {
5350b57cec5SDimitry Andric Def = Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot();
5360b57cec5SDimitry Andric } else {
5370b57cec5SDimitry Andric CopyMI->bundleWithPred();
5380b57cec5SDimitry Andric }
5390b57cec5SDimitry Andric LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubIdx);
5400b57cec5SDimitry Andric DestLI.refineSubRanges(Allocator, LaneMask,
5410b57cec5SDimitry Andric [Def, &Allocator](LiveInterval::SubRange &SR) {
5420b57cec5SDimitry Andric SR.createDeadDef(Def, Allocator);
5430b57cec5SDimitry Andric },
5440b57cec5SDimitry Andric Indexes, TRI);
5450b57cec5SDimitry Andric return Def;
5460b57cec5SDimitry Andric }
5470b57cec5SDimitry Andric
buildCopy(Register FromReg,Register ToReg,LaneBitmask LaneMask,MachineBasicBlock & MBB,MachineBasicBlock::iterator InsertBefore,bool Late,unsigned RegIdx)548af732203SDimitry Andric SlotIndex SplitEditor::buildCopy(Register FromReg, Register ToReg,
5490b57cec5SDimitry Andric LaneBitmask LaneMask, MachineBasicBlock &MBB,
5500b57cec5SDimitry Andric MachineBasicBlock::iterator InsertBefore, bool Late, unsigned RegIdx) {
5510b57cec5SDimitry Andric const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY);
5520b57cec5SDimitry Andric if (LaneMask.all() || LaneMask == MRI.getMaxLaneMaskForVReg(FromReg)) {
5530b57cec5SDimitry Andric // The full vreg is copied.
5540b57cec5SDimitry Andric MachineInstr *CopyMI =
5550b57cec5SDimitry Andric BuildMI(MBB, InsertBefore, DebugLoc(), Desc, ToReg).addReg(FromReg);
5560b57cec5SDimitry Andric SlotIndexes &Indexes = *LIS.getSlotIndexes();
5570b57cec5SDimitry Andric return Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot();
5580b57cec5SDimitry Andric }
5590b57cec5SDimitry Andric
5600b57cec5SDimitry Andric // Only a subset of lanes needs to be copied. The following is a simple
5610b57cec5SDimitry Andric // heuristic to construct a sequence of COPYs. We could add a target
5620b57cec5SDimitry Andric // specific callback if this turns out to be suboptimal.
5630b57cec5SDimitry Andric LiveInterval &DestLI = LIS.getInterval(Edit->get(RegIdx));
5640b57cec5SDimitry Andric
5650b57cec5SDimitry Andric // First pass: Try to find a perfectly matching subregister index. If none
5660b57cec5SDimitry Andric // exists find the one covering the most lanemask bits.
5670b57cec5SDimitry Andric const TargetRegisterClass *RC = MRI.getRegClass(FromReg);
5680b57cec5SDimitry Andric assert(RC == MRI.getRegClass(ToReg) && "Should have same reg class");
5690b57cec5SDimitry Andric
570*5f7ddb14SDimitry Andric SmallVector<unsigned, 8> Indexes;
5710b57cec5SDimitry Andric
5720b57cec5SDimitry Andric // Abort if we cannot possibly implement the COPY with the given indexes.
573*5f7ddb14SDimitry Andric if (!TRI.getCoveringSubRegIndexes(MRI, RC, LaneMask, Indexes))
5740b57cec5SDimitry Andric report_fatal_error("Impossible to implement partial COPY");
5750b57cec5SDimitry Andric
576*5f7ddb14SDimitry Andric SlotIndex Def;
577*5f7ddb14SDimitry Andric for (unsigned BestIdx : Indexes) {
578*5f7ddb14SDimitry Andric Def = buildSingleSubRegCopy(FromReg, ToReg, MBB, InsertBefore, BestIdx,
5790b57cec5SDimitry Andric DestLI, Late, Def);
5800b57cec5SDimitry Andric }
5810b57cec5SDimitry Andric
5820b57cec5SDimitry Andric return Def;
5830b57cec5SDimitry Andric }
5840b57cec5SDimitry Andric
defFromParent(unsigned RegIdx,VNInfo * ParentVNI,SlotIndex UseIdx,MachineBasicBlock & MBB,MachineBasicBlock::iterator I)5850b57cec5SDimitry Andric VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
5860b57cec5SDimitry Andric VNInfo *ParentVNI,
5870b57cec5SDimitry Andric SlotIndex UseIdx,
5880b57cec5SDimitry Andric MachineBasicBlock &MBB,
5890b57cec5SDimitry Andric MachineBasicBlock::iterator I) {
5900b57cec5SDimitry Andric SlotIndex Def;
5910b57cec5SDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
5920b57cec5SDimitry Andric
5930b57cec5SDimitry Andric // We may be trying to avoid interference that ends at a deleted instruction,
5940b57cec5SDimitry Andric // so always begin RegIdx 0 early and all others late.
5950b57cec5SDimitry Andric bool Late = RegIdx != 0;
5960b57cec5SDimitry Andric
5970b57cec5SDimitry Andric // Attempt cheap-as-a-copy rematerialization.
5980b57cec5SDimitry Andric unsigned Original = VRM.getOriginal(Edit->get(RegIdx));
5990b57cec5SDimitry Andric LiveInterval &OrigLI = LIS.getInterval(Original);
6000b57cec5SDimitry Andric VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx);
6010b57cec5SDimitry Andric
602af732203SDimitry Andric Register Reg = LI->reg();
6030b57cec5SDimitry Andric bool DidRemat = false;
6040b57cec5SDimitry Andric if (OrigVNI) {
6050b57cec5SDimitry Andric LiveRangeEdit::Remat RM(ParentVNI);
6060b57cec5SDimitry Andric RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def);
6070b57cec5SDimitry Andric if (Edit->canRematerializeAt(RM, OrigVNI, UseIdx, true)) {
6080b57cec5SDimitry Andric Def = Edit->rematerializeAt(MBB, I, Reg, RM, TRI, Late);
6090b57cec5SDimitry Andric ++NumRemats;
6100b57cec5SDimitry Andric DidRemat = true;
6110b57cec5SDimitry Andric }
6120b57cec5SDimitry Andric }
6130b57cec5SDimitry Andric if (!DidRemat) {
6140b57cec5SDimitry Andric LaneBitmask LaneMask;
615af732203SDimitry Andric if (OrigLI.hasSubRanges()) {
6160b57cec5SDimitry Andric LaneMask = LaneBitmask::getNone();
617af732203SDimitry Andric for (LiveInterval::SubRange &S : OrigLI.subranges()) {
618af732203SDimitry Andric if (S.liveAt(UseIdx))
6190b57cec5SDimitry Andric LaneMask |= S.LaneMask;
620af732203SDimitry Andric }
6210b57cec5SDimitry Andric } else {
6220b57cec5SDimitry Andric LaneMask = LaneBitmask::getAll();
6230b57cec5SDimitry Andric }
6240b57cec5SDimitry Andric
625af732203SDimitry Andric if (LaneMask.none()) {
626af732203SDimitry Andric const MCInstrDesc &Desc = TII.get(TargetOpcode::IMPLICIT_DEF);
627af732203SDimitry Andric MachineInstr *ImplicitDef = BuildMI(MBB, I, DebugLoc(), Desc, Reg);
628af732203SDimitry Andric SlotIndexes &Indexes = *LIS.getSlotIndexes();
629af732203SDimitry Andric Def = Indexes.insertMachineInstrInMaps(*ImplicitDef, Late).getRegSlot();
630af732203SDimitry Andric } else {
6310b57cec5SDimitry Andric ++NumCopies;
6320b57cec5SDimitry Andric Def = buildCopy(Edit->getReg(), Reg, LaneMask, MBB, I, Late, RegIdx);
6330b57cec5SDimitry Andric }
634af732203SDimitry Andric }
6350b57cec5SDimitry Andric
6360b57cec5SDimitry Andric // Define the value in Reg.
6370b57cec5SDimitry Andric return defValue(RegIdx, ParentVNI, Def, false);
6380b57cec5SDimitry Andric }
6390b57cec5SDimitry Andric
6400b57cec5SDimitry Andric /// Create a new virtual register and live interval.
openIntv()6410b57cec5SDimitry Andric unsigned SplitEditor::openIntv() {
6420b57cec5SDimitry Andric // Create the complement as index 0.
6430b57cec5SDimitry Andric if (Edit->empty())
6440b57cec5SDimitry Andric Edit->createEmptyInterval();
6450b57cec5SDimitry Andric
6460b57cec5SDimitry Andric // Create the open interval.
6470b57cec5SDimitry Andric OpenIdx = Edit->size();
6480b57cec5SDimitry Andric Edit->createEmptyInterval();
6490b57cec5SDimitry Andric return OpenIdx;
6500b57cec5SDimitry Andric }
6510b57cec5SDimitry Andric
selectIntv(unsigned Idx)6520b57cec5SDimitry Andric void SplitEditor::selectIntv(unsigned Idx) {
6530b57cec5SDimitry Andric assert(Idx != 0 && "Cannot select the complement interval");
6540b57cec5SDimitry Andric assert(Idx < Edit->size() && "Can only select previously opened interval");
6550b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n');
6560b57cec5SDimitry Andric OpenIdx = Idx;
6570b57cec5SDimitry Andric }
6580b57cec5SDimitry Andric
enterIntvBefore(SlotIndex Idx)6590b57cec5SDimitry Andric SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
6600b57cec5SDimitry Andric assert(OpenIdx && "openIntv not called before enterIntvBefore");
6610b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " enterIntvBefore " << Idx);
6620b57cec5SDimitry Andric Idx = Idx.getBaseIndex();
6630b57cec5SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
6640b57cec5SDimitry Andric if (!ParentVNI) {
6650b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ": not live\n");
6660b57cec5SDimitry Andric return Idx;
6670b57cec5SDimitry Andric }
6680b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
6690b57cec5SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
6700b57cec5SDimitry Andric assert(MI && "enterIntvBefore called with invalid index");
6710b57cec5SDimitry Andric
6720b57cec5SDimitry Andric VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
6730b57cec5SDimitry Andric return VNI->def;
6740b57cec5SDimitry Andric }
6750b57cec5SDimitry Andric
enterIntvAfter(SlotIndex Idx)6760b57cec5SDimitry Andric SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
6770b57cec5SDimitry Andric assert(OpenIdx && "openIntv not called before enterIntvAfter");
6780b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " enterIntvAfter " << Idx);
6790b57cec5SDimitry Andric Idx = Idx.getBoundaryIndex();
6800b57cec5SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
6810b57cec5SDimitry Andric if (!ParentVNI) {
6820b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ": not live\n");
6830b57cec5SDimitry Andric return Idx;
6840b57cec5SDimitry Andric }
6850b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
6860b57cec5SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
6870b57cec5SDimitry Andric assert(MI && "enterIntvAfter called with invalid index");
6880b57cec5SDimitry Andric
6890b57cec5SDimitry Andric VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
6900b57cec5SDimitry Andric std::next(MachineBasicBlock::iterator(MI)));
6910b57cec5SDimitry Andric return VNI->def;
6920b57cec5SDimitry Andric }
6930b57cec5SDimitry Andric
enterIntvAtEnd(MachineBasicBlock & MBB)6940b57cec5SDimitry Andric SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
6950b57cec5SDimitry Andric assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
6960b57cec5SDimitry Andric SlotIndex End = LIS.getMBBEndIdx(&MBB);
6970b57cec5SDimitry Andric SlotIndex Last = End.getPrevSlot();
6980b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " enterIntvAtEnd " << printMBBReference(MBB) << ", "
6990b57cec5SDimitry Andric << Last);
7000b57cec5SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
7010b57cec5SDimitry Andric if (!ParentVNI) {
7020b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ": not live\n");
7030b57cec5SDimitry Andric return End;
7040b57cec5SDimitry Andric }
705*5f7ddb14SDimitry Andric SlotIndex LSP = SA.getLastSplitPoint(&MBB);
706*5f7ddb14SDimitry Andric if (LSP < Last) {
707*5f7ddb14SDimitry Andric // It could be that the use after LSP is a def, and thus the ParentVNI
708*5f7ddb14SDimitry Andric // just selected starts at that def. For this case to exist, the def
709*5f7ddb14SDimitry Andric // must be part of a tied def/use pair (as otherwise we'd have split
710*5f7ddb14SDimitry Andric // distinct live ranges into individual live intervals), and thus we
711*5f7ddb14SDimitry Andric // can insert the def into the VNI of the use and the tied def/use
712*5f7ddb14SDimitry Andric // pair can live in the resulting interval.
713*5f7ddb14SDimitry Andric Last = LSP;
714*5f7ddb14SDimitry Andric ParentVNI = Edit->getParent().getVNInfoAt(Last);
715*5f7ddb14SDimitry Andric if (!ParentVNI) {
716*5f7ddb14SDimitry Andric // undef use --> undef tied def
717*5f7ddb14SDimitry Andric LLVM_DEBUG(dbgs() << ": tied use not live\n");
718*5f7ddb14SDimitry Andric return End;
719*5f7ddb14SDimitry Andric }
720*5f7ddb14SDimitry Andric }
721*5f7ddb14SDimitry Andric
7220b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id);
7230b57cec5SDimitry Andric VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
7240b57cec5SDimitry Andric SA.getLastSplitPointIter(&MBB));
7250b57cec5SDimitry Andric RegAssign.insert(VNI->def, End, OpenIdx);
7260b57cec5SDimitry Andric LLVM_DEBUG(dump());
7270b57cec5SDimitry Andric return VNI->def;
7280b57cec5SDimitry Andric }
7290b57cec5SDimitry Andric
7300b57cec5SDimitry Andric /// useIntv - indicate that all instructions in MBB should use OpenLI.
useIntv(const MachineBasicBlock & MBB)7310b57cec5SDimitry Andric void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
7320b57cec5SDimitry Andric useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
7330b57cec5SDimitry Andric }
7340b57cec5SDimitry Andric
useIntv(SlotIndex Start,SlotIndex End)7350b57cec5SDimitry Andric void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
7360b57cec5SDimitry Andric assert(OpenIdx && "openIntv not called before useIntv");
7370b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
7380b57cec5SDimitry Andric RegAssign.insert(Start, End, OpenIdx);
7390b57cec5SDimitry Andric LLVM_DEBUG(dump());
7400b57cec5SDimitry Andric }
7410b57cec5SDimitry Andric
leaveIntvAfter(SlotIndex Idx)7420b57cec5SDimitry Andric SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
7430b57cec5SDimitry Andric assert(OpenIdx && "openIntv not called before leaveIntvAfter");
7440b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " leaveIntvAfter " << Idx);
7450b57cec5SDimitry Andric
7460b57cec5SDimitry Andric // The interval must be live beyond the instruction at Idx.
7470b57cec5SDimitry Andric SlotIndex Boundary = Idx.getBoundaryIndex();
7480b57cec5SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
7490b57cec5SDimitry Andric if (!ParentVNI) {
7500b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ": not live\n");
7510b57cec5SDimitry Andric return Boundary.getNextSlot();
7520b57cec5SDimitry Andric }
7530b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
7540b57cec5SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
7550b57cec5SDimitry Andric assert(MI && "No instruction at index");
7560b57cec5SDimitry Andric
7570b57cec5SDimitry Andric // In spill mode, make live ranges as short as possible by inserting the copy
7580b57cec5SDimitry Andric // before MI. This is only possible if that instruction doesn't redefine the
7590b57cec5SDimitry Andric // value. The inserted COPY is not a kill, and we don't need to recompute
7600b57cec5SDimitry Andric // the source live range. The spiller also won't try to hoist this copy.
7610b57cec5SDimitry Andric if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
7620b57cec5SDimitry Andric MI->readsVirtualRegister(Edit->getReg())) {
7630b57cec5SDimitry Andric forceRecompute(0, *ParentVNI);
7640b57cec5SDimitry Andric defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
7650b57cec5SDimitry Andric return Idx;
7660b57cec5SDimitry Andric }
7670b57cec5SDimitry Andric
7680b57cec5SDimitry Andric VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
7690b57cec5SDimitry Andric std::next(MachineBasicBlock::iterator(MI)));
7700b57cec5SDimitry Andric return VNI->def;
7710b57cec5SDimitry Andric }
7720b57cec5SDimitry Andric
leaveIntvBefore(SlotIndex Idx)7730b57cec5SDimitry Andric SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
7740b57cec5SDimitry Andric assert(OpenIdx && "openIntv not called before leaveIntvBefore");
7750b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " leaveIntvBefore " << Idx);
7760b57cec5SDimitry Andric
7770b57cec5SDimitry Andric // The interval must be live into the instruction at Idx.
7780b57cec5SDimitry Andric Idx = Idx.getBaseIndex();
7790b57cec5SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
7800b57cec5SDimitry Andric if (!ParentVNI) {
7810b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ": not live\n");
7820b57cec5SDimitry Andric return Idx.getNextSlot();
7830b57cec5SDimitry Andric }
7840b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
7850b57cec5SDimitry Andric
7860b57cec5SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
7870b57cec5SDimitry Andric assert(MI && "No instruction at index");
7880b57cec5SDimitry Andric VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
7890b57cec5SDimitry Andric return VNI->def;
7900b57cec5SDimitry Andric }
7910b57cec5SDimitry Andric
leaveIntvAtTop(MachineBasicBlock & MBB)7920b57cec5SDimitry Andric SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
7930b57cec5SDimitry Andric assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
7940b57cec5SDimitry Andric SlotIndex Start = LIS.getMBBStartIdx(&MBB);
7950b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " leaveIntvAtTop " << printMBBReference(MBB) << ", "
7960b57cec5SDimitry Andric << Start);
7970b57cec5SDimitry Andric
7980b57cec5SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
7990b57cec5SDimitry Andric if (!ParentVNI) {
8000b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ": not live\n");
8010b57cec5SDimitry Andric return Start;
8020b57cec5SDimitry Andric }
8030b57cec5SDimitry Andric
8040b57cec5SDimitry Andric VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
8050b57cec5SDimitry Andric MBB.SkipPHIsLabelsAndDebug(MBB.begin()));
8060b57cec5SDimitry Andric RegAssign.insert(Start, VNI->def, OpenIdx);
8070b57cec5SDimitry Andric LLVM_DEBUG(dump());
8080b57cec5SDimitry Andric return VNI->def;
8090b57cec5SDimitry Andric }
8100b57cec5SDimitry Andric
hasTiedUseOf(MachineInstr & MI,unsigned Reg)811*5f7ddb14SDimitry Andric static bool hasTiedUseOf(MachineInstr &MI, unsigned Reg) {
812*5f7ddb14SDimitry Andric return any_of(MI.defs(), [Reg](const MachineOperand &MO) {
813*5f7ddb14SDimitry Andric return MO.isReg() && MO.isTied() && MO.getReg() == Reg;
814*5f7ddb14SDimitry Andric });
815*5f7ddb14SDimitry Andric }
816*5f7ddb14SDimitry Andric
overlapIntv(SlotIndex Start,SlotIndex End)8170b57cec5SDimitry Andric void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
8180b57cec5SDimitry Andric assert(OpenIdx && "openIntv not called before overlapIntv");
8190b57cec5SDimitry Andric const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
8200b57cec5SDimitry Andric assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
8210b57cec5SDimitry Andric "Parent changes value in extended range");
8220b57cec5SDimitry Andric assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
8230b57cec5SDimitry Andric "Range cannot span basic blocks");
8240b57cec5SDimitry Andric
8255ffd83dbSDimitry Andric // The complement interval will be extended as needed by LICalc.extend().
8260b57cec5SDimitry Andric if (ParentVNI)
8270b57cec5SDimitry Andric forceRecompute(0, *ParentVNI);
828*5f7ddb14SDimitry Andric
829*5f7ddb14SDimitry Andric // If the last use is tied to a def, we can't mark it as live for the
830*5f7ddb14SDimitry Andric // interval which includes only the use. That would cause the tied pair
831*5f7ddb14SDimitry Andric // to end up in two different intervals.
832*5f7ddb14SDimitry Andric if (auto *MI = LIS.getInstructionFromIndex(End))
833*5f7ddb14SDimitry Andric if (hasTiedUseOf(*MI, Edit->getReg())) {
834*5f7ddb14SDimitry Andric LLVM_DEBUG(dbgs() << "skip overlap due to tied def at end\n");
835*5f7ddb14SDimitry Andric return;
836*5f7ddb14SDimitry Andric }
837*5f7ddb14SDimitry Andric
8380b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
8390b57cec5SDimitry Andric RegAssign.insert(Start, End, OpenIdx);
8400b57cec5SDimitry Andric LLVM_DEBUG(dump());
8410b57cec5SDimitry Andric }
8420b57cec5SDimitry Andric
8430b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8440b57cec5SDimitry Andric // Spill modes
8450b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8460b57cec5SDimitry Andric
removeBackCopies(SmallVectorImpl<VNInfo * > & Copies)8470b57cec5SDimitry Andric void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
8480b57cec5SDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(0));
8490b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
8500b57cec5SDimitry Andric RegAssignMap::iterator AssignI;
8510b57cec5SDimitry Andric AssignI.setMap(RegAssign);
8520b57cec5SDimitry Andric
853*5f7ddb14SDimitry Andric for (const VNInfo *C : Copies) {
854*5f7ddb14SDimitry Andric SlotIndex Def = C->def;
8550b57cec5SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Def);
8560b57cec5SDimitry Andric assert(MI && "No instruction for back-copy");
8570b57cec5SDimitry Andric
8580b57cec5SDimitry Andric MachineBasicBlock *MBB = MI->getParent();
8590b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI(MI);
8600b57cec5SDimitry Andric bool AtBegin;
8610b57cec5SDimitry Andric do AtBegin = MBBI == MBB->begin();
862*5f7ddb14SDimitry Andric while (!AtBegin && (--MBBI)->isDebugOrPseudoInstr());
8630b57cec5SDimitry Andric
8640b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
8650b57cec5SDimitry Andric LIS.removeVRegDefAt(*LI, Def);
8660b57cec5SDimitry Andric LIS.RemoveMachineInstrFromMaps(*MI);
8670b57cec5SDimitry Andric MI->eraseFromParent();
8680b57cec5SDimitry Andric
8690b57cec5SDimitry Andric // Adjust RegAssign if a register assignment is killed at Def. We want to
8700b57cec5SDimitry Andric // avoid calculating the live range of the source register if possible.
8710b57cec5SDimitry Andric AssignI.find(Def.getPrevSlot());
8720b57cec5SDimitry Andric if (!AssignI.valid() || AssignI.start() >= Def)
8730b57cec5SDimitry Andric continue;
8740b57cec5SDimitry Andric // If MI doesn't kill the assigned register, just leave it.
8750b57cec5SDimitry Andric if (AssignI.stop() != Def)
8760b57cec5SDimitry Andric continue;
8770b57cec5SDimitry Andric unsigned RegIdx = AssignI.value();
878*5f7ddb14SDimitry Andric // We could hoist back-copy right after another back-copy. As a result
879*5f7ddb14SDimitry Andric // MMBI points to copy instruction which is actually dead now.
880*5f7ddb14SDimitry Andric // We cannot set its stop to MBBI which will be the same as start and
881*5f7ddb14SDimitry Andric // interval does not support that.
882*5f7ddb14SDimitry Andric SlotIndex Kill =
883*5f7ddb14SDimitry Andric AtBegin ? SlotIndex() : LIS.getInstructionIndex(*MBBI).getRegSlot();
884*5f7ddb14SDimitry Andric if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg()) ||
885*5f7ddb14SDimitry Andric Kill <= AssignI.start()) {
8860b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx
8870b57cec5SDimitry Andric << '\n');
8880b57cec5SDimitry Andric forceRecompute(RegIdx, *Edit->getParent().getVNInfoAt(Def));
8890b57cec5SDimitry Andric } else {
8900b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI);
8910b57cec5SDimitry Andric AssignI.setStop(Kill);
8920b57cec5SDimitry Andric }
8930b57cec5SDimitry Andric }
8940b57cec5SDimitry Andric }
8950b57cec5SDimitry Andric
8960b57cec5SDimitry Andric MachineBasicBlock*
findShallowDominator(MachineBasicBlock * MBB,MachineBasicBlock * DefMBB)8970b57cec5SDimitry Andric SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
8980b57cec5SDimitry Andric MachineBasicBlock *DefMBB) {
8990b57cec5SDimitry Andric if (MBB == DefMBB)
9000b57cec5SDimitry Andric return MBB;
9010b57cec5SDimitry Andric assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
9020b57cec5SDimitry Andric
9030b57cec5SDimitry Andric const MachineLoopInfo &Loops = SA.Loops;
9040b57cec5SDimitry Andric const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
9050b57cec5SDimitry Andric MachineDomTreeNode *DefDomNode = MDT[DefMBB];
9060b57cec5SDimitry Andric
9070b57cec5SDimitry Andric // Best candidate so far.
9080b57cec5SDimitry Andric MachineBasicBlock *BestMBB = MBB;
9090b57cec5SDimitry Andric unsigned BestDepth = std::numeric_limits<unsigned>::max();
9100b57cec5SDimitry Andric
9110b57cec5SDimitry Andric while (true) {
9120b57cec5SDimitry Andric const MachineLoop *Loop = Loops.getLoopFor(MBB);
9130b57cec5SDimitry Andric
9140b57cec5SDimitry Andric // MBB isn't in a loop, it doesn't get any better. All dominators have a
9150b57cec5SDimitry Andric // higher frequency by definition.
9160b57cec5SDimitry Andric if (!Loop) {
9170b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
9180b57cec5SDimitry Andric << " dominates " << printMBBReference(*MBB)
9190b57cec5SDimitry Andric << " at depth 0\n");
9200b57cec5SDimitry Andric return MBB;
9210b57cec5SDimitry Andric }
9220b57cec5SDimitry Andric
9230b57cec5SDimitry Andric // We'll never be able to exit the DefLoop.
9240b57cec5SDimitry Andric if (Loop == DefLoop) {
9250b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
9260b57cec5SDimitry Andric << " dominates " << printMBBReference(*MBB)
9270b57cec5SDimitry Andric << " in the same loop\n");
9280b57cec5SDimitry Andric return MBB;
9290b57cec5SDimitry Andric }
9300b57cec5SDimitry Andric
9310b57cec5SDimitry Andric // Least busy dominator seen so far.
9320b57cec5SDimitry Andric unsigned Depth = Loop->getLoopDepth();
9330b57cec5SDimitry Andric if (Depth < BestDepth) {
9340b57cec5SDimitry Andric BestMBB = MBB;
9350b57cec5SDimitry Andric BestDepth = Depth;
9360b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
9370b57cec5SDimitry Andric << " dominates " << printMBBReference(*MBB)
9380b57cec5SDimitry Andric << " at depth " << Depth << '\n');
9390b57cec5SDimitry Andric }
9400b57cec5SDimitry Andric
9410b57cec5SDimitry Andric // Leave loop by going to the immediate dominator of the loop header.
9420b57cec5SDimitry Andric // This is a bigger stride than simply walking up the dominator tree.
9430b57cec5SDimitry Andric MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
9440b57cec5SDimitry Andric
9450b57cec5SDimitry Andric // Too far up the dominator tree?
9460b57cec5SDimitry Andric if (!IDom || !MDT.dominates(DefDomNode, IDom))
9470b57cec5SDimitry Andric return BestMBB;
9480b57cec5SDimitry Andric
9490b57cec5SDimitry Andric MBB = IDom->getBlock();
9500b57cec5SDimitry Andric }
9510b57cec5SDimitry Andric }
9520b57cec5SDimitry Andric
computeRedundantBackCopies(DenseSet<unsigned> & NotToHoistSet,SmallVectorImpl<VNInfo * > & BackCopies)9530b57cec5SDimitry Andric void SplitEditor::computeRedundantBackCopies(
9540b57cec5SDimitry Andric DenseSet<unsigned> &NotToHoistSet, SmallVectorImpl<VNInfo *> &BackCopies) {
9550b57cec5SDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(0));
9560b57cec5SDimitry Andric LiveInterval *Parent = &Edit->getParent();
9570b57cec5SDimitry Andric SmallVector<SmallPtrSet<VNInfo *, 8>, 8> EqualVNs(Parent->getNumValNums());
9580b57cec5SDimitry Andric SmallPtrSet<VNInfo *, 8> DominatedVNIs;
9590b57cec5SDimitry Andric
9600b57cec5SDimitry Andric // Aggregate VNIs having the same value as ParentVNI.
9610b57cec5SDimitry Andric for (VNInfo *VNI : LI->valnos) {
9620b57cec5SDimitry Andric if (VNI->isUnused())
9630b57cec5SDimitry Andric continue;
9640b57cec5SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
9650b57cec5SDimitry Andric EqualVNs[ParentVNI->id].insert(VNI);
9660b57cec5SDimitry Andric }
9670b57cec5SDimitry Andric
9680b57cec5SDimitry Andric // For VNI aggregation of each ParentVNI, collect dominated, i.e.,
9690b57cec5SDimitry Andric // redundant VNIs to BackCopies.
9700b57cec5SDimitry Andric for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
9710b57cec5SDimitry Andric VNInfo *ParentVNI = Parent->getValNumInfo(i);
9720b57cec5SDimitry Andric if (!NotToHoistSet.count(ParentVNI->id))
9730b57cec5SDimitry Andric continue;
9740b57cec5SDimitry Andric SmallPtrSetIterator<VNInfo *> It1 = EqualVNs[ParentVNI->id].begin();
9750b57cec5SDimitry Andric SmallPtrSetIterator<VNInfo *> It2 = It1;
9760b57cec5SDimitry Andric for (; It1 != EqualVNs[ParentVNI->id].end(); ++It1) {
9770b57cec5SDimitry Andric It2 = It1;
9780b57cec5SDimitry Andric for (++It2; It2 != EqualVNs[ParentVNI->id].end(); ++It2) {
9790b57cec5SDimitry Andric if (DominatedVNIs.count(*It1) || DominatedVNIs.count(*It2))
9800b57cec5SDimitry Andric continue;
9810b57cec5SDimitry Andric
9820b57cec5SDimitry Andric MachineBasicBlock *MBB1 = LIS.getMBBFromIndex((*It1)->def);
9830b57cec5SDimitry Andric MachineBasicBlock *MBB2 = LIS.getMBBFromIndex((*It2)->def);
9840b57cec5SDimitry Andric if (MBB1 == MBB2) {
9850b57cec5SDimitry Andric DominatedVNIs.insert((*It1)->def < (*It2)->def ? (*It2) : (*It1));
9860b57cec5SDimitry Andric } else if (MDT.dominates(MBB1, MBB2)) {
9870b57cec5SDimitry Andric DominatedVNIs.insert(*It2);
9880b57cec5SDimitry Andric } else if (MDT.dominates(MBB2, MBB1)) {
9890b57cec5SDimitry Andric DominatedVNIs.insert(*It1);
9900b57cec5SDimitry Andric }
9910b57cec5SDimitry Andric }
9920b57cec5SDimitry Andric }
9930b57cec5SDimitry Andric if (!DominatedVNIs.empty()) {
9940b57cec5SDimitry Andric forceRecompute(0, *ParentVNI);
995af732203SDimitry Andric append_range(BackCopies, DominatedVNIs);
9960b57cec5SDimitry Andric DominatedVNIs.clear();
9970b57cec5SDimitry Andric }
9980b57cec5SDimitry Andric }
9990b57cec5SDimitry Andric }
10000b57cec5SDimitry Andric
10010b57cec5SDimitry Andric /// For SM_Size mode, find a common dominator for all the back-copies for
10020b57cec5SDimitry Andric /// the same ParentVNI and hoist the backcopies to the dominator BB.
10030b57cec5SDimitry Andric /// For SM_Speed mode, if the common dominator is hot and it is not beneficial
10040b57cec5SDimitry Andric /// to do the hoisting, simply remove the dominated backcopies for the same
10050b57cec5SDimitry Andric /// ParentVNI.
hoistCopies()10060b57cec5SDimitry Andric void SplitEditor::hoistCopies() {
10070b57cec5SDimitry Andric // Get the complement interval, always RegIdx 0.
10080b57cec5SDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(0));
10090b57cec5SDimitry Andric LiveInterval *Parent = &Edit->getParent();
10100b57cec5SDimitry Andric
10110b57cec5SDimitry Andric // Track the nearest common dominator for all back-copies for each ParentVNI,
10120b57cec5SDimitry Andric // indexed by ParentVNI->id.
10130b57cec5SDimitry Andric using DomPair = std::pair<MachineBasicBlock *, SlotIndex>;
10140b57cec5SDimitry Andric SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
10150b57cec5SDimitry Andric // The total cost of all the back-copies for each ParentVNI.
10160b57cec5SDimitry Andric SmallVector<BlockFrequency, 8> Costs(Parent->getNumValNums());
10170b57cec5SDimitry Andric // The ParentVNI->id set for which hoisting back-copies are not beneficial
10180b57cec5SDimitry Andric // for Speed.
10190b57cec5SDimitry Andric DenseSet<unsigned> NotToHoistSet;
10200b57cec5SDimitry Andric
10210b57cec5SDimitry Andric // Find the nearest common dominator for parent values with multiple
10220b57cec5SDimitry Andric // back-copies. If a single back-copy dominates, put it in DomPair.second.
10230b57cec5SDimitry Andric for (VNInfo *VNI : LI->valnos) {
10240b57cec5SDimitry Andric if (VNI->isUnused())
10250b57cec5SDimitry Andric continue;
10260b57cec5SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
10270b57cec5SDimitry Andric assert(ParentVNI && "Parent not live at complement def");
10280b57cec5SDimitry Andric
10290b57cec5SDimitry Andric // Don't hoist remats. The complement is probably going to disappear
10300b57cec5SDimitry Andric // completely anyway.
10310b57cec5SDimitry Andric if (Edit->didRematerialize(ParentVNI))
10320b57cec5SDimitry Andric continue;
10330b57cec5SDimitry Andric
10340b57cec5SDimitry Andric MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
10350b57cec5SDimitry Andric
10360b57cec5SDimitry Andric DomPair &Dom = NearestDom[ParentVNI->id];
10370b57cec5SDimitry Andric
10380b57cec5SDimitry Andric // Keep directly defined parent values. This is either a PHI or an
10390b57cec5SDimitry Andric // instruction in the complement range. All other copies of ParentVNI
10400b57cec5SDimitry Andric // should be eliminated.
10410b57cec5SDimitry Andric if (VNI->def == ParentVNI->def) {
10420b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
10430b57cec5SDimitry Andric Dom = DomPair(ValMBB, VNI->def);
10440b57cec5SDimitry Andric continue;
10450b57cec5SDimitry Andric }
10460b57cec5SDimitry Andric // Skip the singly mapped values. There is nothing to gain from hoisting a
10470b57cec5SDimitry Andric // single back-copy.
10480b57cec5SDimitry Andric if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
10490b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
10500b57cec5SDimitry Andric continue;
10510b57cec5SDimitry Andric }
10520b57cec5SDimitry Andric
10530b57cec5SDimitry Andric if (!Dom.first) {
10540b57cec5SDimitry Andric // First time we see ParentVNI. VNI dominates itself.
10550b57cec5SDimitry Andric Dom = DomPair(ValMBB, VNI->def);
10560b57cec5SDimitry Andric } else if (Dom.first == ValMBB) {
10570b57cec5SDimitry Andric // Two defs in the same block. Pick the earlier def.
10580b57cec5SDimitry Andric if (!Dom.second.isValid() || VNI->def < Dom.second)
10590b57cec5SDimitry Andric Dom.second = VNI->def;
10600b57cec5SDimitry Andric } else {
10610b57cec5SDimitry Andric // Different basic blocks. Check if one dominates.
10620b57cec5SDimitry Andric MachineBasicBlock *Near =
10630b57cec5SDimitry Andric MDT.findNearestCommonDominator(Dom.first, ValMBB);
10640b57cec5SDimitry Andric if (Near == ValMBB)
10650b57cec5SDimitry Andric // Def ValMBB dominates.
10660b57cec5SDimitry Andric Dom = DomPair(ValMBB, VNI->def);
10670b57cec5SDimitry Andric else if (Near != Dom.first)
10680b57cec5SDimitry Andric // None dominate. Hoist to common dominator, need new def.
10690b57cec5SDimitry Andric Dom = DomPair(Near, SlotIndex());
10700b57cec5SDimitry Andric Costs[ParentVNI->id] += MBFI.getBlockFreq(ValMBB);
10710b57cec5SDimitry Andric }
10720b57cec5SDimitry Andric
10730b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@'
10740b57cec5SDimitry Andric << VNI->def << " for parent " << ParentVNI->id << '@'
10750b57cec5SDimitry Andric << ParentVNI->def << " hoist to "
10760b57cec5SDimitry Andric << printMBBReference(*Dom.first) << ' ' << Dom.second
10770b57cec5SDimitry Andric << '\n');
10780b57cec5SDimitry Andric }
10790b57cec5SDimitry Andric
10800b57cec5SDimitry Andric // Insert the hoisted copies.
10810b57cec5SDimitry Andric for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
10820b57cec5SDimitry Andric DomPair &Dom = NearestDom[i];
10830b57cec5SDimitry Andric if (!Dom.first || Dom.second.isValid())
10840b57cec5SDimitry Andric continue;
10850b57cec5SDimitry Andric // This value needs a hoisted copy inserted at the end of Dom.first.
10860b57cec5SDimitry Andric VNInfo *ParentVNI = Parent->getValNumInfo(i);
10870b57cec5SDimitry Andric MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
10880b57cec5SDimitry Andric // Get a less loopy dominator than Dom.first.
10890b57cec5SDimitry Andric Dom.first = findShallowDominator(Dom.first, DefMBB);
10900b57cec5SDimitry Andric if (SpillMode == SM_Speed &&
10910b57cec5SDimitry Andric MBFI.getBlockFreq(Dom.first) > Costs[ParentVNI->id]) {
10920b57cec5SDimitry Andric NotToHoistSet.insert(ParentVNI->id);
10930b57cec5SDimitry Andric continue;
10940b57cec5SDimitry Andric }
1095*5f7ddb14SDimitry Andric SlotIndex LSP = SA.getLastSplitPoint(Dom.first);
1096*5f7ddb14SDimitry Andric if (LSP <= ParentVNI->def) {
1097*5f7ddb14SDimitry Andric NotToHoistSet.insert(ParentVNI->id);
1098*5f7ddb14SDimitry Andric continue;
1099*5f7ddb14SDimitry Andric }
1100*5f7ddb14SDimitry Andric Dom.second = defFromParent(0, ParentVNI, LSP, *Dom.first,
11010b57cec5SDimitry Andric SA.getLastSplitPointIter(Dom.first))->def;
11020b57cec5SDimitry Andric }
11030b57cec5SDimitry Andric
11040b57cec5SDimitry Andric // Remove redundant back-copies that are now known to be dominated by another
11050b57cec5SDimitry Andric // def with the same value.
11060b57cec5SDimitry Andric SmallVector<VNInfo*, 8> BackCopies;
11070b57cec5SDimitry Andric for (VNInfo *VNI : LI->valnos) {
11080b57cec5SDimitry Andric if (VNI->isUnused())
11090b57cec5SDimitry Andric continue;
11100b57cec5SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
11110b57cec5SDimitry Andric const DomPair &Dom = NearestDom[ParentVNI->id];
11120b57cec5SDimitry Andric if (!Dom.first || Dom.second == VNI->def ||
11130b57cec5SDimitry Andric NotToHoistSet.count(ParentVNI->id))
11140b57cec5SDimitry Andric continue;
11150b57cec5SDimitry Andric BackCopies.push_back(VNI);
11160b57cec5SDimitry Andric forceRecompute(0, *ParentVNI);
11170b57cec5SDimitry Andric }
11180b57cec5SDimitry Andric
11190b57cec5SDimitry Andric // If it is not beneficial to hoist all the BackCopies, simply remove
11200b57cec5SDimitry Andric // redundant BackCopies in speed mode.
11210b57cec5SDimitry Andric if (SpillMode == SM_Speed && !NotToHoistSet.empty())
11220b57cec5SDimitry Andric computeRedundantBackCopies(NotToHoistSet, BackCopies);
11230b57cec5SDimitry Andric
11240b57cec5SDimitry Andric removeBackCopies(BackCopies);
11250b57cec5SDimitry Andric }
11260b57cec5SDimitry Andric
11270b57cec5SDimitry Andric /// transferValues - Transfer all possible values to the new live ranges.
11285ffd83dbSDimitry Andric /// Values that were rematerialized are left alone, they need LICalc.extend().
transferValues()11290b57cec5SDimitry Andric bool SplitEditor::transferValues() {
11300b57cec5SDimitry Andric bool Skipped = false;
11310b57cec5SDimitry Andric RegAssignMap::const_iterator AssignI = RegAssign.begin();
11320b57cec5SDimitry Andric for (const LiveRange::Segment &S : Edit->getParent()) {
11330b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " blit " << S << ':');
11340b57cec5SDimitry Andric VNInfo *ParentVNI = S.valno;
11350b57cec5SDimitry Andric // RegAssign has holes where RegIdx 0 should be used.
11360b57cec5SDimitry Andric SlotIndex Start = S.start;
11370b57cec5SDimitry Andric AssignI.advanceTo(Start);
11380b57cec5SDimitry Andric do {
11390b57cec5SDimitry Andric unsigned RegIdx;
11400b57cec5SDimitry Andric SlotIndex End = S.end;
11410b57cec5SDimitry Andric if (!AssignI.valid()) {
11420b57cec5SDimitry Andric RegIdx = 0;
11430b57cec5SDimitry Andric } else if (AssignI.start() <= Start) {
11440b57cec5SDimitry Andric RegIdx = AssignI.value();
11450b57cec5SDimitry Andric if (AssignI.stop() < End) {
11460b57cec5SDimitry Andric End = AssignI.stop();
11470b57cec5SDimitry Andric ++AssignI;
11480b57cec5SDimitry Andric }
11490b57cec5SDimitry Andric } else {
11500b57cec5SDimitry Andric RegIdx = 0;
11510b57cec5SDimitry Andric End = std::min(End, AssignI.start());
11520b57cec5SDimitry Andric }
11530b57cec5SDimitry Andric
11540b57cec5SDimitry Andric // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
11550b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx << '('
11560b57cec5SDimitry Andric << printReg(Edit->get(RegIdx)) << ')');
11570b57cec5SDimitry Andric LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
11580b57cec5SDimitry Andric
11590b57cec5SDimitry Andric // Check for a simply defined value that can be blitted directly.
11600b57cec5SDimitry Andric ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
11610b57cec5SDimitry Andric if (VNInfo *VNI = VFP.getPointer()) {
11620b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ':' << VNI->id);
11630b57cec5SDimitry Andric LI.addSegment(LiveInterval::Segment(Start, End, VNI));
11640b57cec5SDimitry Andric Start = End;
11650b57cec5SDimitry Andric continue;
11660b57cec5SDimitry Andric }
11670b57cec5SDimitry Andric
11680b57cec5SDimitry Andric // Skip values with forced recomputation.
11690b57cec5SDimitry Andric if (VFP.getInt()) {
11700b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "(recalc)");
11710b57cec5SDimitry Andric Skipped = true;
11720b57cec5SDimitry Andric Start = End;
11730b57cec5SDimitry Andric continue;
11740b57cec5SDimitry Andric }
11750b57cec5SDimitry Andric
11765ffd83dbSDimitry Andric LiveIntervalCalc &LIC = getLICalc(RegIdx);
11770b57cec5SDimitry Andric
11780b57cec5SDimitry Andric // This value has multiple defs in RegIdx, but it wasn't rematerialized,
11790b57cec5SDimitry Andric // so the live range is accurate. Add live-in blocks in [Start;End) to the
11800b57cec5SDimitry Andric // LiveInBlocks.
11810b57cec5SDimitry Andric MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
11820b57cec5SDimitry Andric SlotIndex BlockStart, BlockEnd;
11830b57cec5SDimitry Andric std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(&*MBB);
11840b57cec5SDimitry Andric
11850b57cec5SDimitry Andric // The first block may be live-in, or it may have its own def.
11860b57cec5SDimitry Andric if (Start != BlockStart) {
11870b57cec5SDimitry Andric VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
11880b57cec5SDimitry Andric assert(VNI && "Missing def for complex mapped value");
11890b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ':' << VNI->id << "*" << printMBBReference(*MBB));
11900b57cec5SDimitry Andric // MBB has its own def. Is it also live-out?
11910b57cec5SDimitry Andric if (BlockEnd <= End)
11925ffd83dbSDimitry Andric LIC.setLiveOutValue(&*MBB, VNI);
11930b57cec5SDimitry Andric
11940b57cec5SDimitry Andric // Skip to the next block for live-in.
11950b57cec5SDimitry Andric ++MBB;
11960b57cec5SDimitry Andric BlockStart = BlockEnd;
11970b57cec5SDimitry Andric }
11980b57cec5SDimitry Andric
11990b57cec5SDimitry Andric // Handle the live-in blocks covered by [Start;End).
12000b57cec5SDimitry Andric assert(Start <= BlockStart && "Expected live-in block");
12010b57cec5SDimitry Andric while (BlockStart < End) {
12020b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ">" << printMBBReference(*MBB));
12030b57cec5SDimitry Andric BlockEnd = LIS.getMBBEndIdx(&*MBB);
12040b57cec5SDimitry Andric if (BlockStart == ParentVNI->def) {
12050b57cec5SDimitry Andric // This block has the def of a parent PHI, so it isn't live-in.
12060b57cec5SDimitry Andric assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
12070b57cec5SDimitry Andric VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
12080b57cec5SDimitry Andric assert(VNI && "Missing def for complex mapped parent PHI");
12090b57cec5SDimitry Andric if (End >= BlockEnd)
12105ffd83dbSDimitry Andric LIC.setLiveOutValue(&*MBB, VNI); // Live-out as well.
12110b57cec5SDimitry Andric } else {
12120b57cec5SDimitry Andric // This block needs a live-in value. The last block covered may not
12130b57cec5SDimitry Andric // be live-out.
12140b57cec5SDimitry Andric if (End < BlockEnd)
12155ffd83dbSDimitry Andric LIC.addLiveInBlock(LI, MDT[&*MBB], End);
12160b57cec5SDimitry Andric else {
12170b57cec5SDimitry Andric // Live-through, and we don't know the value.
12185ffd83dbSDimitry Andric LIC.addLiveInBlock(LI, MDT[&*MBB]);
12195ffd83dbSDimitry Andric LIC.setLiveOutValue(&*MBB, nullptr);
12200b57cec5SDimitry Andric }
12210b57cec5SDimitry Andric }
12220b57cec5SDimitry Andric BlockStart = BlockEnd;
12230b57cec5SDimitry Andric ++MBB;
12240b57cec5SDimitry Andric }
12250b57cec5SDimitry Andric Start = End;
12260b57cec5SDimitry Andric } while (Start != S.end);
12270b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << '\n');
12280b57cec5SDimitry Andric }
12290b57cec5SDimitry Andric
12305ffd83dbSDimitry Andric LICalc[0].calculateValues();
12310b57cec5SDimitry Andric if (SpillMode)
12325ffd83dbSDimitry Andric LICalc[1].calculateValues();
12330b57cec5SDimitry Andric
12340b57cec5SDimitry Andric return Skipped;
12350b57cec5SDimitry Andric }
12360b57cec5SDimitry Andric
removeDeadSegment(SlotIndex Def,LiveRange & LR)12370b57cec5SDimitry Andric static bool removeDeadSegment(SlotIndex Def, LiveRange &LR) {
12380b57cec5SDimitry Andric const LiveRange::Segment *Seg = LR.getSegmentContaining(Def);
12390b57cec5SDimitry Andric if (Seg == nullptr)
12400b57cec5SDimitry Andric return true;
12410b57cec5SDimitry Andric if (Seg->end != Def.getDeadSlot())
12420b57cec5SDimitry Andric return false;
12430b57cec5SDimitry Andric // This is a dead PHI. Remove it.
12440b57cec5SDimitry Andric LR.removeSegment(*Seg, true);
12450b57cec5SDimitry Andric return true;
12460b57cec5SDimitry Andric }
12470b57cec5SDimitry Andric
extendPHIRange(MachineBasicBlock & B,LiveIntervalCalc & LIC,LiveRange & LR,LaneBitmask LM,ArrayRef<SlotIndex> Undefs)12485ffd83dbSDimitry Andric void SplitEditor::extendPHIRange(MachineBasicBlock &B, LiveIntervalCalc &LIC,
12490b57cec5SDimitry Andric LiveRange &LR, LaneBitmask LM,
12500b57cec5SDimitry Andric ArrayRef<SlotIndex> Undefs) {
12510b57cec5SDimitry Andric for (MachineBasicBlock *P : B.predecessors()) {
12520b57cec5SDimitry Andric SlotIndex End = LIS.getMBBEndIdx(P);
12530b57cec5SDimitry Andric SlotIndex LastUse = End.getPrevSlot();
12540b57cec5SDimitry Andric // The predecessor may not have a live-out value. That is OK, like an
12550b57cec5SDimitry Andric // undef PHI operand.
12560b57cec5SDimitry Andric LiveInterval &PLI = Edit->getParent();
12570b57cec5SDimitry Andric // Need the cast because the inputs to ?: would otherwise be deemed
12580b57cec5SDimitry Andric // "incompatible": SubRange vs LiveInterval.
1259af732203SDimitry Andric LiveRange &PSR = !LM.all() ? getSubRangeForMaskExact(LM, PLI)
12600b57cec5SDimitry Andric : static_cast<LiveRange &>(PLI);
12610b57cec5SDimitry Andric if (PSR.liveAt(LastUse))
12625ffd83dbSDimitry Andric LIC.extend(LR, End, /*PhysReg=*/0, Undefs);
12630b57cec5SDimitry Andric }
12640b57cec5SDimitry Andric }
12650b57cec5SDimitry Andric
extendPHIKillRanges()12660b57cec5SDimitry Andric void SplitEditor::extendPHIKillRanges() {
12670b57cec5SDimitry Andric // Extend live ranges to be live-out for successor PHI values.
12680b57cec5SDimitry Andric
12690b57cec5SDimitry Andric // Visit each PHI def slot in the parent live interval. If the def is dead,
12700b57cec5SDimitry Andric // remove it. Otherwise, extend the live interval to reach the end indexes
12710b57cec5SDimitry Andric // of all predecessor blocks.
12720b57cec5SDimitry Andric
12730b57cec5SDimitry Andric LiveInterval &ParentLI = Edit->getParent();
12740b57cec5SDimitry Andric for (const VNInfo *V : ParentLI.valnos) {
12750b57cec5SDimitry Andric if (V->isUnused() || !V->isPHIDef())
12760b57cec5SDimitry Andric continue;
12770b57cec5SDimitry Andric
12780b57cec5SDimitry Andric unsigned RegIdx = RegAssign.lookup(V->def);
12790b57cec5SDimitry Andric LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
12805ffd83dbSDimitry Andric LiveIntervalCalc &LIC = getLICalc(RegIdx);
12810b57cec5SDimitry Andric MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
12820b57cec5SDimitry Andric if (!removeDeadSegment(V->def, LI))
12835ffd83dbSDimitry Andric extendPHIRange(B, LIC, LI, LaneBitmask::getAll(), /*Undefs=*/{});
12840b57cec5SDimitry Andric }
12850b57cec5SDimitry Andric
12860b57cec5SDimitry Andric SmallVector<SlotIndex, 4> Undefs;
12875ffd83dbSDimitry Andric LiveIntervalCalc SubLIC;
12880b57cec5SDimitry Andric
12890b57cec5SDimitry Andric for (LiveInterval::SubRange &PS : ParentLI.subranges()) {
12900b57cec5SDimitry Andric for (const VNInfo *V : PS.valnos) {
12910b57cec5SDimitry Andric if (V->isUnused() || !V->isPHIDef())
12920b57cec5SDimitry Andric continue;
12930b57cec5SDimitry Andric unsigned RegIdx = RegAssign.lookup(V->def);
12940b57cec5SDimitry Andric LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1295af732203SDimitry Andric LiveInterval::SubRange &S = getSubRangeForMaskExact(PS.LaneMask, LI);
12960b57cec5SDimitry Andric if (removeDeadSegment(V->def, S))
12970b57cec5SDimitry Andric continue;
12980b57cec5SDimitry Andric
12990b57cec5SDimitry Andric MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
13005ffd83dbSDimitry Andric SubLIC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
13010b57cec5SDimitry Andric &LIS.getVNInfoAllocator());
13020b57cec5SDimitry Andric Undefs.clear();
13030b57cec5SDimitry Andric LI.computeSubRangeUndefs(Undefs, PS.LaneMask, MRI, *LIS.getSlotIndexes());
13045ffd83dbSDimitry Andric extendPHIRange(B, SubLIC, S, PS.LaneMask, Undefs);
13050b57cec5SDimitry Andric }
13060b57cec5SDimitry Andric }
13070b57cec5SDimitry Andric }
13080b57cec5SDimitry Andric
13090b57cec5SDimitry Andric /// rewriteAssigned - Rewrite all uses of Edit->getReg().
rewriteAssigned(bool ExtendRanges)13100b57cec5SDimitry Andric void SplitEditor::rewriteAssigned(bool ExtendRanges) {
13110b57cec5SDimitry Andric struct ExtPoint {
13120b57cec5SDimitry Andric ExtPoint(const MachineOperand &O, unsigned R, SlotIndex N)
13130b57cec5SDimitry Andric : MO(O), RegIdx(R), Next(N) {}
13140b57cec5SDimitry Andric
13150b57cec5SDimitry Andric MachineOperand MO;
13160b57cec5SDimitry Andric unsigned RegIdx;
13170b57cec5SDimitry Andric SlotIndex Next;
13180b57cec5SDimitry Andric };
13190b57cec5SDimitry Andric
13200b57cec5SDimitry Andric SmallVector<ExtPoint,4> ExtPoints;
13210b57cec5SDimitry Andric
1322*5f7ddb14SDimitry Andric for (MachineOperand &MO :
1323*5f7ddb14SDimitry Andric llvm::make_early_inc_range(MRI.reg_operands(Edit->getReg()))) {
13240b57cec5SDimitry Andric MachineInstr *MI = MO.getParent();
13250b57cec5SDimitry Andric // LiveDebugVariables should have handled all DBG_VALUE instructions.
13260b57cec5SDimitry Andric if (MI->isDebugValue()) {
13270b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Zapping " << *MI);
13280b57cec5SDimitry Andric MO.setReg(0);
13290b57cec5SDimitry Andric continue;
13300b57cec5SDimitry Andric }
13310b57cec5SDimitry Andric
13320b57cec5SDimitry Andric // <undef> operands don't really read the register, so it doesn't matter
13330b57cec5SDimitry Andric // which register we choose. When the use operand is tied to a def, we must
13340b57cec5SDimitry Andric // use the same register as the def, so just do that always.
13350b57cec5SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(*MI);
13360b57cec5SDimitry Andric if (MO.isDef() || MO.isUndef())
13370b57cec5SDimitry Andric Idx = Idx.getRegSlot(MO.isEarlyClobber());
13380b57cec5SDimitry Andric
13390b57cec5SDimitry Andric // Rewrite to the mapped register at Idx.
13400b57cec5SDimitry Andric unsigned RegIdx = RegAssign.lookup(Idx);
13410b57cec5SDimitry Andric LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1342af732203SDimitry Andric MO.setReg(LI.reg());
13430b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " rewr " << printMBBReference(*MI->getParent())
13440b57cec5SDimitry Andric << '\t' << Idx << ':' << RegIdx << '\t' << *MI);
13450b57cec5SDimitry Andric
13460b57cec5SDimitry Andric // Extend liveness to Idx if the instruction reads reg.
13470b57cec5SDimitry Andric if (!ExtendRanges || MO.isUndef())
13480b57cec5SDimitry Andric continue;
13490b57cec5SDimitry Andric
13500b57cec5SDimitry Andric // Skip instructions that don't read Reg.
13510b57cec5SDimitry Andric if (MO.isDef()) {
13520b57cec5SDimitry Andric if (!MO.getSubReg() && !MO.isEarlyClobber())
13530b57cec5SDimitry Andric continue;
13540b57cec5SDimitry Andric // We may want to extend a live range for a partial redef, or for a use
13550b57cec5SDimitry Andric // tied to an early clobber.
13560b57cec5SDimitry Andric Idx = Idx.getPrevSlot();
13570b57cec5SDimitry Andric if (!Edit->getParent().liveAt(Idx))
13580b57cec5SDimitry Andric continue;
13590b57cec5SDimitry Andric } else
13600b57cec5SDimitry Andric Idx = Idx.getRegSlot(true);
13610b57cec5SDimitry Andric
13620b57cec5SDimitry Andric SlotIndex Next = Idx.getNextSlot();
13630b57cec5SDimitry Andric if (LI.hasSubRanges()) {
13640b57cec5SDimitry Andric // We have to delay extending subranges until we have seen all operands
13650b57cec5SDimitry Andric // defining the register. This is because a <def,read-undef> operand
13660b57cec5SDimitry Andric // will create an "undef" point, and we cannot extend any subranges
13670b57cec5SDimitry Andric // until all of them have been accounted for.
13680b57cec5SDimitry Andric if (MO.isUse())
13690b57cec5SDimitry Andric ExtPoints.push_back(ExtPoint(MO, RegIdx, Next));
13700b57cec5SDimitry Andric } else {
13715ffd83dbSDimitry Andric LiveIntervalCalc &LIC = getLICalc(RegIdx);
13725ffd83dbSDimitry Andric LIC.extend(LI, Next, 0, ArrayRef<SlotIndex>());
13730b57cec5SDimitry Andric }
13740b57cec5SDimitry Andric }
13750b57cec5SDimitry Andric
13760b57cec5SDimitry Andric for (ExtPoint &EP : ExtPoints) {
13770b57cec5SDimitry Andric LiveInterval &LI = LIS.getInterval(Edit->get(EP.RegIdx));
13780b57cec5SDimitry Andric assert(LI.hasSubRanges());
13790b57cec5SDimitry Andric
13805ffd83dbSDimitry Andric LiveIntervalCalc SubLIC;
13818bcb0991SDimitry Andric Register Reg = EP.MO.getReg(), Sub = EP.MO.getSubReg();
13820b57cec5SDimitry Andric LaneBitmask LM = Sub != 0 ? TRI.getSubRegIndexLaneMask(Sub)
13830b57cec5SDimitry Andric : MRI.getMaxLaneMaskForVReg(Reg);
13840b57cec5SDimitry Andric for (LiveInterval::SubRange &S : LI.subranges()) {
13850b57cec5SDimitry Andric if ((S.LaneMask & LM).none())
13860b57cec5SDimitry Andric continue;
13870b57cec5SDimitry Andric // The problem here can be that the new register may have been created
13880b57cec5SDimitry Andric // for a partially defined original register. For example:
13890b57cec5SDimitry Andric // %0:subreg_hireg<def,read-undef> = ...
13900b57cec5SDimitry Andric // ...
13910b57cec5SDimitry Andric // %1 = COPY %0
13920b57cec5SDimitry Andric if (S.empty())
13930b57cec5SDimitry Andric continue;
13945ffd83dbSDimitry Andric SubLIC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
13950b57cec5SDimitry Andric &LIS.getVNInfoAllocator());
13960b57cec5SDimitry Andric SmallVector<SlotIndex, 4> Undefs;
13970b57cec5SDimitry Andric LI.computeSubRangeUndefs(Undefs, S.LaneMask, MRI, *LIS.getSlotIndexes());
13985ffd83dbSDimitry Andric SubLIC.extend(S, EP.Next, 0, Undefs);
13990b57cec5SDimitry Andric }
14000b57cec5SDimitry Andric }
14010b57cec5SDimitry Andric
1402af732203SDimitry Andric for (Register R : *Edit) {
14030b57cec5SDimitry Andric LiveInterval &LI = LIS.getInterval(R);
14040b57cec5SDimitry Andric if (!LI.hasSubRanges())
14050b57cec5SDimitry Andric continue;
14060b57cec5SDimitry Andric LI.clear();
14070b57cec5SDimitry Andric LI.removeEmptySubRanges();
14080b57cec5SDimitry Andric LIS.constructMainRangeFromSubranges(LI);
14090b57cec5SDimitry Andric }
14100b57cec5SDimitry Andric }
14110b57cec5SDimitry Andric
deleteRematVictims()14120b57cec5SDimitry Andric void SplitEditor::deleteRematVictims() {
14130b57cec5SDimitry Andric SmallVector<MachineInstr*, 8> Dead;
1414*5f7ddb14SDimitry Andric for (const Register &R : *Edit) {
1415*5f7ddb14SDimitry Andric LiveInterval *LI = &LIS.getInterval(R);
14160b57cec5SDimitry Andric for (const LiveRange::Segment &S : LI->segments) {
14170b57cec5SDimitry Andric // Dead defs end at the dead slot.
14180b57cec5SDimitry Andric if (S.end != S.valno->def.getDeadSlot())
14190b57cec5SDimitry Andric continue;
14200b57cec5SDimitry Andric if (S.valno->isPHIDef())
14210b57cec5SDimitry Andric continue;
14220b57cec5SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def);
14230b57cec5SDimitry Andric assert(MI && "Missing instruction for dead def");
1424af732203SDimitry Andric MI->addRegisterDead(LI->reg(), &TRI);
14250b57cec5SDimitry Andric
14260b57cec5SDimitry Andric if (!MI->allDefsAreDead())
14270b57cec5SDimitry Andric continue;
14280b57cec5SDimitry Andric
14290b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "All defs dead: " << *MI);
14300b57cec5SDimitry Andric Dead.push_back(MI);
14310b57cec5SDimitry Andric }
14320b57cec5SDimitry Andric }
14330b57cec5SDimitry Andric
14340b57cec5SDimitry Andric if (Dead.empty())
14350b57cec5SDimitry Andric return;
14360b57cec5SDimitry Andric
14370b57cec5SDimitry Andric Edit->eliminateDeadDefs(Dead, None, &AA);
14380b57cec5SDimitry Andric }
14390b57cec5SDimitry Andric
forceRecomputeVNI(const VNInfo & ParentVNI)14400b57cec5SDimitry Andric void SplitEditor::forceRecomputeVNI(const VNInfo &ParentVNI) {
14410b57cec5SDimitry Andric // Fast-path for common case.
14420b57cec5SDimitry Andric if (!ParentVNI.isPHIDef()) {
14430b57cec5SDimitry Andric for (unsigned I = 0, E = Edit->size(); I != E; ++I)
14440b57cec5SDimitry Andric forceRecompute(I, ParentVNI);
14450b57cec5SDimitry Andric return;
14460b57cec5SDimitry Andric }
14470b57cec5SDimitry Andric
14480b57cec5SDimitry Andric // Trace value through phis.
14490b57cec5SDimitry Andric SmallPtrSet<const VNInfo *, 8> Visited; ///< whether VNI was/is in worklist.
14500b57cec5SDimitry Andric SmallVector<const VNInfo *, 4> WorkList;
14510b57cec5SDimitry Andric Visited.insert(&ParentVNI);
14520b57cec5SDimitry Andric WorkList.push_back(&ParentVNI);
14530b57cec5SDimitry Andric
14540b57cec5SDimitry Andric const LiveInterval &ParentLI = Edit->getParent();
14550b57cec5SDimitry Andric const SlotIndexes &Indexes = *LIS.getSlotIndexes();
14560b57cec5SDimitry Andric do {
14570b57cec5SDimitry Andric const VNInfo &VNI = *WorkList.back();
14580b57cec5SDimitry Andric WorkList.pop_back();
14590b57cec5SDimitry Andric for (unsigned I = 0, E = Edit->size(); I != E; ++I)
14600b57cec5SDimitry Andric forceRecompute(I, VNI);
14610b57cec5SDimitry Andric if (!VNI.isPHIDef())
14620b57cec5SDimitry Andric continue;
14630b57cec5SDimitry Andric
14640b57cec5SDimitry Andric MachineBasicBlock &MBB = *Indexes.getMBBFromIndex(VNI.def);
14650b57cec5SDimitry Andric for (const MachineBasicBlock *Pred : MBB.predecessors()) {
14660b57cec5SDimitry Andric SlotIndex PredEnd = Indexes.getMBBEndIdx(Pred);
14670b57cec5SDimitry Andric VNInfo *PredVNI = ParentLI.getVNInfoBefore(PredEnd);
14680b57cec5SDimitry Andric assert(PredVNI && "Value available in PhiVNI predecessor");
14690b57cec5SDimitry Andric if (Visited.insert(PredVNI).second)
14700b57cec5SDimitry Andric WorkList.push_back(PredVNI);
14710b57cec5SDimitry Andric }
14720b57cec5SDimitry Andric } while(!WorkList.empty());
14730b57cec5SDimitry Andric }
14740b57cec5SDimitry Andric
finish(SmallVectorImpl<unsigned> * LRMap)14750b57cec5SDimitry Andric void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
14760b57cec5SDimitry Andric ++NumFinished;
14770b57cec5SDimitry Andric
14780b57cec5SDimitry Andric // At this point, the live intervals in Edit contain VNInfos corresponding to
14790b57cec5SDimitry Andric // the inserted copies.
14800b57cec5SDimitry Andric
14810b57cec5SDimitry Andric // Add the original defs from the parent interval.
14820b57cec5SDimitry Andric for (const VNInfo *ParentVNI : Edit->getParent().valnos) {
14830b57cec5SDimitry Andric if (ParentVNI->isUnused())
14840b57cec5SDimitry Andric continue;
14850b57cec5SDimitry Andric unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
14860b57cec5SDimitry Andric defValue(RegIdx, ParentVNI, ParentVNI->def, true);
14870b57cec5SDimitry Andric
14880b57cec5SDimitry Andric // Force rematted values to be recomputed everywhere.
14890b57cec5SDimitry Andric // The new live ranges may be truncated.
14900b57cec5SDimitry Andric if (Edit->didRematerialize(ParentVNI))
14910b57cec5SDimitry Andric forceRecomputeVNI(*ParentVNI);
14920b57cec5SDimitry Andric }
14930b57cec5SDimitry Andric
14940b57cec5SDimitry Andric // Hoist back-copies to the complement interval when in spill mode.
14950b57cec5SDimitry Andric switch (SpillMode) {
14960b57cec5SDimitry Andric case SM_Partition:
14970b57cec5SDimitry Andric // Leave all back-copies as is.
14980b57cec5SDimitry Andric break;
14990b57cec5SDimitry Andric case SM_Size:
15000b57cec5SDimitry Andric case SM_Speed:
15010b57cec5SDimitry Andric // hoistCopies will behave differently between size and speed.
15020b57cec5SDimitry Andric hoistCopies();
15030b57cec5SDimitry Andric }
15040b57cec5SDimitry Andric
15050b57cec5SDimitry Andric // Transfer the simply mapped values, check if any are skipped.
15060b57cec5SDimitry Andric bool Skipped = transferValues();
15070b57cec5SDimitry Andric
15080b57cec5SDimitry Andric // Rewrite virtual registers, possibly extending ranges.
15090b57cec5SDimitry Andric rewriteAssigned(Skipped);
15100b57cec5SDimitry Andric
15110b57cec5SDimitry Andric if (Skipped)
15120b57cec5SDimitry Andric extendPHIKillRanges();
15130b57cec5SDimitry Andric else
15140b57cec5SDimitry Andric ++NumSimple;
15150b57cec5SDimitry Andric
15160b57cec5SDimitry Andric // Delete defs that were rematted everywhere.
15170b57cec5SDimitry Andric if (Skipped)
15180b57cec5SDimitry Andric deleteRematVictims();
15190b57cec5SDimitry Andric
15200b57cec5SDimitry Andric // Get rid of unused values and set phi-kill flags.
1521af732203SDimitry Andric for (Register Reg : *Edit) {
15220b57cec5SDimitry Andric LiveInterval &LI = LIS.getInterval(Reg);
15230b57cec5SDimitry Andric LI.removeEmptySubRanges();
15240b57cec5SDimitry Andric LI.RenumberValues();
15250b57cec5SDimitry Andric }
15260b57cec5SDimitry Andric
15270b57cec5SDimitry Andric // Provide a reverse mapping from original indices to Edit ranges.
15280b57cec5SDimitry Andric if (LRMap) {
15290b57cec5SDimitry Andric LRMap->clear();
15300b57cec5SDimitry Andric for (unsigned i = 0, e = Edit->size(); i != e; ++i)
15310b57cec5SDimitry Andric LRMap->push_back(i);
15320b57cec5SDimitry Andric }
15330b57cec5SDimitry Andric
15340b57cec5SDimitry Andric // Now check if any registers were separated into multiple components.
15350b57cec5SDimitry Andric ConnectedVNInfoEqClasses ConEQ(LIS);
15360b57cec5SDimitry Andric for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
15370b57cec5SDimitry Andric // Don't use iterators, they are invalidated by create() below.
1538af732203SDimitry Andric Register VReg = Edit->get(i);
15390b57cec5SDimitry Andric LiveInterval &LI = LIS.getInterval(VReg);
15400b57cec5SDimitry Andric SmallVector<LiveInterval*, 8> SplitLIs;
15410b57cec5SDimitry Andric LIS.splitSeparateComponents(LI, SplitLIs);
1542af732203SDimitry Andric Register Original = VRM.getOriginal(VReg);
15430b57cec5SDimitry Andric for (LiveInterval *SplitLI : SplitLIs)
1544af732203SDimitry Andric VRM.setIsSplitFromReg(SplitLI->reg(), Original);
15450b57cec5SDimitry Andric
15460b57cec5SDimitry Andric // The new intervals all map back to i.
15470b57cec5SDimitry Andric if (LRMap)
15480b57cec5SDimitry Andric LRMap->resize(Edit->size(), i);
15490b57cec5SDimitry Andric }
15500b57cec5SDimitry Andric
15510b57cec5SDimitry Andric // Calculate spill weight and allocation hints for new intervals.
1552*5f7ddb14SDimitry Andric Edit->calculateRegClassAndHint(VRM.getMachineFunction(), VRAI);
15530b57cec5SDimitry Andric
15540b57cec5SDimitry Andric assert(!LRMap || LRMap->size() == Edit->size());
15550b57cec5SDimitry Andric }
15560b57cec5SDimitry Andric
15570b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
15580b57cec5SDimitry Andric // Single Block Splitting
15590b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
15600b57cec5SDimitry Andric
shouldSplitSingleBlock(const BlockInfo & BI,bool SingleInstrs) const15610b57cec5SDimitry Andric bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
15620b57cec5SDimitry Andric bool SingleInstrs) const {
15630b57cec5SDimitry Andric // Always split for multiple instructions.
15640b57cec5SDimitry Andric if (!BI.isOneInstr())
15650b57cec5SDimitry Andric return true;
15660b57cec5SDimitry Andric // Don't split for single instructions unless explicitly requested.
15670b57cec5SDimitry Andric if (!SingleInstrs)
15680b57cec5SDimitry Andric return false;
15690b57cec5SDimitry Andric // Splitting a live-through range always makes progress.
15700b57cec5SDimitry Andric if (BI.LiveIn && BI.LiveOut)
15710b57cec5SDimitry Andric return true;
15720b57cec5SDimitry Andric // No point in isolating a copy. It has no register class constraints.
15730b57cec5SDimitry Andric if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
15740b57cec5SDimitry Andric return false;
15750b57cec5SDimitry Andric // Finally, don't isolate an end point that was created by earlier splits.
15760b57cec5SDimitry Andric return isOriginalEndpoint(BI.FirstInstr);
15770b57cec5SDimitry Andric }
15780b57cec5SDimitry Andric
splitSingleBlock(const SplitAnalysis::BlockInfo & BI)15790b57cec5SDimitry Andric void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
15800b57cec5SDimitry Andric openIntv();
1581*5f7ddb14SDimitry Andric SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB);
15820b57cec5SDimitry Andric SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
15830b57cec5SDimitry Andric LastSplitPoint));
15840b57cec5SDimitry Andric if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
15850b57cec5SDimitry Andric useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
15860b57cec5SDimitry Andric } else {
15870b57cec5SDimitry Andric // The last use is after the last valid split point.
15880b57cec5SDimitry Andric SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
15890b57cec5SDimitry Andric useIntv(SegStart, SegStop);
15900b57cec5SDimitry Andric overlapIntv(SegStop, BI.LastInstr);
15910b57cec5SDimitry Andric }
15920b57cec5SDimitry Andric }
15930b57cec5SDimitry Andric
15940b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
15950b57cec5SDimitry Andric // Global Live Range Splitting Support
15960b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
15970b57cec5SDimitry Andric
15980b57cec5SDimitry Andric // These methods support a method of global live range splitting that uses a
15990b57cec5SDimitry Andric // global algorithm to decide intervals for CFG edges. They will insert split
16000b57cec5SDimitry Andric // points and color intervals in basic blocks while avoiding interference.
16010b57cec5SDimitry Andric //
16020b57cec5SDimitry Andric // Note that splitSingleBlock is also useful for blocks where both CFG edges
16030b57cec5SDimitry Andric // are on the stack.
16040b57cec5SDimitry Andric
splitLiveThroughBlock(unsigned MBBNum,unsigned IntvIn,SlotIndex LeaveBefore,unsigned IntvOut,SlotIndex EnterAfter)16050b57cec5SDimitry Andric void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
16060b57cec5SDimitry Andric unsigned IntvIn, SlotIndex LeaveBefore,
16070b57cec5SDimitry Andric unsigned IntvOut, SlotIndex EnterAfter){
16080b57cec5SDimitry Andric SlotIndex Start, Stop;
16090b57cec5SDimitry Andric std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
16100b57cec5SDimitry Andric
16110b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "%bb." << MBBNum << " [" << Start << ';' << Stop
16120b57cec5SDimitry Andric << ") intf " << LeaveBefore << '-' << EnterAfter
16130b57cec5SDimitry Andric << ", live-through " << IntvIn << " -> " << IntvOut);
16140b57cec5SDimitry Andric
16150b57cec5SDimitry Andric assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
16160b57cec5SDimitry Andric
16170b57cec5SDimitry Andric assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
16180b57cec5SDimitry Andric assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
16190b57cec5SDimitry Andric assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
16200b57cec5SDimitry Andric
16210b57cec5SDimitry Andric MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
16220b57cec5SDimitry Andric
16230b57cec5SDimitry Andric if (!IntvOut) {
16240b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ", spill on entry.\n");
16250b57cec5SDimitry Andric //
16260b57cec5SDimitry Andric // <<<<<<<<< Possible LeaveBefore interference.
16270b57cec5SDimitry Andric // |-----------| Live through.
16280b57cec5SDimitry Andric // -____________ Spill on entry.
16290b57cec5SDimitry Andric //
16300b57cec5SDimitry Andric selectIntv(IntvIn);
16310b57cec5SDimitry Andric SlotIndex Idx = leaveIntvAtTop(*MBB);
16320b57cec5SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
16330b57cec5SDimitry Andric (void)Idx;
16340b57cec5SDimitry Andric return;
16350b57cec5SDimitry Andric }
16360b57cec5SDimitry Andric
16370b57cec5SDimitry Andric if (!IntvIn) {
16380b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ", reload on exit.\n");
16390b57cec5SDimitry Andric //
16400b57cec5SDimitry Andric // >>>>>>> Possible EnterAfter interference.
16410b57cec5SDimitry Andric // |-----------| Live through.
16420b57cec5SDimitry Andric // ___________-- Reload on exit.
16430b57cec5SDimitry Andric //
16440b57cec5SDimitry Andric selectIntv(IntvOut);
16450b57cec5SDimitry Andric SlotIndex Idx = enterIntvAtEnd(*MBB);
16460b57cec5SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
16470b57cec5SDimitry Andric (void)Idx;
16480b57cec5SDimitry Andric return;
16490b57cec5SDimitry Andric }
16500b57cec5SDimitry Andric
16510b57cec5SDimitry Andric if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
16520b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ", straight through.\n");
16530b57cec5SDimitry Andric //
16540b57cec5SDimitry Andric // |-----------| Live through.
16550b57cec5SDimitry Andric // ------------- Straight through, same intv, no interference.
16560b57cec5SDimitry Andric //
16570b57cec5SDimitry Andric selectIntv(IntvOut);
16580b57cec5SDimitry Andric useIntv(Start, Stop);
16590b57cec5SDimitry Andric return;
16600b57cec5SDimitry Andric }
16610b57cec5SDimitry Andric
16620b57cec5SDimitry Andric // We cannot legally insert splits after LSP.
16630b57cec5SDimitry Andric SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
16640b57cec5SDimitry Andric assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
16650b57cec5SDimitry Andric
16660b57cec5SDimitry Andric if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
16670b57cec5SDimitry Andric LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
16680b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ", switch avoiding interference.\n");
16690b57cec5SDimitry Andric //
16700b57cec5SDimitry Andric // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference.
16710b57cec5SDimitry Andric // |-----------| Live through.
16720b57cec5SDimitry Andric // ------======= Switch intervals between interference.
16730b57cec5SDimitry Andric //
16740b57cec5SDimitry Andric selectIntv(IntvOut);
16750b57cec5SDimitry Andric SlotIndex Idx;
16760b57cec5SDimitry Andric if (LeaveBefore && LeaveBefore < LSP) {
16770b57cec5SDimitry Andric Idx = enterIntvBefore(LeaveBefore);
16780b57cec5SDimitry Andric useIntv(Idx, Stop);
16790b57cec5SDimitry Andric } else {
16800b57cec5SDimitry Andric Idx = enterIntvAtEnd(*MBB);
16810b57cec5SDimitry Andric }
16820b57cec5SDimitry Andric selectIntv(IntvIn);
16830b57cec5SDimitry Andric useIntv(Start, Idx);
16840b57cec5SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
16850b57cec5SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
16860b57cec5SDimitry Andric return;
16870b57cec5SDimitry Andric }
16880b57cec5SDimitry Andric
16890b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ", create local intv for interference.\n");
16900b57cec5SDimitry Andric //
16910b57cec5SDimitry Andric // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference.
16920b57cec5SDimitry Andric // |-----------| Live through.
16930b57cec5SDimitry Andric // ==---------== Switch intervals before/after interference.
16940b57cec5SDimitry Andric //
16950b57cec5SDimitry Andric assert(LeaveBefore <= EnterAfter && "Missed case");
16960b57cec5SDimitry Andric
16970b57cec5SDimitry Andric selectIntv(IntvOut);
16980b57cec5SDimitry Andric SlotIndex Idx = enterIntvAfter(EnterAfter);
16990b57cec5SDimitry Andric useIntv(Idx, Stop);
17000b57cec5SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
17010b57cec5SDimitry Andric
17020b57cec5SDimitry Andric selectIntv(IntvIn);
17030b57cec5SDimitry Andric Idx = leaveIntvBefore(LeaveBefore);
17040b57cec5SDimitry Andric useIntv(Start, Idx);
17050b57cec5SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
17060b57cec5SDimitry Andric }
17070b57cec5SDimitry Andric
splitRegInBlock(const SplitAnalysis::BlockInfo & BI,unsigned IntvIn,SlotIndex LeaveBefore)17080b57cec5SDimitry Andric void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
17090b57cec5SDimitry Andric unsigned IntvIn, SlotIndex LeaveBefore) {
17100b57cec5SDimitry Andric SlotIndex Start, Stop;
17110b57cec5SDimitry Andric std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
17120b57cec5SDimitry Andric
17130b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << printMBBReference(*BI.MBB) << " [" << Start << ';'
17140b57cec5SDimitry Andric << Stop << "), uses " << BI.FirstInstr << '-'
17150b57cec5SDimitry Andric << BI.LastInstr << ", reg-in " << IntvIn
17160b57cec5SDimitry Andric << ", leave before " << LeaveBefore
17170b57cec5SDimitry Andric << (BI.LiveOut ? ", stack-out" : ", killed in block"));
17180b57cec5SDimitry Andric
17190b57cec5SDimitry Andric assert(IntvIn && "Must have register in");
17200b57cec5SDimitry Andric assert(BI.LiveIn && "Must be live-in");
17210b57cec5SDimitry Andric assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
17220b57cec5SDimitry Andric
17230b57cec5SDimitry Andric if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
17240b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " before interference.\n");
17250b57cec5SDimitry Andric //
17260b57cec5SDimitry Andric // <<< Interference after kill.
17270b57cec5SDimitry Andric // |---o---x | Killed in block.
17280b57cec5SDimitry Andric // ========= Use IntvIn everywhere.
17290b57cec5SDimitry Andric //
17300b57cec5SDimitry Andric selectIntv(IntvIn);
17310b57cec5SDimitry Andric useIntv(Start, BI.LastInstr);
17320b57cec5SDimitry Andric return;
17330b57cec5SDimitry Andric }
17340b57cec5SDimitry Andric
1735*5f7ddb14SDimitry Andric SlotIndex LSP = SA.getLastSplitPoint(BI.MBB);
17360b57cec5SDimitry Andric
17370b57cec5SDimitry Andric if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
17380b57cec5SDimitry Andric //
17390b57cec5SDimitry Andric // <<< Possible interference after last use.
17400b57cec5SDimitry Andric // |---o---o---| Live-out on stack.
17410b57cec5SDimitry Andric // =========____ Leave IntvIn after last use.
17420b57cec5SDimitry Andric //
17430b57cec5SDimitry Andric // < Interference after last use.
17440b57cec5SDimitry Andric // |---o---o--o| Live-out on stack, late last use.
17450b57cec5SDimitry Andric // ============ Copy to stack after LSP, overlap IntvIn.
17460b57cec5SDimitry Andric // \_____ Stack interval is live-out.
17470b57cec5SDimitry Andric //
17480b57cec5SDimitry Andric if (BI.LastInstr < LSP) {
17490b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ", spill after last use before interference.\n");
17500b57cec5SDimitry Andric selectIntv(IntvIn);
17510b57cec5SDimitry Andric SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
17520b57cec5SDimitry Andric useIntv(Start, Idx);
17530b57cec5SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
17540b57cec5SDimitry Andric } else {
17550b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ", spill before last split point.\n");
17560b57cec5SDimitry Andric selectIntv(IntvIn);
17570b57cec5SDimitry Andric SlotIndex Idx = leaveIntvBefore(LSP);
17580b57cec5SDimitry Andric overlapIntv(Idx, BI.LastInstr);
17590b57cec5SDimitry Andric useIntv(Start, Idx);
17600b57cec5SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
17610b57cec5SDimitry Andric }
17620b57cec5SDimitry Andric return;
17630b57cec5SDimitry Andric }
17640b57cec5SDimitry Andric
17650b57cec5SDimitry Andric // The interference is overlapping somewhere we wanted to use IntvIn. That
17660b57cec5SDimitry Andric // means we need to create a local interval that can be allocated a
17670b57cec5SDimitry Andric // different register.
17680b57cec5SDimitry Andric unsigned LocalIntv = openIntv();
17690b57cec5SDimitry Andric (void)LocalIntv;
17700b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
17710b57cec5SDimitry Andric
17720b57cec5SDimitry Andric if (!BI.LiveOut || BI.LastInstr < LSP) {
17730b57cec5SDimitry Andric //
17740b57cec5SDimitry Andric // <<<<<<< Interference overlapping uses.
17750b57cec5SDimitry Andric // |---o---o---| Live-out on stack.
17760b57cec5SDimitry Andric // =====----____ Leave IntvIn before interference, then spill.
17770b57cec5SDimitry Andric //
17780b57cec5SDimitry Andric SlotIndex To = leaveIntvAfter(BI.LastInstr);
17790b57cec5SDimitry Andric SlotIndex From = enterIntvBefore(LeaveBefore);
17800b57cec5SDimitry Andric useIntv(From, To);
17810b57cec5SDimitry Andric selectIntv(IntvIn);
17820b57cec5SDimitry Andric useIntv(Start, From);
17830b57cec5SDimitry Andric assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
17840b57cec5SDimitry Andric return;
17850b57cec5SDimitry Andric }
17860b57cec5SDimitry Andric
17870b57cec5SDimitry Andric // <<<<<<< Interference overlapping uses.
17880b57cec5SDimitry Andric // |---o---o--o| Live-out on stack, late last use.
17890b57cec5SDimitry Andric // =====------- Copy to stack before LSP, overlap LocalIntv.
17900b57cec5SDimitry Andric // \_____ Stack interval is live-out.
17910b57cec5SDimitry Andric //
17920b57cec5SDimitry Andric SlotIndex To = leaveIntvBefore(LSP);
17930b57cec5SDimitry Andric overlapIntv(To, BI.LastInstr);
17940b57cec5SDimitry Andric SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
17950b57cec5SDimitry Andric useIntv(From, To);
17960b57cec5SDimitry Andric selectIntv(IntvIn);
17970b57cec5SDimitry Andric useIntv(Start, From);
17980b57cec5SDimitry Andric assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
17990b57cec5SDimitry Andric }
18000b57cec5SDimitry Andric
splitRegOutBlock(const SplitAnalysis::BlockInfo & BI,unsigned IntvOut,SlotIndex EnterAfter)18010b57cec5SDimitry Andric void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
18020b57cec5SDimitry Andric unsigned IntvOut, SlotIndex EnterAfter) {
18030b57cec5SDimitry Andric SlotIndex Start, Stop;
18040b57cec5SDimitry Andric std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
18050b57cec5SDimitry Andric
18060b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << printMBBReference(*BI.MBB) << " [" << Start << ';'
18070b57cec5SDimitry Andric << Stop << "), uses " << BI.FirstInstr << '-'
18080b57cec5SDimitry Andric << BI.LastInstr << ", reg-out " << IntvOut
18090b57cec5SDimitry Andric << ", enter after " << EnterAfter
18100b57cec5SDimitry Andric << (BI.LiveIn ? ", stack-in" : ", defined in block"));
18110b57cec5SDimitry Andric
1812*5f7ddb14SDimitry Andric SlotIndex LSP = SA.getLastSplitPoint(BI.MBB);
18130b57cec5SDimitry Andric
18140b57cec5SDimitry Andric assert(IntvOut && "Must have register out");
18150b57cec5SDimitry Andric assert(BI.LiveOut && "Must be live-out");
18160b57cec5SDimitry Andric assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
18170b57cec5SDimitry Andric
18180b57cec5SDimitry Andric if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
18190b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " after interference.\n");
18200b57cec5SDimitry Andric //
18210b57cec5SDimitry Andric // >>>> Interference before def.
18220b57cec5SDimitry Andric // | o---o---| Defined in block.
18230b57cec5SDimitry Andric // ========= Use IntvOut everywhere.
18240b57cec5SDimitry Andric //
18250b57cec5SDimitry Andric selectIntv(IntvOut);
18260b57cec5SDimitry Andric useIntv(BI.FirstInstr, Stop);
18270b57cec5SDimitry Andric return;
18280b57cec5SDimitry Andric }
18290b57cec5SDimitry Andric
18300b57cec5SDimitry Andric if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
18310b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ", reload after interference.\n");
18320b57cec5SDimitry Andric //
18330b57cec5SDimitry Andric // >>>> Interference before def.
18340b57cec5SDimitry Andric // |---o---o---| Live-through, stack-in.
18350b57cec5SDimitry Andric // ____========= Enter IntvOut before first use.
18360b57cec5SDimitry Andric //
18370b57cec5SDimitry Andric selectIntv(IntvOut);
18380b57cec5SDimitry Andric SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
18390b57cec5SDimitry Andric useIntv(Idx, Stop);
18400b57cec5SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
18410b57cec5SDimitry Andric return;
18420b57cec5SDimitry Andric }
18430b57cec5SDimitry Andric
18440b57cec5SDimitry Andric // The interference is overlapping somewhere we wanted to use IntvOut. That
18450b57cec5SDimitry Andric // means we need to create a local interval that can be allocated a
18460b57cec5SDimitry Andric // different register.
18470b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ", interference overlaps uses.\n");
18480b57cec5SDimitry Andric //
18490b57cec5SDimitry Andric // >>>>>>> Interference overlapping uses.
18500b57cec5SDimitry Andric // |---o---o---| Live-through, stack-in.
18510b57cec5SDimitry Andric // ____---====== Create local interval for interference range.
18520b57cec5SDimitry Andric //
18530b57cec5SDimitry Andric selectIntv(IntvOut);
18540b57cec5SDimitry Andric SlotIndex Idx = enterIntvAfter(EnterAfter);
18550b57cec5SDimitry Andric useIntv(Idx, Stop);
18560b57cec5SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
18570b57cec5SDimitry Andric
18580b57cec5SDimitry Andric openIntv();
18590b57cec5SDimitry Andric SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
18600b57cec5SDimitry Andric useIntv(From, Idx);
18610b57cec5SDimitry Andric }
1862*5f7ddb14SDimitry Andric
print(raw_ostream & OS) const1863*5f7ddb14SDimitry Andric void SplitAnalysis::BlockInfo::print(raw_ostream &OS) const {
1864*5f7ddb14SDimitry Andric OS << "{" << printMBBReference(*MBB) << ", "
1865*5f7ddb14SDimitry Andric << "uses " << FirstInstr << " to " << LastInstr << ", "
1866*5f7ddb14SDimitry Andric << "1st def " << FirstDef << ", "
1867*5f7ddb14SDimitry Andric << (LiveIn ? "live in" : "dead in") << ", "
1868*5f7ddb14SDimitry Andric << (LiveOut ? "live out" : "dead out") << "}";
1869*5f7ddb14SDimitry Andric }
1870*5f7ddb14SDimitry Andric
dump() const1871*5f7ddb14SDimitry Andric void SplitAnalysis::BlockInfo::dump() const {
1872*5f7ddb14SDimitry Andric print(dbgs());
1873*5f7ddb14SDimitry Andric dbgs() << "\n";
1874*5f7ddb14SDimitry Andric }
1875