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