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