10b57cec5SDimitry Andric //===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===// 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 // Collect the sequence of machine instructions for a basic block. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 14bdd1243dSDimitry Andric #include "llvm/ADT/STLExtras.h" 1506c3fb27SDimitry Andric #include "llvm/ADT/StringExtras.h" 160b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h" 1781ad6265SDimitry Andric #include "llvm/CodeGen/LivePhysRegs.h" 180b57cec5SDimitry Andric #include "llvm/CodeGen/LiveVariables.h" 190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h" 200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 2206c3fb27SDimitry Andric #include "llvm/CodeGen/MachineJumpTableInfo.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/SlotIndexes.h" 260b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 27fe6060f1SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 280b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 290b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 300b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h" 310b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 320b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 330b57cec5SDimitry Andric #include "llvm/IR/ModuleSlotTracker.h" 340b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h" 350b57cec5SDimitry Andric #include "llvm/MC/MCContext.h" 360b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 370b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 380b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 390b57cec5SDimitry Andric #include <algorithm> 40bdd1243dSDimitry Andric #include <cmath> 410b57cec5SDimitry Andric using namespace llvm; 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric #define DEBUG_TYPE "codegen" 440b57cec5SDimitry Andric 458bcb0991SDimitry Andric static cl::opt<bool> PrintSlotIndexes( 468bcb0991SDimitry Andric "print-slotindexes", 478bcb0991SDimitry Andric cl::desc("When printing machine IR, annotate instructions and blocks with " 488bcb0991SDimitry Andric "SlotIndexes when available"), 498bcb0991SDimitry Andric cl::init(true), cl::Hidden); 508bcb0991SDimitry Andric 510b57cec5SDimitry Andric MachineBasicBlock::MachineBasicBlock(MachineFunction &MF, const BasicBlock *B) 520b57cec5SDimitry Andric : BB(B), Number(-1), xParent(&MF) { 530b57cec5SDimitry Andric Insts.Parent = this; 540b57cec5SDimitry Andric if (B) 550b57cec5SDimitry Andric IrrLoopHeaderWeight = B->getIrrLoopHeaderWeight(); 560b57cec5SDimitry Andric } 570b57cec5SDimitry Andric 5881ad6265SDimitry Andric MachineBasicBlock::~MachineBasicBlock() = default; 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric /// Return the MCSymbol for this basic block. 610b57cec5SDimitry Andric MCSymbol *MachineBasicBlock::getSymbol() const { 620b57cec5SDimitry Andric if (!CachedMCSymbol) { 630b57cec5SDimitry Andric const MachineFunction *MF = getParent(); 640b57cec5SDimitry Andric MCContext &Ctx = MF->getContext(); 655ffd83dbSDimitry Andric 66e8d8bef9SDimitry Andric // We emit a non-temporary symbol -- with a descriptive name -- if it begins 67e8d8bef9SDimitry Andric // a section (with basic block sections). Otherwise we fall back to use temp 68e8d8bef9SDimitry Andric // label. 69e8d8bef9SDimitry Andric if (MF->hasBBSections() && isBeginSection()) { 705ffd83dbSDimitry Andric SmallString<5> Suffix; 715ffd83dbSDimitry Andric if (SectionID == MBBSectionID::ColdSectionID) { 725ffd83dbSDimitry Andric Suffix += ".cold"; 735ffd83dbSDimitry Andric } else if (SectionID == MBBSectionID::ExceptionSectionID) { 745ffd83dbSDimitry Andric Suffix += ".eh"; 755ffd83dbSDimitry Andric } else { 76e8d8bef9SDimitry Andric // For symbols that represent basic block sections, we add ".__part." to 77e8d8bef9SDimitry Andric // allow tools like symbolizers to know that this represents a part of 78e8d8bef9SDimitry Andric // the original function. 79e8d8bef9SDimitry Andric Suffix = (Suffix + Twine(".__part.") + Twine(SectionID.Number)).str(); 805ffd83dbSDimitry Andric } 815ffd83dbSDimitry Andric CachedMCSymbol = Ctx.getOrCreateSymbol(MF->getName() + Suffix); 825ffd83dbSDimitry Andric } else { 83*0fca6ea1SDimitry Andric // If the block occurs as label in inline assembly, parsing the assembly 84*0fca6ea1SDimitry Andric // needs an actual label name => set AlwaysEmit in these cases. 85*0fca6ea1SDimitry Andric CachedMCSymbol = Ctx.createBlockSymbol( 86*0fca6ea1SDimitry Andric "BB" + Twine(MF->getFunctionNumber()) + "_" + Twine(getNumber()), 87*0fca6ea1SDimitry Andric /*AlwaysEmit=*/hasLabelMustBeEmitted()); 880b57cec5SDimitry Andric } 895ffd83dbSDimitry Andric } 900b57cec5SDimitry Andric return CachedMCSymbol; 910b57cec5SDimitry Andric } 920b57cec5SDimitry Andric 93fe6060f1SDimitry Andric MCSymbol *MachineBasicBlock::getEHCatchretSymbol() const { 94fe6060f1SDimitry Andric if (!CachedEHCatchretMCSymbol) { 95fe6060f1SDimitry Andric const MachineFunction *MF = getParent(); 96fe6060f1SDimitry Andric SmallString<128> SymbolName; 97fe6060f1SDimitry Andric raw_svector_ostream(SymbolName) 98fe6060f1SDimitry Andric << "$ehgcr_" << MF->getFunctionNumber() << '_' << getNumber(); 99fe6060f1SDimitry Andric CachedEHCatchretMCSymbol = MF->getContext().getOrCreateSymbol(SymbolName); 100fe6060f1SDimitry Andric } 101fe6060f1SDimitry Andric return CachedEHCatchretMCSymbol; 102fe6060f1SDimitry Andric } 103fe6060f1SDimitry Andric 104e8d8bef9SDimitry Andric MCSymbol *MachineBasicBlock::getEndSymbol() const { 105e8d8bef9SDimitry Andric if (!CachedEndMCSymbol) { 106e8d8bef9SDimitry Andric const MachineFunction *MF = getParent(); 107e8d8bef9SDimitry Andric MCContext &Ctx = MF->getContext(); 108*0fca6ea1SDimitry Andric CachedEndMCSymbol = Ctx.createBlockSymbol( 109*0fca6ea1SDimitry Andric "BB_END" + Twine(MF->getFunctionNumber()) + "_" + Twine(getNumber()), 110*0fca6ea1SDimitry Andric /*AlwaysEmit=*/false); 111e8d8bef9SDimitry Andric } 112e8d8bef9SDimitry Andric return CachedEndMCSymbol; 113e8d8bef9SDimitry Andric } 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) { 1160b57cec5SDimitry Andric MBB.print(OS); 1170b57cec5SDimitry Andric return OS; 1180b57cec5SDimitry Andric } 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric Printable llvm::printMBBReference(const MachineBasicBlock &MBB) { 1210b57cec5SDimitry Andric return Printable([&MBB](raw_ostream &OS) { return MBB.printAsOperand(OS); }); 1220b57cec5SDimitry Andric } 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric /// When an MBB is added to an MF, we need to update the parent pointer of the 1250b57cec5SDimitry Andric /// MBB, the MBB numbering, and any instructions in the MBB to be on the right 1260b57cec5SDimitry Andric /// operand list for registers. 1270b57cec5SDimitry Andric /// 1280b57cec5SDimitry Andric /// MBBs start out as #-1. When a MBB is added to a MachineFunction, it 1290b57cec5SDimitry Andric /// gets the next available unique MBB number. If it is removed from a 1300b57cec5SDimitry Andric /// MachineFunction, it goes back to being #-1. 1310b57cec5SDimitry Andric void ilist_callback_traits<MachineBasicBlock>::addNodeToList( 1320b57cec5SDimitry Andric MachineBasicBlock *N) { 1330b57cec5SDimitry Andric MachineFunction &MF = *N->getParent(); 1340b57cec5SDimitry Andric N->Number = MF.addToMBBNumbering(N); 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric // Make sure the instructions have their operands in the reginfo lists. 1370b57cec5SDimitry Andric MachineRegisterInfo &RegInfo = MF.getRegInfo(); 138349cc55cSDimitry Andric for (MachineInstr &MI : N->instrs()) 13981ad6265SDimitry Andric MI.addRegOperandsToUseLists(RegInfo); 1400b57cec5SDimitry Andric } 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric void ilist_callback_traits<MachineBasicBlock>::removeNodeFromList( 1430b57cec5SDimitry Andric MachineBasicBlock *N) { 1440b57cec5SDimitry Andric N->getParent()->removeFromMBBNumbering(N->Number); 1450b57cec5SDimitry Andric N->Number = -1; 1460b57cec5SDimitry Andric } 1470b57cec5SDimitry Andric 1480b57cec5SDimitry Andric /// When we add an instruction to a basic block list, we update its parent 1490b57cec5SDimitry Andric /// pointer and add its operands from reg use/def lists if appropriate. 1500b57cec5SDimitry Andric void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) { 1510b57cec5SDimitry Andric assert(!N->getParent() && "machine instruction already in a basic block"); 1520b57cec5SDimitry Andric N->setParent(Parent); 1530b57cec5SDimitry Andric 1540b57cec5SDimitry Andric // Add the instruction's register operands to their corresponding 1550b57cec5SDimitry Andric // use/def lists. 1560b57cec5SDimitry Andric MachineFunction *MF = Parent->getParent(); 15781ad6265SDimitry Andric N->addRegOperandsToUseLists(MF->getRegInfo()); 1580b57cec5SDimitry Andric MF->handleInsertion(*N); 1590b57cec5SDimitry Andric } 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric /// When we remove an instruction from a basic block list, we update its parent 1620b57cec5SDimitry Andric /// pointer and remove its operands from reg use/def lists if appropriate. 1630b57cec5SDimitry Andric void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) { 1640b57cec5SDimitry Andric assert(N->getParent() && "machine instruction not in a basic block"); 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric // Remove from the use/def lists. 1670b57cec5SDimitry Andric if (MachineFunction *MF = N->getMF()) { 1680b57cec5SDimitry Andric MF->handleRemoval(*N); 16981ad6265SDimitry Andric N->removeRegOperandsFromUseLists(MF->getRegInfo()); 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric N->setParent(nullptr); 1730b57cec5SDimitry Andric } 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andric /// When moving a range of instructions from one MBB list to another, we need to 1760b57cec5SDimitry Andric /// update the parent pointers and the use/def lists. 1770b57cec5SDimitry Andric void ilist_traits<MachineInstr>::transferNodesFromList(ilist_traits &FromList, 1780b57cec5SDimitry Andric instr_iterator First, 1790b57cec5SDimitry Andric instr_iterator Last) { 1800b57cec5SDimitry Andric assert(Parent->getParent() == FromList.Parent->getParent() && 1810b57cec5SDimitry Andric "cannot transfer MachineInstrs between MachineFunctions"); 1820b57cec5SDimitry Andric 1830b57cec5SDimitry Andric // If it's within the same BB, there's nothing to do. 1840b57cec5SDimitry Andric if (this == &FromList) 1850b57cec5SDimitry Andric return; 1860b57cec5SDimitry Andric 1870b57cec5SDimitry Andric assert(Parent != FromList.Parent && "Two lists have the same parent?"); 1880b57cec5SDimitry Andric 1890b57cec5SDimitry Andric // If splicing between two blocks within the same function, just update the 1900b57cec5SDimitry Andric // parent pointers. 1910b57cec5SDimitry Andric for (; First != Last; ++First) 1920b57cec5SDimitry Andric First->setParent(Parent); 1930b57cec5SDimitry Andric } 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric void ilist_traits<MachineInstr>::deleteNode(MachineInstr *MI) { 1960b57cec5SDimitry Andric assert(!MI->getParent() && "MI is still in a block!"); 1970eae32dcSDimitry Andric Parent->getParent()->deleteMachineInstr(MI); 1980b57cec5SDimitry Andric } 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric MachineBasicBlock::iterator MachineBasicBlock::getFirstNonPHI() { 2010b57cec5SDimitry Andric instr_iterator I = instr_begin(), E = instr_end(); 2020b57cec5SDimitry Andric while (I != E && I->isPHI()) 2030b57cec5SDimitry Andric ++I; 2040b57cec5SDimitry Andric assert((I == E || !I->isInsideBundle()) && 2050b57cec5SDimitry Andric "First non-phi MI cannot be inside a bundle!"); 2060b57cec5SDimitry Andric return I; 2070b57cec5SDimitry Andric } 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric MachineBasicBlock::iterator 2100b57cec5SDimitry Andric MachineBasicBlock::SkipPHIsAndLabels(MachineBasicBlock::iterator I) { 2110b57cec5SDimitry Andric const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo(); 2120b57cec5SDimitry Andric 2130b57cec5SDimitry Andric iterator E = end(); 2140b57cec5SDimitry Andric while (I != E && (I->isPHI() || I->isPosition() || 2150b57cec5SDimitry Andric TII->isBasicBlockPrologue(*I))) 2160b57cec5SDimitry Andric ++I; 2170b57cec5SDimitry Andric // FIXME: This needs to change if we wish to bundle labels 2180b57cec5SDimitry Andric // inside the bundle. 2190b57cec5SDimitry Andric assert((I == E || !I->isInsideBundle()) && 2200b57cec5SDimitry Andric "First non-phi / non-label instruction is inside a bundle!"); 2210b57cec5SDimitry Andric return I; 2220b57cec5SDimitry Andric } 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric MachineBasicBlock::iterator 225fe6060f1SDimitry Andric MachineBasicBlock::SkipPHIsLabelsAndDebug(MachineBasicBlock::iterator I, 2265f757f3fSDimitry Andric Register Reg, bool SkipPseudoOp) { 2270b57cec5SDimitry Andric const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo(); 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric iterator E = end(); 2300b57cec5SDimitry Andric while (I != E && (I->isPHI() || I->isPosition() || I->isDebugInstr() || 231fe6060f1SDimitry Andric (SkipPseudoOp && I->isPseudoProbe()) || 2325f757f3fSDimitry Andric TII->isBasicBlockPrologue(*I, Reg))) 2330b57cec5SDimitry Andric ++I; 2340b57cec5SDimitry Andric // FIXME: This needs to change if we wish to bundle labels / dbg_values 2350b57cec5SDimitry Andric // inside the bundle. 2360b57cec5SDimitry Andric assert((I == E || !I->isInsideBundle()) && 2370b57cec5SDimitry Andric "First non-phi / non-label / non-debug " 2380b57cec5SDimitry Andric "instruction is inside a bundle!"); 2390b57cec5SDimitry Andric return I; 2400b57cec5SDimitry Andric } 2410b57cec5SDimitry Andric 2420b57cec5SDimitry Andric MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { 2430b57cec5SDimitry Andric iterator B = begin(), E = end(), I = E; 2440b57cec5SDimitry Andric while (I != B && ((--I)->isTerminator() || I->isDebugInstr())) 2450b57cec5SDimitry Andric ; /*noop */ 2460b57cec5SDimitry Andric while (I != E && !I->isTerminator()) 2470b57cec5SDimitry Andric ++I; 2480b57cec5SDimitry Andric return I; 2490b57cec5SDimitry Andric } 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric MachineBasicBlock::instr_iterator MachineBasicBlock::getFirstInstrTerminator() { 2520b57cec5SDimitry Andric instr_iterator B = instr_begin(), E = instr_end(), I = E; 2530b57cec5SDimitry Andric while (I != B && ((--I)->isTerminator() || I->isDebugInstr())) 2540b57cec5SDimitry Andric ; /*noop */ 2550b57cec5SDimitry Andric while (I != E && !I->isTerminator()) 2560b57cec5SDimitry Andric ++I; 2570b57cec5SDimitry Andric return I; 2580b57cec5SDimitry Andric } 2590b57cec5SDimitry Andric 260bdd1243dSDimitry Andric MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminatorForward() { 261bdd1243dSDimitry Andric return find_if(instrs(), [](auto &II) { return II.isTerminator(); }); 262bdd1243dSDimitry Andric } 263bdd1243dSDimitry Andric 264fe6060f1SDimitry Andric MachineBasicBlock::iterator 265fe6060f1SDimitry Andric MachineBasicBlock::getFirstNonDebugInstr(bool SkipPseudoOp) { 2660b57cec5SDimitry Andric // Skip over begin-of-block dbg_value instructions. 267fe6060f1SDimitry Andric return skipDebugInstructionsForward(begin(), end(), SkipPseudoOp); 2680b57cec5SDimitry Andric } 2690b57cec5SDimitry Andric 270fe6060f1SDimitry Andric MachineBasicBlock::iterator 271fe6060f1SDimitry Andric MachineBasicBlock::getLastNonDebugInstr(bool SkipPseudoOp) { 2720b57cec5SDimitry Andric // Skip over end-of-block dbg_value instructions. 2730b57cec5SDimitry Andric instr_iterator B = instr_begin(), I = instr_end(); 2740b57cec5SDimitry Andric while (I != B) { 2750b57cec5SDimitry Andric --I; 2760b57cec5SDimitry Andric // Return instruction that starts a bundle. 2770b57cec5SDimitry Andric if (I->isDebugInstr() || I->isInsideBundle()) 2780b57cec5SDimitry Andric continue; 279fe6060f1SDimitry Andric if (SkipPseudoOp && I->isPseudoProbe()) 280fe6060f1SDimitry Andric continue; 2810b57cec5SDimitry Andric return I; 2820b57cec5SDimitry Andric } 2830b57cec5SDimitry Andric // The block is all debug values. 2840b57cec5SDimitry Andric return end(); 2850b57cec5SDimitry Andric } 2860b57cec5SDimitry Andric 2870b57cec5SDimitry Andric bool MachineBasicBlock::hasEHPadSuccessor() const { 288349cc55cSDimitry Andric for (const MachineBasicBlock *Succ : successors()) 289349cc55cSDimitry Andric if (Succ->isEHPad()) 2900b57cec5SDimitry Andric return true; 2910b57cec5SDimitry Andric return false; 2920b57cec5SDimitry Andric } 2930b57cec5SDimitry Andric 294e8d8bef9SDimitry Andric bool MachineBasicBlock::isEntryBlock() const { 295e8d8bef9SDimitry Andric return getParent()->begin() == getIterator(); 296e8d8bef9SDimitry Andric } 297e8d8bef9SDimitry Andric 2980b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 2990b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineBasicBlock::dump() const { 3000b57cec5SDimitry Andric print(dbgs()); 3010b57cec5SDimitry Andric } 3020b57cec5SDimitry Andric #endif 3030b57cec5SDimitry Andric 3045ffd83dbSDimitry Andric bool MachineBasicBlock::mayHaveInlineAsmBr() const { 3055ffd83dbSDimitry Andric for (const MachineBasicBlock *Succ : successors()) { 3065ffd83dbSDimitry Andric if (Succ->isInlineAsmBrIndirectTarget()) 3075ffd83dbSDimitry Andric return true; 3085ffd83dbSDimitry Andric } 3095ffd83dbSDimitry Andric return false; 3105ffd83dbSDimitry Andric } 3115ffd83dbSDimitry Andric 3120b57cec5SDimitry Andric bool MachineBasicBlock::isLegalToHoistInto() const { 3135ffd83dbSDimitry Andric if (isReturnBlock() || hasEHPadSuccessor() || mayHaveInlineAsmBr()) 3140b57cec5SDimitry Andric return false; 3150b57cec5SDimitry Andric return true; 3160b57cec5SDimitry Andric } 3170b57cec5SDimitry Andric 318*0fca6ea1SDimitry Andric bool MachineBasicBlock::hasName() const { 319*0fca6ea1SDimitry Andric if (const BasicBlock *LBB = getBasicBlock()) 320*0fca6ea1SDimitry Andric return LBB->hasName(); 321*0fca6ea1SDimitry Andric return false; 322*0fca6ea1SDimitry Andric } 323*0fca6ea1SDimitry Andric 3240b57cec5SDimitry Andric StringRef MachineBasicBlock::getName() const { 3250b57cec5SDimitry Andric if (const BasicBlock *LBB = getBasicBlock()) 3260b57cec5SDimitry Andric return LBB->getName(); 3270b57cec5SDimitry Andric else 3280b57cec5SDimitry Andric return StringRef("", 0); 3290b57cec5SDimitry Andric } 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric /// Return a hopefully unique identifier for this block. 3320b57cec5SDimitry Andric std::string MachineBasicBlock::getFullName() const { 3330b57cec5SDimitry Andric std::string Name; 3340b57cec5SDimitry Andric if (getParent()) 3350b57cec5SDimitry Andric Name = (getParent()->getName() + ":").str(); 3360b57cec5SDimitry Andric if (getBasicBlock()) 3370b57cec5SDimitry Andric Name += getBasicBlock()->getName(); 3380b57cec5SDimitry Andric else 3390b57cec5SDimitry Andric Name += ("BB" + Twine(getNumber())).str(); 3400b57cec5SDimitry Andric return Name; 3410b57cec5SDimitry Andric } 3420b57cec5SDimitry Andric 3430b57cec5SDimitry Andric void MachineBasicBlock::print(raw_ostream &OS, const SlotIndexes *Indexes, 3440b57cec5SDimitry Andric bool IsStandalone) const { 3450b57cec5SDimitry Andric const MachineFunction *MF = getParent(); 3460b57cec5SDimitry Andric if (!MF) { 3470b57cec5SDimitry Andric OS << "Can't print out MachineBasicBlock because parent MachineFunction" 3480b57cec5SDimitry Andric << " is null\n"; 3490b57cec5SDimitry Andric return; 3500b57cec5SDimitry Andric } 3510b57cec5SDimitry Andric const Function &F = MF->getFunction(); 3520b57cec5SDimitry Andric const Module *M = F.getParent(); 3530b57cec5SDimitry Andric ModuleSlotTracker MST(M); 3540b57cec5SDimitry Andric MST.incorporateFunction(F); 3550b57cec5SDimitry Andric print(OS, MST, Indexes, IsStandalone); 3560b57cec5SDimitry Andric } 3570b57cec5SDimitry Andric 3580b57cec5SDimitry Andric void MachineBasicBlock::print(raw_ostream &OS, ModuleSlotTracker &MST, 3590b57cec5SDimitry Andric const SlotIndexes *Indexes, 3600b57cec5SDimitry Andric bool IsStandalone) const { 3610b57cec5SDimitry Andric const MachineFunction *MF = getParent(); 3620b57cec5SDimitry Andric if (!MF) { 3630b57cec5SDimitry Andric OS << "Can't print out MachineBasicBlock because parent MachineFunction" 3640b57cec5SDimitry Andric << " is null\n"; 3650b57cec5SDimitry Andric return; 3660b57cec5SDimitry Andric } 3670b57cec5SDimitry Andric 3688bcb0991SDimitry Andric if (Indexes && PrintSlotIndexes) 3690b57cec5SDimitry Andric OS << Indexes->getMBBStartIdx(this) << '\t'; 3700b57cec5SDimitry Andric 371e8d8bef9SDimitry Andric printName(OS, PrintNameIr | PrintNameAttributes, &MST); 3720b57cec5SDimitry Andric OS << ":\n"; 3730b57cec5SDimitry Andric 3740b57cec5SDimitry Andric const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); 3750b57cec5SDimitry Andric const MachineRegisterInfo &MRI = MF->getRegInfo(); 3760b57cec5SDimitry Andric const TargetInstrInfo &TII = *getParent()->getSubtarget().getInstrInfo(); 3770b57cec5SDimitry Andric bool HasLineAttributes = false; 3780b57cec5SDimitry Andric 3790b57cec5SDimitry Andric // Print the preds of this block according to the CFG. 3800b57cec5SDimitry Andric if (!pred_empty() && IsStandalone) { 3810b57cec5SDimitry Andric if (Indexes) OS << '\t'; 3820b57cec5SDimitry Andric // Don't indent(2), align with previous line attributes. 3830b57cec5SDimitry Andric OS << "; predecessors: "; 384e8d8bef9SDimitry Andric ListSeparator LS; 385e8d8bef9SDimitry Andric for (auto *Pred : predecessors()) 386e8d8bef9SDimitry Andric OS << LS << printMBBReference(*Pred); 3870b57cec5SDimitry Andric OS << '\n'; 3880b57cec5SDimitry Andric HasLineAttributes = true; 3890b57cec5SDimitry Andric } 3900b57cec5SDimitry Andric 3910b57cec5SDimitry Andric if (!succ_empty()) { 3920b57cec5SDimitry Andric if (Indexes) OS << '\t'; 3930b57cec5SDimitry Andric // Print the successors 3940b57cec5SDimitry Andric OS.indent(2) << "successors: "; 395e8d8bef9SDimitry Andric ListSeparator LS; 3960b57cec5SDimitry Andric for (auto I = succ_begin(), E = succ_end(); I != E; ++I) { 397e8d8bef9SDimitry Andric OS << LS << printMBBReference(**I); 3980b57cec5SDimitry Andric if (!Probs.empty()) 3990b57cec5SDimitry Andric OS << '(' 4000b57cec5SDimitry Andric << format("0x%08" PRIx32, getSuccProbability(I).getNumerator()) 4010b57cec5SDimitry Andric << ')'; 4020b57cec5SDimitry Andric } 4030b57cec5SDimitry Andric if (!Probs.empty() && IsStandalone) { 4040b57cec5SDimitry Andric // Print human readable probabilities as comments. 4050b57cec5SDimitry Andric OS << "; "; 406e8d8bef9SDimitry Andric ListSeparator LS; 4070b57cec5SDimitry Andric for (auto I = succ_begin(), E = succ_end(); I != E; ++I) { 4080b57cec5SDimitry Andric const BranchProbability &BP = getSuccProbability(I); 409e8d8bef9SDimitry Andric OS << LS << printMBBReference(**I) << '(' 4100b57cec5SDimitry Andric << format("%.2f%%", 4110b57cec5SDimitry Andric rint(((double)BP.getNumerator() / BP.getDenominator()) * 4120b57cec5SDimitry Andric 100.0 * 100.0) / 4130b57cec5SDimitry Andric 100.0) 4140b57cec5SDimitry Andric << ')'; 4150b57cec5SDimitry Andric } 4160b57cec5SDimitry Andric } 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andric OS << '\n'; 4190b57cec5SDimitry Andric HasLineAttributes = true; 4200b57cec5SDimitry Andric } 4210b57cec5SDimitry Andric 4220b57cec5SDimitry Andric if (!livein_empty() && MRI.tracksLiveness()) { 4230b57cec5SDimitry Andric if (Indexes) OS << '\t'; 4240b57cec5SDimitry Andric OS.indent(2) << "liveins: "; 4250b57cec5SDimitry Andric 426e8d8bef9SDimitry Andric ListSeparator LS; 4270b57cec5SDimitry Andric for (const auto &LI : liveins()) { 428e8d8bef9SDimitry Andric OS << LS << printReg(LI.PhysReg, TRI); 4290b57cec5SDimitry Andric if (!LI.LaneMask.all()) 4300b57cec5SDimitry Andric OS << ":0x" << PrintLaneMask(LI.LaneMask); 4310b57cec5SDimitry Andric } 4320b57cec5SDimitry Andric HasLineAttributes = true; 4330b57cec5SDimitry Andric } 4340b57cec5SDimitry Andric 4350b57cec5SDimitry Andric if (HasLineAttributes) 4360b57cec5SDimitry Andric OS << '\n'; 4370b57cec5SDimitry Andric 4380b57cec5SDimitry Andric bool IsInBundle = false; 4390b57cec5SDimitry Andric for (const MachineInstr &MI : instrs()) { 4408bcb0991SDimitry Andric if (Indexes && PrintSlotIndexes) { 4410b57cec5SDimitry Andric if (Indexes->hasIndex(MI)) 4420b57cec5SDimitry Andric OS << Indexes->getInstructionIndex(MI); 4430b57cec5SDimitry Andric OS << '\t'; 4440b57cec5SDimitry Andric } 4450b57cec5SDimitry Andric 4460b57cec5SDimitry Andric if (IsInBundle && !MI.isInsideBundle()) { 4470b57cec5SDimitry Andric OS.indent(2) << "}\n"; 4480b57cec5SDimitry Andric IsInBundle = false; 4490b57cec5SDimitry Andric } 4500b57cec5SDimitry Andric 4510b57cec5SDimitry Andric OS.indent(IsInBundle ? 4 : 2); 4520b57cec5SDimitry Andric MI.print(OS, MST, IsStandalone, /*SkipOpers=*/false, /*SkipDebugLoc=*/false, 4530b57cec5SDimitry Andric /*AddNewLine=*/false, &TII); 4540b57cec5SDimitry Andric 4550b57cec5SDimitry Andric if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) { 4560b57cec5SDimitry Andric OS << " {"; 4570b57cec5SDimitry Andric IsInBundle = true; 4580b57cec5SDimitry Andric } 4590b57cec5SDimitry Andric OS << '\n'; 4600b57cec5SDimitry Andric } 4610b57cec5SDimitry Andric 4620b57cec5SDimitry Andric if (IsInBundle) 4630b57cec5SDimitry Andric OS.indent(2) << "}\n"; 4640b57cec5SDimitry Andric 4650b57cec5SDimitry Andric if (IrrLoopHeaderWeight && IsStandalone) { 4660b57cec5SDimitry Andric if (Indexes) OS << '\t'; 467bdd1243dSDimitry Andric OS.indent(2) << "; Irreducible loop header weight: " << *IrrLoopHeaderWeight 468bdd1243dSDimitry Andric << '\n'; 4690b57cec5SDimitry Andric } 4700b57cec5SDimitry Andric } 4710b57cec5SDimitry Andric 472e8d8bef9SDimitry Andric /// Print the basic block's name as: 473e8d8bef9SDimitry Andric /// 474e8d8bef9SDimitry Andric /// bb.{number}[.{ir-name}] [(attributes...)] 475e8d8bef9SDimitry Andric /// 476e8d8bef9SDimitry Andric /// The {ir-name} is only printed when the \ref PrintNameIr flag is passed 477e8d8bef9SDimitry Andric /// (which is the default). If the IR block has no name, it is identified 478e8d8bef9SDimitry Andric /// numerically using the attribute syntax as "(%ir-block.{ir-slot})". 479e8d8bef9SDimitry Andric /// 480e8d8bef9SDimitry Andric /// When the \ref PrintNameAttributes flag is passed, additional attributes 481e8d8bef9SDimitry Andric /// of the block are printed when set. 482e8d8bef9SDimitry Andric /// 483e8d8bef9SDimitry Andric /// \param printNameFlags Combination of \ref PrintNameFlag flags indicating 484e8d8bef9SDimitry Andric /// the parts to print. 485e8d8bef9SDimitry Andric /// \param moduleSlotTracker Optional ModuleSlotTracker. This method will 486e8d8bef9SDimitry Andric /// incorporate its own tracker when necessary to 487e8d8bef9SDimitry Andric /// determine the block's IR name. 488e8d8bef9SDimitry Andric void MachineBasicBlock::printName(raw_ostream &os, unsigned printNameFlags, 489e8d8bef9SDimitry Andric ModuleSlotTracker *moduleSlotTracker) const { 490e8d8bef9SDimitry Andric os << "bb." << getNumber(); 491e8d8bef9SDimitry Andric bool hasAttributes = false; 492e8d8bef9SDimitry Andric 493bdd1243dSDimitry Andric auto PrintBBRef = [&](const BasicBlock *bb) { 494bdd1243dSDimitry Andric os << "%ir-block."; 495e8d8bef9SDimitry Andric if (bb->hasName()) { 496bdd1243dSDimitry Andric os << bb->getName(); 497e8d8bef9SDimitry Andric } else { 498e8d8bef9SDimitry Andric int slot = -1; 499e8d8bef9SDimitry Andric 500e8d8bef9SDimitry Andric if (moduleSlotTracker) { 501e8d8bef9SDimitry Andric slot = moduleSlotTracker->getLocalSlot(bb); 502e8d8bef9SDimitry Andric } else if (bb->getParent()) { 503e8d8bef9SDimitry Andric ModuleSlotTracker tmpTracker(bb->getModule(), false); 504e8d8bef9SDimitry Andric tmpTracker.incorporateFunction(*bb->getParent()); 505e8d8bef9SDimitry Andric slot = tmpTracker.getLocalSlot(bb); 506e8d8bef9SDimitry Andric } 507e8d8bef9SDimitry Andric 508e8d8bef9SDimitry Andric if (slot == -1) 509e8d8bef9SDimitry Andric os << "<ir-block badref>"; 510e8d8bef9SDimitry Andric else 511bdd1243dSDimitry Andric os << slot; 512bdd1243dSDimitry Andric } 513bdd1243dSDimitry Andric }; 514bdd1243dSDimitry Andric 515bdd1243dSDimitry Andric if (printNameFlags & PrintNameIr) { 516bdd1243dSDimitry Andric if (const auto *bb = getBasicBlock()) { 517bdd1243dSDimitry Andric if (bb->hasName()) { 518bdd1243dSDimitry Andric os << '.' << bb->getName(); 519bdd1243dSDimitry Andric } else { 520bdd1243dSDimitry Andric hasAttributes = true; 521bdd1243dSDimitry Andric os << " ("; 522bdd1243dSDimitry Andric PrintBBRef(bb); 523e8d8bef9SDimitry Andric } 524e8d8bef9SDimitry Andric } 525e8d8bef9SDimitry Andric } 526e8d8bef9SDimitry Andric 527e8d8bef9SDimitry Andric if (printNameFlags & PrintNameAttributes) { 528bdd1243dSDimitry Andric if (isMachineBlockAddressTaken()) { 529e8d8bef9SDimitry Andric os << (hasAttributes ? ", " : " ("); 530bdd1243dSDimitry Andric os << "machine-block-address-taken"; 531bdd1243dSDimitry Andric hasAttributes = true; 532bdd1243dSDimitry Andric } 533bdd1243dSDimitry Andric if (isIRBlockAddressTaken()) { 534bdd1243dSDimitry Andric os << (hasAttributes ? ", " : " ("); 535bdd1243dSDimitry Andric os << "ir-block-address-taken "; 536bdd1243dSDimitry Andric PrintBBRef(getAddressTakenIRBlock()); 537e8d8bef9SDimitry Andric hasAttributes = true; 538e8d8bef9SDimitry Andric } 539e8d8bef9SDimitry Andric if (isEHPad()) { 540e8d8bef9SDimitry Andric os << (hasAttributes ? ", " : " ("); 541e8d8bef9SDimitry Andric os << "landing-pad"; 542e8d8bef9SDimitry Andric hasAttributes = true; 543e8d8bef9SDimitry Andric } 544349cc55cSDimitry Andric if (isInlineAsmBrIndirectTarget()) { 545349cc55cSDimitry Andric os << (hasAttributes ? ", " : " ("); 546349cc55cSDimitry Andric os << "inlineasm-br-indirect-target"; 547349cc55cSDimitry Andric hasAttributes = true; 548349cc55cSDimitry Andric } 549e8d8bef9SDimitry Andric if (isEHFuncletEntry()) { 550e8d8bef9SDimitry Andric os << (hasAttributes ? ", " : " ("); 551e8d8bef9SDimitry Andric os << "ehfunclet-entry"; 552e8d8bef9SDimitry Andric hasAttributes = true; 553e8d8bef9SDimitry Andric } 554e8d8bef9SDimitry Andric if (getAlignment() != Align(1)) { 555e8d8bef9SDimitry Andric os << (hasAttributes ? ", " : " ("); 556e8d8bef9SDimitry Andric os << "align " << getAlignment().value(); 557e8d8bef9SDimitry Andric hasAttributes = true; 558e8d8bef9SDimitry Andric } 559e8d8bef9SDimitry Andric if (getSectionID() != MBBSectionID(0)) { 560e8d8bef9SDimitry Andric os << (hasAttributes ? ", " : " ("); 561e8d8bef9SDimitry Andric os << "bbsections "; 562e8d8bef9SDimitry Andric switch (getSectionID().Type) { 563e8d8bef9SDimitry Andric case MBBSectionID::SectionType::Exception: 564e8d8bef9SDimitry Andric os << "Exception"; 565e8d8bef9SDimitry Andric break; 566e8d8bef9SDimitry Andric case MBBSectionID::SectionType::Cold: 567e8d8bef9SDimitry Andric os << "Cold"; 568e8d8bef9SDimitry Andric break; 569e8d8bef9SDimitry Andric default: 570e8d8bef9SDimitry Andric os << getSectionID().Number; 571e8d8bef9SDimitry Andric } 572e8d8bef9SDimitry Andric hasAttributes = true; 573e8d8bef9SDimitry Andric } 574bdd1243dSDimitry Andric if (getBBID().has_value()) { 575bdd1243dSDimitry Andric os << (hasAttributes ? ", " : " ("); 5765f757f3fSDimitry Andric os << "bb_id " << getBBID()->BaseID; 5775f757f3fSDimitry Andric if (getBBID()->CloneID != 0) 5785f757f3fSDimitry Andric os << " " << getBBID()->CloneID; 5795f757f3fSDimitry Andric hasAttributes = true; 5805f757f3fSDimitry Andric } 5815f757f3fSDimitry Andric if (CallFrameSize != 0) { 5825f757f3fSDimitry Andric os << (hasAttributes ? ", " : " ("); 5835f757f3fSDimitry Andric os << "call-frame-size " << CallFrameSize; 584bdd1243dSDimitry Andric hasAttributes = true; 585bdd1243dSDimitry Andric } 586e8d8bef9SDimitry Andric } 587e8d8bef9SDimitry Andric 588e8d8bef9SDimitry Andric if (hasAttributes) 589e8d8bef9SDimitry Andric os << ')'; 590e8d8bef9SDimitry Andric } 591e8d8bef9SDimitry Andric 5920b57cec5SDimitry Andric void MachineBasicBlock::printAsOperand(raw_ostream &OS, 5930b57cec5SDimitry Andric bool /*PrintType*/) const { 594e8d8bef9SDimitry Andric OS << '%'; 595e8d8bef9SDimitry Andric printName(OS, 0); 5960b57cec5SDimitry Andric } 5970b57cec5SDimitry Andric 5980b57cec5SDimitry Andric void MachineBasicBlock::removeLiveIn(MCPhysReg Reg, LaneBitmask LaneMask) { 5990b57cec5SDimitry Andric LiveInVector::iterator I = find_if( 6000b57cec5SDimitry Andric LiveIns, [Reg](const RegisterMaskPair &LI) { return LI.PhysReg == Reg; }); 6010b57cec5SDimitry Andric if (I == LiveIns.end()) 6020b57cec5SDimitry Andric return; 6030b57cec5SDimitry Andric 6040b57cec5SDimitry Andric I->LaneMask &= ~LaneMask; 6050b57cec5SDimitry Andric if (I->LaneMask.none()) 6060b57cec5SDimitry Andric LiveIns.erase(I); 6070b57cec5SDimitry Andric } 6080b57cec5SDimitry Andric 6090b57cec5SDimitry Andric MachineBasicBlock::livein_iterator 6100b57cec5SDimitry Andric MachineBasicBlock::removeLiveIn(MachineBasicBlock::livein_iterator I) { 6110b57cec5SDimitry Andric // Get non-const version of iterator. 6120b57cec5SDimitry Andric LiveInVector::iterator LI = LiveIns.begin() + (I - LiveIns.begin()); 6130b57cec5SDimitry Andric return LiveIns.erase(LI); 6140b57cec5SDimitry Andric } 6150b57cec5SDimitry Andric 6160b57cec5SDimitry Andric bool MachineBasicBlock::isLiveIn(MCPhysReg Reg, LaneBitmask LaneMask) const { 6170b57cec5SDimitry Andric livein_iterator I = find_if( 6180b57cec5SDimitry Andric LiveIns, [Reg](const RegisterMaskPair &LI) { return LI.PhysReg == Reg; }); 6190b57cec5SDimitry Andric return I != livein_end() && (I->LaneMask & LaneMask).any(); 6200b57cec5SDimitry Andric } 6210b57cec5SDimitry Andric 6220b57cec5SDimitry Andric void MachineBasicBlock::sortUniqueLiveIns() { 6230b57cec5SDimitry Andric llvm::sort(LiveIns, 6240b57cec5SDimitry Andric [](const RegisterMaskPair &LI0, const RegisterMaskPair &LI1) { 6250b57cec5SDimitry Andric return LI0.PhysReg < LI1.PhysReg; 6260b57cec5SDimitry Andric }); 6270b57cec5SDimitry Andric // Liveins are sorted by physreg now we can merge their lanemasks. 6280b57cec5SDimitry Andric LiveInVector::const_iterator I = LiveIns.begin(); 6290b57cec5SDimitry Andric LiveInVector::const_iterator J; 6300b57cec5SDimitry Andric LiveInVector::iterator Out = LiveIns.begin(); 6310b57cec5SDimitry Andric for (; I != LiveIns.end(); ++Out, I = J) { 6325ffd83dbSDimitry Andric MCRegister PhysReg = I->PhysReg; 6330b57cec5SDimitry Andric LaneBitmask LaneMask = I->LaneMask; 6340b57cec5SDimitry Andric for (J = std::next(I); J != LiveIns.end() && J->PhysReg == PhysReg; ++J) 6350b57cec5SDimitry Andric LaneMask |= J->LaneMask; 6360b57cec5SDimitry Andric Out->PhysReg = PhysReg; 6370b57cec5SDimitry Andric Out->LaneMask = LaneMask; 6380b57cec5SDimitry Andric } 6390b57cec5SDimitry Andric LiveIns.erase(Out, LiveIns.end()); 6400b57cec5SDimitry Andric } 6410b57cec5SDimitry Andric 6425ffd83dbSDimitry Andric Register 6438bcb0991SDimitry Andric MachineBasicBlock::addLiveIn(MCRegister PhysReg, const TargetRegisterClass *RC) { 6440b57cec5SDimitry Andric assert(getParent() && "MBB must be inserted in function"); 645e8d8bef9SDimitry Andric assert(Register::isPhysicalRegister(PhysReg) && "Expected physreg"); 6460b57cec5SDimitry Andric assert(RC && "Register class is required"); 6470b57cec5SDimitry Andric assert((isEHPad() || this == &getParent()->front()) && 6480b57cec5SDimitry Andric "Only the entry block and landing pads can have physreg live ins"); 6490b57cec5SDimitry Andric 6500b57cec5SDimitry Andric bool LiveIn = isLiveIn(PhysReg); 6510b57cec5SDimitry Andric iterator I = SkipPHIsAndLabels(begin()), E = end(); 6520b57cec5SDimitry Andric MachineRegisterInfo &MRI = getParent()->getRegInfo(); 6530b57cec5SDimitry Andric const TargetInstrInfo &TII = *getParent()->getSubtarget().getInstrInfo(); 6540b57cec5SDimitry Andric 6550b57cec5SDimitry Andric // Look for an existing copy. 6560b57cec5SDimitry Andric if (LiveIn) 6570b57cec5SDimitry Andric for (;I != E && I->isCopy(); ++I) 6580b57cec5SDimitry Andric if (I->getOperand(1).getReg() == PhysReg) { 6598bcb0991SDimitry Andric Register VirtReg = I->getOperand(0).getReg(); 6600b57cec5SDimitry Andric if (!MRI.constrainRegClass(VirtReg, RC)) 6610b57cec5SDimitry Andric llvm_unreachable("Incompatible live-in register class."); 6620b57cec5SDimitry Andric return VirtReg; 6630b57cec5SDimitry Andric } 6640b57cec5SDimitry Andric 6650b57cec5SDimitry Andric // No luck, create a virtual register. 6668bcb0991SDimitry Andric Register VirtReg = MRI.createVirtualRegister(RC); 6670b57cec5SDimitry Andric BuildMI(*this, I, DebugLoc(), TII.get(TargetOpcode::COPY), VirtReg) 6680b57cec5SDimitry Andric .addReg(PhysReg, RegState::Kill); 6690b57cec5SDimitry Andric if (!LiveIn) 6700b57cec5SDimitry Andric addLiveIn(PhysReg); 6710b57cec5SDimitry Andric return VirtReg; 6720b57cec5SDimitry Andric } 6730b57cec5SDimitry Andric 6740b57cec5SDimitry Andric void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) { 6750b57cec5SDimitry Andric getParent()->splice(NewAfter->getIterator(), getIterator()); 6760b57cec5SDimitry Andric } 6770b57cec5SDimitry Andric 6780b57cec5SDimitry Andric void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) { 6790b57cec5SDimitry Andric getParent()->splice(++NewBefore->getIterator(), getIterator()); 6800b57cec5SDimitry Andric } 6810b57cec5SDimitry Andric 68206c3fb27SDimitry Andric static int findJumpTableIndex(const MachineBasicBlock &MBB) { 68306c3fb27SDimitry Andric MachineBasicBlock::const_iterator TerminatorI = MBB.getFirstTerminator(); 68406c3fb27SDimitry Andric if (TerminatorI == MBB.end()) 68506c3fb27SDimitry Andric return -1; 68606c3fb27SDimitry Andric const MachineInstr &Terminator = *TerminatorI; 68706c3fb27SDimitry Andric const TargetInstrInfo *TII = MBB.getParent()->getSubtarget().getInstrInfo(); 68806c3fb27SDimitry Andric return TII->getJumpTableIndex(Terminator); 68906c3fb27SDimitry Andric } 69006c3fb27SDimitry Andric 6915ffd83dbSDimitry Andric void MachineBasicBlock::updateTerminator( 6925ffd83dbSDimitry Andric MachineBasicBlock *PreviousLayoutSuccessor) { 6935ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Updating terminators on " << printMBBReference(*this) 6945ffd83dbSDimitry Andric << "\n"); 6955ffd83dbSDimitry Andric 6960b57cec5SDimitry Andric const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo(); 6970b57cec5SDimitry Andric // A block with no successors has no concerns with fall-through edges. 6980b57cec5SDimitry Andric if (this->succ_empty()) 6990b57cec5SDimitry Andric return; 7000b57cec5SDimitry Andric 7010b57cec5SDimitry Andric MachineBasicBlock *TBB = nullptr, *FBB = nullptr; 7020b57cec5SDimitry Andric SmallVector<MachineOperand, 4> Cond; 7030b57cec5SDimitry Andric DebugLoc DL = findBranchDebugLoc(); 7040b57cec5SDimitry Andric bool B = TII->analyzeBranch(*this, TBB, FBB, Cond); 7050b57cec5SDimitry Andric (void) B; 7060b57cec5SDimitry Andric assert(!B && "UpdateTerminators requires analyzable predecessors!"); 7070b57cec5SDimitry Andric if (Cond.empty()) { 7080b57cec5SDimitry Andric if (TBB) { 7090b57cec5SDimitry Andric // The block has an unconditional branch. If its successor is now its 7100b57cec5SDimitry Andric // layout successor, delete the branch. 7110b57cec5SDimitry Andric if (isLayoutSuccessor(TBB)) 7120b57cec5SDimitry Andric TII->removeBranch(*this); 7130b57cec5SDimitry Andric } else { 7145ffd83dbSDimitry Andric // The block has an unconditional fallthrough, or the end of the block is 7155ffd83dbSDimitry Andric // unreachable. 7160b57cec5SDimitry Andric 7175ffd83dbSDimitry Andric // Unfortunately, whether the end of the block is unreachable is not 7185ffd83dbSDimitry Andric // immediately obvious; we must fall back to checking the successor list, 7195ffd83dbSDimitry Andric // and assuming that if the passed in block is in the succesor list and 7205ffd83dbSDimitry Andric // not an EHPad, it must be the intended target. 7215ffd83dbSDimitry Andric if (!PreviousLayoutSuccessor || !isSuccessor(PreviousLayoutSuccessor) || 7225ffd83dbSDimitry Andric PreviousLayoutSuccessor->isEHPad()) 7230b57cec5SDimitry Andric return; 7240b57cec5SDimitry Andric 7255ffd83dbSDimitry Andric // If the unconditional successor block is not the current layout 7265ffd83dbSDimitry Andric // successor, insert a branch to jump to it. 7275ffd83dbSDimitry Andric if (!isLayoutSuccessor(PreviousLayoutSuccessor)) 7285ffd83dbSDimitry Andric TII->insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL); 7290b57cec5SDimitry Andric } 7300b57cec5SDimitry Andric return; 7310b57cec5SDimitry Andric } 7320b57cec5SDimitry Andric 7330b57cec5SDimitry Andric if (FBB) { 7340b57cec5SDimitry Andric // The block has a non-fallthrough conditional branch. If one of its 7350b57cec5SDimitry Andric // successors is its layout successor, rewrite it to a fallthrough 7360b57cec5SDimitry Andric // conditional branch. 7370b57cec5SDimitry Andric if (isLayoutSuccessor(TBB)) { 7380b57cec5SDimitry Andric if (TII->reverseBranchCondition(Cond)) 7390b57cec5SDimitry Andric return; 7400b57cec5SDimitry Andric TII->removeBranch(*this); 7410b57cec5SDimitry Andric TII->insertBranch(*this, FBB, nullptr, Cond, DL); 7420b57cec5SDimitry Andric } else if (isLayoutSuccessor(FBB)) { 7430b57cec5SDimitry Andric TII->removeBranch(*this); 7440b57cec5SDimitry Andric TII->insertBranch(*this, TBB, nullptr, Cond, DL); 7450b57cec5SDimitry Andric } 7460b57cec5SDimitry Andric return; 7470b57cec5SDimitry Andric } 7480b57cec5SDimitry Andric 7495ffd83dbSDimitry Andric // We now know we're going to fallthrough to PreviousLayoutSuccessor. 7505ffd83dbSDimitry Andric assert(PreviousLayoutSuccessor); 7515ffd83dbSDimitry Andric assert(!PreviousLayoutSuccessor->isEHPad()); 7525ffd83dbSDimitry Andric assert(isSuccessor(PreviousLayoutSuccessor)); 7530b57cec5SDimitry Andric 7545ffd83dbSDimitry Andric if (PreviousLayoutSuccessor == TBB) { 7555ffd83dbSDimitry Andric // We had a fallthrough to the same basic block as the conditional jump 7565ffd83dbSDimitry Andric // targets. Remove the conditional jump, leaving an unconditional 7575ffd83dbSDimitry Andric // fallthrough or an unconditional jump. 7580b57cec5SDimitry Andric TII->removeBranch(*this); 7595ffd83dbSDimitry Andric if (!isLayoutSuccessor(TBB)) { 7600b57cec5SDimitry Andric Cond.clear(); 7610b57cec5SDimitry Andric TII->insertBranch(*this, TBB, nullptr, Cond, DL); 7625ffd83dbSDimitry Andric } 7630b57cec5SDimitry Andric return; 7640b57cec5SDimitry Andric } 7650b57cec5SDimitry Andric 7660b57cec5SDimitry Andric // The block has a fallthrough conditional branch. 7670b57cec5SDimitry Andric if (isLayoutSuccessor(TBB)) { 7680b57cec5SDimitry Andric if (TII->reverseBranchCondition(Cond)) { 7690b57cec5SDimitry Andric // We can't reverse the condition, add an unconditional branch. 7700b57cec5SDimitry Andric Cond.clear(); 7715ffd83dbSDimitry Andric TII->insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL); 7720b57cec5SDimitry Andric return; 7730b57cec5SDimitry Andric } 7740b57cec5SDimitry Andric TII->removeBranch(*this); 7755ffd83dbSDimitry Andric TII->insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL); 7765ffd83dbSDimitry Andric } else if (!isLayoutSuccessor(PreviousLayoutSuccessor)) { 7770b57cec5SDimitry Andric TII->removeBranch(*this); 7785ffd83dbSDimitry Andric TII->insertBranch(*this, TBB, PreviousLayoutSuccessor, Cond, DL); 7790b57cec5SDimitry Andric } 7800b57cec5SDimitry Andric } 7810b57cec5SDimitry Andric 7820b57cec5SDimitry Andric void MachineBasicBlock::validateSuccProbs() const { 7830b57cec5SDimitry Andric #ifndef NDEBUG 7840b57cec5SDimitry Andric int64_t Sum = 0; 7850b57cec5SDimitry Andric for (auto Prob : Probs) 7860b57cec5SDimitry Andric Sum += Prob.getNumerator(); 7870b57cec5SDimitry Andric // Due to precision issue, we assume that the sum of probabilities is one if 7880b57cec5SDimitry Andric // the difference between the sum of their numerators and the denominator is 7890b57cec5SDimitry Andric // no greater than the number of successors. 7900b57cec5SDimitry Andric assert((uint64_t)std::abs(Sum - BranchProbability::getDenominator()) <= 7910b57cec5SDimitry Andric Probs.size() && 7920b57cec5SDimitry Andric "The sum of successors's probabilities exceeds one."); 7930b57cec5SDimitry Andric #endif // NDEBUG 7940b57cec5SDimitry Andric } 7950b57cec5SDimitry Andric 7960b57cec5SDimitry Andric void MachineBasicBlock::addSuccessor(MachineBasicBlock *Succ, 7970b57cec5SDimitry Andric BranchProbability Prob) { 7980b57cec5SDimitry Andric // Probability list is either empty (if successor list isn't empty, this means 7990b57cec5SDimitry Andric // disabled optimization) or has the same size as successor list. 8000b57cec5SDimitry Andric if (!(Probs.empty() && !Successors.empty())) 8010b57cec5SDimitry Andric Probs.push_back(Prob); 8020b57cec5SDimitry Andric Successors.push_back(Succ); 8030b57cec5SDimitry Andric Succ->addPredecessor(this); 8040b57cec5SDimitry Andric } 8050b57cec5SDimitry Andric 8060b57cec5SDimitry Andric void MachineBasicBlock::addSuccessorWithoutProb(MachineBasicBlock *Succ) { 8070b57cec5SDimitry Andric // We need to make sure probability list is either empty or has the same size 8080b57cec5SDimitry Andric // of successor list. When this function is called, we can safely delete all 8090b57cec5SDimitry Andric // probability in the list. 8100b57cec5SDimitry Andric Probs.clear(); 8110b57cec5SDimitry Andric Successors.push_back(Succ); 8120b57cec5SDimitry Andric Succ->addPredecessor(this); 8130b57cec5SDimitry Andric } 8140b57cec5SDimitry Andric 8150b57cec5SDimitry Andric void MachineBasicBlock::splitSuccessor(MachineBasicBlock *Old, 8160b57cec5SDimitry Andric MachineBasicBlock *New, 8170b57cec5SDimitry Andric bool NormalizeSuccProbs) { 8180b57cec5SDimitry Andric succ_iterator OldI = llvm::find(successors(), Old); 8190b57cec5SDimitry Andric assert(OldI != succ_end() && "Old is not a successor of this block!"); 820e8d8bef9SDimitry Andric assert(!llvm::is_contained(successors(), New) && 8210b57cec5SDimitry Andric "New is already a successor of this block!"); 8220b57cec5SDimitry Andric 8230b57cec5SDimitry Andric // Add a new successor with equal probability as the original one. Note 8240b57cec5SDimitry Andric // that we directly copy the probability using the iterator rather than 8250b57cec5SDimitry Andric // getting a potentially synthetic probability computed when unknown. This 8260b57cec5SDimitry Andric // preserves the probabilities as-is and then we can renormalize them and 8270b57cec5SDimitry Andric // query them effectively afterward. 8280b57cec5SDimitry Andric addSuccessor(New, Probs.empty() ? BranchProbability::getUnknown() 8290b57cec5SDimitry Andric : *getProbabilityIterator(OldI)); 8300b57cec5SDimitry Andric if (NormalizeSuccProbs) 8310b57cec5SDimitry Andric normalizeSuccProbs(); 8320b57cec5SDimitry Andric } 8330b57cec5SDimitry Andric 8340b57cec5SDimitry Andric void MachineBasicBlock::removeSuccessor(MachineBasicBlock *Succ, 8350b57cec5SDimitry Andric bool NormalizeSuccProbs) { 8360b57cec5SDimitry Andric succ_iterator I = find(Successors, Succ); 8370b57cec5SDimitry Andric removeSuccessor(I, NormalizeSuccProbs); 8380b57cec5SDimitry Andric } 8390b57cec5SDimitry Andric 8400b57cec5SDimitry Andric MachineBasicBlock::succ_iterator 8410b57cec5SDimitry Andric MachineBasicBlock::removeSuccessor(succ_iterator I, bool NormalizeSuccProbs) { 8420b57cec5SDimitry Andric assert(I != Successors.end() && "Not a current successor!"); 8430b57cec5SDimitry Andric 8440b57cec5SDimitry Andric // If probability list is empty it means we don't use it (disabled 8450b57cec5SDimitry Andric // optimization). 8460b57cec5SDimitry Andric if (!Probs.empty()) { 8470b57cec5SDimitry Andric probability_iterator WI = getProbabilityIterator(I); 8480b57cec5SDimitry Andric Probs.erase(WI); 8490b57cec5SDimitry Andric if (NormalizeSuccProbs) 8500b57cec5SDimitry Andric normalizeSuccProbs(); 8510b57cec5SDimitry Andric } 8520b57cec5SDimitry Andric 8530b57cec5SDimitry Andric (*I)->removePredecessor(this); 8540b57cec5SDimitry Andric return Successors.erase(I); 8550b57cec5SDimitry Andric } 8560b57cec5SDimitry Andric 8570b57cec5SDimitry Andric void MachineBasicBlock::replaceSuccessor(MachineBasicBlock *Old, 8580b57cec5SDimitry Andric MachineBasicBlock *New) { 8590b57cec5SDimitry Andric if (Old == New) 8600b57cec5SDimitry Andric return; 8610b57cec5SDimitry Andric 8620b57cec5SDimitry Andric succ_iterator E = succ_end(); 8630b57cec5SDimitry Andric succ_iterator NewI = E; 8640b57cec5SDimitry Andric succ_iterator OldI = E; 8650b57cec5SDimitry Andric for (succ_iterator I = succ_begin(); I != E; ++I) { 8660b57cec5SDimitry Andric if (*I == Old) { 8670b57cec5SDimitry Andric OldI = I; 8680b57cec5SDimitry Andric if (NewI != E) 8690b57cec5SDimitry Andric break; 8700b57cec5SDimitry Andric } 8710b57cec5SDimitry Andric if (*I == New) { 8720b57cec5SDimitry Andric NewI = I; 8730b57cec5SDimitry Andric if (OldI != E) 8740b57cec5SDimitry Andric break; 8750b57cec5SDimitry Andric } 8760b57cec5SDimitry Andric } 8770b57cec5SDimitry Andric assert(OldI != E && "Old is not a successor of this block"); 8780b57cec5SDimitry Andric 8790b57cec5SDimitry Andric // If New isn't already a successor, let it take Old's place. 8800b57cec5SDimitry Andric if (NewI == E) { 8810b57cec5SDimitry Andric Old->removePredecessor(this); 8820b57cec5SDimitry Andric New->addPredecessor(this); 8830b57cec5SDimitry Andric *OldI = New; 8840b57cec5SDimitry Andric return; 8850b57cec5SDimitry Andric } 8860b57cec5SDimitry Andric 8870b57cec5SDimitry Andric // New is already a successor. 8880b57cec5SDimitry Andric // Update its probability instead of adding a duplicate edge. 8890b57cec5SDimitry Andric if (!Probs.empty()) { 8900b57cec5SDimitry Andric auto ProbIter = getProbabilityIterator(NewI); 8910b57cec5SDimitry Andric if (!ProbIter->isUnknown()) 8920b57cec5SDimitry Andric *ProbIter += *getProbabilityIterator(OldI); 8930b57cec5SDimitry Andric } 8940b57cec5SDimitry Andric removeSuccessor(OldI); 8950b57cec5SDimitry Andric } 8960b57cec5SDimitry Andric 8975f757f3fSDimitry Andric void MachineBasicBlock::copySuccessor(const MachineBasicBlock *Orig, 8980b57cec5SDimitry Andric succ_iterator I) { 899e8d8bef9SDimitry Andric if (!Orig->Probs.empty()) 9000b57cec5SDimitry Andric addSuccessor(*I, Orig->getSuccProbability(I)); 9010b57cec5SDimitry Andric else 9020b57cec5SDimitry Andric addSuccessorWithoutProb(*I); 9030b57cec5SDimitry Andric } 9040b57cec5SDimitry Andric 9050b57cec5SDimitry Andric void MachineBasicBlock::addPredecessor(MachineBasicBlock *Pred) { 9060b57cec5SDimitry Andric Predecessors.push_back(Pred); 9070b57cec5SDimitry Andric } 9080b57cec5SDimitry Andric 9090b57cec5SDimitry Andric void MachineBasicBlock::removePredecessor(MachineBasicBlock *Pred) { 9100b57cec5SDimitry Andric pred_iterator I = find(Predecessors, Pred); 9110b57cec5SDimitry Andric assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); 9120b57cec5SDimitry Andric Predecessors.erase(I); 9130b57cec5SDimitry Andric } 9140b57cec5SDimitry Andric 9150b57cec5SDimitry Andric void MachineBasicBlock::transferSuccessors(MachineBasicBlock *FromMBB) { 9160b57cec5SDimitry Andric if (this == FromMBB) 9170b57cec5SDimitry Andric return; 9180b57cec5SDimitry Andric 9190b57cec5SDimitry Andric while (!FromMBB->succ_empty()) { 9200b57cec5SDimitry Andric MachineBasicBlock *Succ = *FromMBB->succ_begin(); 9210b57cec5SDimitry Andric 9228bcb0991SDimitry Andric // If probability list is empty it means we don't use it (disabled 9238bcb0991SDimitry Andric // optimization). 9240b57cec5SDimitry Andric if (!FromMBB->Probs.empty()) { 9250b57cec5SDimitry Andric auto Prob = *FromMBB->Probs.begin(); 9260b57cec5SDimitry Andric addSuccessor(Succ, Prob); 9270b57cec5SDimitry Andric } else 9280b57cec5SDimitry Andric addSuccessorWithoutProb(Succ); 9290b57cec5SDimitry Andric 9300b57cec5SDimitry Andric FromMBB->removeSuccessor(Succ); 9310b57cec5SDimitry Andric } 9320b57cec5SDimitry Andric } 9330b57cec5SDimitry Andric 9340b57cec5SDimitry Andric void 9350b57cec5SDimitry Andric MachineBasicBlock::transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB) { 9360b57cec5SDimitry Andric if (this == FromMBB) 9370b57cec5SDimitry Andric return; 9380b57cec5SDimitry Andric 9390b57cec5SDimitry Andric while (!FromMBB->succ_empty()) { 9400b57cec5SDimitry Andric MachineBasicBlock *Succ = *FromMBB->succ_begin(); 9410b57cec5SDimitry Andric if (!FromMBB->Probs.empty()) { 9420b57cec5SDimitry Andric auto Prob = *FromMBB->Probs.begin(); 9430b57cec5SDimitry Andric addSuccessor(Succ, Prob); 9440b57cec5SDimitry Andric } else 9450b57cec5SDimitry Andric addSuccessorWithoutProb(Succ); 9460b57cec5SDimitry Andric FromMBB->removeSuccessor(Succ); 9470b57cec5SDimitry Andric 9480b57cec5SDimitry Andric // Fix up any PHI nodes in the successor. 9498bcb0991SDimitry Andric Succ->replacePhiUsesWith(FromMBB, this); 9500b57cec5SDimitry Andric } 9510b57cec5SDimitry Andric normalizeSuccProbs(); 9520b57cec5SDimitry Andric } 9530b57cec5SDimitry Andric 9540b57cec5SDimitry Andric bool MachineBasicBlock::isPredecessor(const MachineBasicBlock *MBB) const { 9550b57cec5SDimitry Andric return is_contained(predecessors(), MBB); 9560b57cec5SDimitry Andric } 9570b57cec5SDimitry Andric 9580b57cec5SDimitry Andric bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const { 9590b57cec5SDimitry Andric return is_contained(successors(), MBB); 9600b57cec5SDimitry Andric } 9610b57cec5SDimitry Andric 9620b57cec5SDimitry Andric bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const { 9630b57cec5SDimitry Andric MachineFunction::const_iterator I(this); 9640b57cec5SDimitry Andric return std::next(I) == MachineFunction::const_iterator(MBB); 9650b57cec5SDimitry Andric } 9660b57cec5SDimitry Andric 96781ad6265SDimitry Andric const MachineBasicBlock *MachineBasicBlock::getSingleSuccessor() const { 96881ad6265SDimitry Andric return Successors.size() == 1 ? Successors[0] : nullptr; 96981ad6265SDimitry Andric } 97081ad6265SDimitry Andric 9715f757f3fSDimitry Andric const MachineBasicBlock *MachineBasicBlock::getSinglePredecessor() const { 9725f757f3fSDimitry Andric return Predecessors.size() == 1 ? Predecessors[0] : nullptr; 9735f757f3fSDimitry Andric } 9745f757f3fSDimitry Andric 975bdd1243dSDimitry Andric MachineBasicBlock *MachineBasicBlock::getFallThrough(bool JumpToFallThrough) { 9760b57cec5SDimitry Andric MachineFunction::iterator Fallthrough = getIterator(); 9770b57cec5SDimitry Andric ++Fallthrough; 9780b57cec5SDimitry Andric // If FallthroughBlock is off the end of the function, it can't fall through. 9790b57cec5SDimitry Andric if (Fallthrough == getParent()->end()) 9800b57cec5SDimitry Andric return nullptr; 9810b57cec5SDimitry Andric 9820b57cec5SDimitry Andric // If FallthroughBlock isn't a successor, no fallthrough is possible. 9830b57cec5SDimitry Andric if (!isSuccessor(&*Fallthrough)) 9840b57cec5SDimitry Andric return nullptr; 9850b57cec5SDimitry Andric 9860b57cec5SDimitry Andric // Analyze the branches, if any, at the end of the block. 9870b57cec5SDimitry Andric MachineBasicBlock *TBB = nullptr, *FBB = nullptr; 9880b57cec5SDimitry Andric SmallVector<MachineOperand, 4> Cond; 9890b57cec5SDimitry Andric const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo(); 9900b57cec5SDimitry Andric if (TII->analyzeBranch(*this, TBB, FBB, Cond)) { 9910b57cec5SDimitry Andric // If we couldn't analyze the branch, examine the last instruction. 9920b57cec5SDimitry Andric // If the block doesn't end in a known control barrier, assume fallthrough 9930b57cec5SDimitry Andric // is possible. The isPredicated check is needed because this code can be 9940b57cec5SDimitry Andric // called during IfConversion, where an instruction which is normally a 9950b57cec5SDimitry Andric // Barrier is predicated and thus no longer an actual control barrier. 9960b57cec5SDimitry Andric return (empty() || !back().isBarrier() || TII->isPredicated(back())) 9970b57cec5SDimitry Andric ? &*Fallthrough 9980b57cec5SDimitry Andric : nullptr; 9990b57cec5SDimitry Andric } 10000b57cec5SDimitry Andric 10010b57cec5SDimitry Andric // If there is no branch, control always falls through. 10020b57cec5SDimitry Andric if (!TBB) return &*Fallthrough; 10030b57cec5SDimitry Andric 10040b57cec5SDimitry Andric // If there is some explicit branch to the fallthrough block, it can obviously 10050b57cec5SDimitry Andric // reach, even though the branch should get folded to fall through implicitly. 100606c3fb27SDimitry Andric if (JumpToFallThrough && (MachineFunction::iterator(TBB) == Fallthrough || 1007bdd1243dSDimitry Andric MachineFunction::iterator(FBB) == Fallthrough)) 10080b57cec5SDimitry Andric return &*Fallthrough; 10090b57cec5SDimitry Andric 10100b57cec5SDimitry Andric // If it's an unconditional branch to some block not the fall through, it 10110b57cec5SDimitry Andric // doesn't fall through. 10120b57cec5SDimitry Andric if (Cond.empty()) return nullptr; 10130b57cec5SDimitry Andric 10140b57cec5SDimitry Andric // Otherwise, if it is conditional and has no explicit false block, it falls 10150b57cec5SDimitry Andric // through. 10160b57cec5SDimitry Andric return (FBB == nullptr) ? &*Fallthrough : nullptr; 10170b57cec5SDimitry Andric } 10180b57cec5SDimitry Andric 10190b57cec5SDimitry Andric bool MachineBasicBlock::canFallThrough() { 10200b57cec5SDimitry Andric return getFallThrough() != nullptr; 10210b57cec5SDimitry Andric } 10220b57cec5SDimitry Andric 1023e8d8bef9SDimitry Andric MachineBasicBlock *MachineBasicBlock::splitAt(MachineInstr &MI, 1024e8d8bef9SDimitry Andric bool UpdateLiveIns, 1025e8d8bef9SDimitry Andric LiveIntervals *LIS) { 1026e8d8bef9SDimitry Andric MachineBasicBlock::iterator SplitPoint(&MI); 1027e8d8bef9SDimitry Andric ++SplitPoint; 1028e8d8bef9SDimitry Andric 1029e8d8bef9SDimitry Andric if (SplitPoint == end()) { 1030e8d8bef9SDimitry Andric // Don't bother with a new block. 1031e8d8bef9SDimitry Andric return this; 1032e8d8bef9SDimitry Andric } 1033e8d8bef9SDimitry Andric 1034e8d8bef9SDimitry Andric MachineFunction *MF = getParent(); 1035e8d8bef9SDimitry Andric 1036e8d8bef9SDimitry Andric LivePhysRegs LiveRegs; 1037e8d8bef9SDimitry Andric if (UpdateLiveIns) { 1038e8d8bef9SDimitry Andric // Make sure we add any physregs we define in the block as liveins to the 1039e8d8bef9SDimitry Andric // new block. 1040e8d8bef9SDimitry Andric MachineBasicBlock::iterator Prev(&MI); 1041e8d8bef9SDimitry Andric LiveRegs.init(*MF->getSubtarget().getRegisterInfo()); 1042e8d8bef9SDimitry Andric LiveRegs.addLiveOuts(*this); 1043e8d8bef9SDimitry Andric for (auto I = rbegin(), E = Prev.getReverse(); I != E; ++I) 1044e8d8bef9SDimitry Andric LiveRegs.stepBackward(*I); 1045e8d8bef9SDimitry Andric } 1046e8d8bef9SDimitry Andric 1047e8d8bef9SDimitry Andric MachineBasicBlock *SplitBB = MF->CreateMachineBasicBlock(getBasicBlock()); 1048e8d8bef9SDimitry Andric 1049e8d8bef9SDimitry Andric MF->insert(++MachineFunction::iterator(this), SplitBB); 1050e8d8bef9SDimitry Andric SplitBB->splice(SplitBB->begin(), this, SplitPoint, end()); 1051e8d8bef9SDimitry Andric 1052e8d8bef9SDimitry Andric SplitBB->transferSuccessorsAndUpdatePHIs(this); 1053e8d8bef9SDimitry Andric addSuccessor(SplitBB); 1054e8d8bef9SDimitry Andric 1055e8d8bef9SDimitry Andric if (UpdateLiveIns) 1056e8d8bef9SDimitry Andric addLiveIns(*SplitBB, LiveRegs); 1057e8d8bef9SDimitry Andric 1058e8d8bef9SDimitry Andric if (LIS) 1059e8d8bef9SDimitry Andric LIS->insertMBBInMaps(SplitBB); 1060e8d8bef9SDimitry Andric 1061e8d8bef9SDimitry Andric return SplitBB; 1062e8d8bef9SDimitry Andric } 1063e8d8bef9SDimitry Andric 106406c3fb27SDimitry Andric // Returns `true` if there are possibly other users of the jump table at 106506c3fb27SDimitry Andric // `JumpTableIndex` except for the ones in `IgnoreMBB`. 106606c3fb27SDimitry Andric static bool jumpTableHasOtherUses(const MachineFunction &MF, 106706c3fb27SDimitry Andric const MachineBasicBlock &IgnoreMBB, 106806c3fb27SDimitry Andric int JumpTableIndex) { 106906c3fb27SDimitry Andric assert(JumpTableIndex >= 0 && "need valid index"); 107006c3fb27SDimitry Andric const MachineJumpTableInfo &MJTI = *MF.getJumpTableInfo(); 107106c3fb27SDimitry Andric const MachineJumpTableEntry &MJTE = MJTI.getJumpTables()[JumpTableIndex]; 107206c3fb27SDimitry Andric // Take any basic block from the table; every user of the jump table must 107306c3fb27SDimitry Andric // show up in the predecessor list. 107406c3fb27SDimitry Andric const MachineBasicBlock *MBB = nullptr; 107506c3fb27SDimitry Andric for (MachineBasicBlock *B : MJTE.MBBs) { 107606c3fb27SDimitry Andric if (B != nullptr) { 107706c3fb27SDimitry Andric MBB = B; 107806c3fb27SDimitry Andric break; 107906c3fb27SDimitry Andric } 108006c3fb27SDimitry Andric } 108106c3fb27SDimitry Andric if (MBB == nullptr) 108206c3fb27SDimitry Andric return true; // can't rule out other users if there isn't any block. 108306c3fb27SDimitry Andric const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); 108406c3fb27SDimitry Andric SmallVector<MachineOperand, 4> Cond; 108506c3fb27SDimitry Andric for (MachineBasicBlock *Pred : MBB->predecessors()) { 108606c3fb27SDimitry Andric if (Pred == &IgnoreMBB) 108706c3fb27SDimitry Andric continue; 108806c3fb27SDimitry Andric MachineBasicBlock *DummyT = nullptr; 108906c3fb27SDimitry Andric MachineBasicBlock *DummyF = nullptr; 109006c3fb27SDimitry Andric Cond.clear(); 109106c3fb27SDimitry Andric if (!TII.analyzeBranch(*Pred, DummyT, DummyF, Cond, 109206c3fb27SDimitry Andric /*AllowModify=*/false)) { 109306c3fb27SDimitry Andric // analyzable direct jump 109406c3fb27SDimitry Andric continue; 109506c3fb27SDimitry Andric } 109606c3fb27SDimitry Andric int PredJTI = findJumpTableIndex(*Pred); 109706c3fb27SDimitry Andric if (PredJTI >= 0) { 109806c3fb27SDimitry Andric if (PredJTI == JumpTableIndex) 109906c3fb27SDimitry Andric return true; 110006c3fb27SDimitry Andric continue; 110106c3fb27SDimitry Andric } 110206c3fb27SDimitry Andric // Be conservative for unanalyzable jumps. 110306c3fb27SDimitry Andric return true; 110406c3fb27SDimitry Andric } 110506c3fb27SDimitry Andric return false; 110606c3fb27SDimitry Andric } 110706c3fb27SDimitry Andric 11085f757f3fSDimitry Andric class SlotIndexUpdateDelegate : public MachineFunction::Delegate { 11095f757f3fSDimitry Andric private: 11105f757f3fSDimitry Andric MachineFunction &MF; 11115f757f3fSDimitry Andric SlotIndexes *Indexes; 11125f757f3fSDimitry Andric SmallSetVector<MachineInstr *, 2> Insertions; 11135f757f3fSDimitry Andric 11145f757f3fSDimitry Andric public: 11155f757f3fSDimitry Andric SlotIndexUpdateDelegate(MachineFunction &MF, SlotIndexes *Indexes) 11165f757f3fSDimitry Andric : MF(MF), Indexes(Indexes) { 11175f757f3fSDimitry Andric MF.setDelegate(this); 11185f757f3fSDimitry Andric } 11195f757f3fSDimitry Andric 11205f757f3fSDimitry Andric ~SlotIndexUpdateDelegate() { 11215f757f3fSDimitry Andric MF.resetDelegate(this); 11225f757f3fSDimitry Andric for (auto MI : Insertions) 11235f757f3fSDimitry Andric Indexes->insertMachineInstrInMaps(*MI); 11245f757f3fSDimitry Andric } 11255f757f3fSDimitry Andric 11265f757f3fSDimitry Andric void MF_HandleInsertion(MachineInstr &MI) override { 11275f757f3fSDimitry Andric // This is called before MI is inserted into block so defer index update. 11285f757f3fSDimitry Andric if (Indexes) 11295f757f3fSDimitry Andric Insertions.insert(&MI); 11305f757f3fSDimitry Andric } 11315f757f3fSDimitry Andric 11325f757f3fSDimitry Andric void MF_HandleRemoval(MachineInstr &MI) override { 11335f757f3fSDimitry Andric if (Indexes && !Insertions.remove(&MI)) 11345f757f3fSDimitry Andric Indexes->removeMachineInstrFromMaps(MI); 11355f757f3fSDimitry Andric } 11365f757f3fSDimitry Andric }; 11375f757f3fSDimitry Andric 1138*0fca6ea1SDimitry Andric #define GET_RESULT(RESULT, GETTER, INFIX) \ 1139*0fca6ea1SDimitry Andric [MF, P, MFAM]() { \ 1140*0fca6ea1SDimitry Andric if (P) { \ 1141*0fca6ea1SDimitry Andric auto *Wrapper = P->getAnalysisIfAvailable<RESULT##INFIX##WrapperPass>(); \ 1142*0fca6ea1SDimitry Andric return Wrapper ? &Wrapper->GETTER() : nullptr; \ 1143*0fca6ea1SDimitry Andric } \ 1144*0fca6ea1SDimitry Andric return MFAM->getCachedResult<RESULT##Analysis>(*MF); \ 1145*0fca6ea1SDimitry Andric }() 1146*0fca6ea1SDimitry Andric 11475ffd83dbSDimitry Andric MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge( 1148*0fca6ea1SDimitry Andric MachineBasicBlock *Succ, Pass *P, MachineFunctionAnalysisManager *MFAM, 11495ffd83dbSDimitry Andric std::vector<SparseBitVector<>> *LiveInSets) { 1150*0fca6ea1SDimitry Andric assert((P || MFAM) && "Need a way to get analysis results!"); 11510b57cec5SDimitry Andric if (!canSplitCriticalEdge(Succ)) 11520b57cec5SDimitry Andric return nullptr; 11530b57cec5SDimitry Andric 11540b57cec5SDimitry Andric MachineFunction *MF = getParent(); 11555ffd83dbSDimitry Andric MachineBasicBlock *PrevFallthrough = getNextNode(); 11560b57cec5SDimitry Andric 11570b57cec5SDimitry Andric MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock(); 11585f757f3fSDimitry Andric NMBB->setCallFrameSize(Succ->getCallFrameSize()); 115906c3fb27SDimitry Andric 116006c3fb27SDimitry Andric // Is there an indirect jump with jump table? 116106c3fb27SDimitry Andric bool ChangedIndirectJump = false; 116206c3fb27SDimitry Andric int JTI = findJumpTableIndex(*this); 116306c3fb27SDimitry Andric if (JTI >= 0) { 116406c3fb27SDimitry Andric MachineJumpTableInfo &MJTI = *MF->getJumpTableInfo(); 116506c3fb27SDimitry Andric MJTI.ReplaceMBBInJumpTable(JTI, Succ, NMBB); 116606c3fb27SDimitry Andric ChangedIndirectJump = true; 116706c3fb27SDimitry Andric } 116806c3fb27SDimitry Andric 11690b57cec5SDimitry Andric MF->insert(std::next(MachineFunction::iterator(this)), NMBB); 11700b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Splitting critical edge: " << printMBBReference(*this) 11710b57cec5SDimitry Andric << " -- " << printMBBReference(*NMBB) << " -- " 11720b57cec5SDimitry Andric << printMBBReference(*Succ) << '\n'); 11730b57cec5SDimitry Andric 1174*0fca6ea1SDimitry Andric LiveIntervals *LIS = GET_RESULT(LiveIntervals, getLIS, ); 1175*0fca6ea1SDimitry Andric SlotIndexes *Indexes = GET_RESULT(SlotIndexes, getSI, ); 11760b57cec5SDimitry Andric if (LIS) 11770b57cec5SDimitry Andric LIS->insertMBBInMaps(NMBB); 11780b57cec5SDimitry Andric else if (Indexes) 11790b57cec5SDimitry Andric Indexes->insertMBBInMaps(NMBB); 11800b57cec5SDimitry Andric 11810b57cec5SDimitry Andric // On some targets like Mips, branches may kill virtual registers. Make sure 11820b57cec5SDimitry Andric // that LiveVariables is properly updated after updateTerminator replaces the 11830b57cec5SDimitry Andric // terminators. 1184*0fca6ea1SDimitry Andric LiveVariables *LV = GET_RESULT(LiveVariables, getLV, ); 11850b57cec5SDimitry Andric 11860b57cec5SDimitry Andric // Collect a list of virtual registers killed by the terminators. 11875ffd83dbSDimitry Andric SmallVector<Register, 4> KilledRegs; 11880b57cec5SDimitry Andric if (LV) 11890eae32dcSDimitry Andric for (MachineInstr &MI : 11900eae32dcSDimitry Andric llvm::make_range(getFirstInstrTerminator(), instr_end())) { 119106c3fb27SDimitry Andric for (MachineOperand &MO : MI.all_uses()) { 119206c3fb27SDimitry Andric if (MO.getReg() == 0 || !MO.isKill() || MO.isUndef()) 11930b57cec5SDimitry Andric continue; 1194349cc55cSDimitry Andric Register Reg = MO.getReg(); 1195bdd1243dSDimitry Andric if (Reg.isPhysical() || LV->getVarInfo(Reg).removeKill(MI)) { 11960b57cec5SDimitry Andric KilledRegs.push_back(Reg); 1197349cc55cSDimitry Andric LLVM_DEBUG(dbgs() << "Removing terminator kill: " << MI); 1198349cc55cSDimitry Andric MO.setIsKill(false); 11990b57cec5SDimitry Andric } 12000b57cec5SDimitry Andric } 12010b57cec5SDimitry Andric } 12020b57cec5SDimitry Andric 12035ffd83dbSDimitry Andric SmallVector<Register, 4> UsedRegs; 12040b57cec5SDimitry Andric if (LIS) { 12050eae32dcSDimitry Andric for (MachineInstr &MI : 12060eae32dcSDimitry Andric llvm::make_range(getFirstInstrTerminator(), instr_end())) { 12070eae32dcSDimitry Andric for (const MachineOperand &MO : MI.operands()) { 1208349cc55cSDimitry Andric if (!MO.isReg() || MO.getReg() == 0) 12090b57cec5SDimitry Andric continue; 12100b57cec5SDimitry Andric 1211349cc55cSDimitry Andric Register Reg = MO.getReg(); 12120b57cec5SDimitry Andric if (!is_contained(UsedRegs, Reg)) 12130b57cec5SDimitry Andric UsedRegs.push_back(Reg); 12140b57cec5SDimitry Andric } 12150b57cec5SDimitry Andric } 12160b57cec5SDimitry Andric } 12170b57cec5SDimitry Andric 12180b57cec5SDimitry Andric ReplaceUsesOfBlockWith(Succ, NMBB); 12190b57cec5SDimitry Andric 12205ffd83dbSDimitry Andric // Since we replaced all uses of Succ with NMBB, that should also be treated 12215ffd83dbSDimitry Andric // as the fallthrough successor 12225ffd83dbSDimitry Andric if (Succ == PrevFallthrough) 12235ffd83dbSDimitry Andric PrevFallthrough = NMBB; 122406c3fb27SDimitry Andric 12255f757f3fSDimitry Andric if (!ChangedIndirectJump) { 12265f757f3fSDimitry Andric SlotIndexUpdateDelegate SlotUpdater(*MF, Indexes); 12275ffd83dbSDimitry Andric updateTerminator(PrevFallthrough); 12280b57cec5SDimitry Andric } 12290b57cec5SDimitry Andric 12300b57cec5SDimitry Andric // Insert unconditional "jump Succ" instruction in NMBB if necessary. 12310b57cec5SDimitry Andric NMBB->addSuccessor(Succ); 12320b57cec5SDimitry Andric if (!NMBB->isLayoutSuccessor(Succ)) { 12335f757f3fSDimitry Andric SlotIndexUpdateDelegate SlotUpdater(*MF, Indexes); 12340b57cec5SDimitry Andric SmallVector<MachineOperand, 4> Cond; 12350b57cec5SDimitry Andric const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo(); 1236*0fca6ea1SDimitry Andric 1237*0fca6ea1SDimitry Andric // In original 'this' BB, there must be a branch instruction targeting at 1238*0fca6ea1SDimitry Andric // Succ. We can not find it out since currently getBranchDestBlock was not 1239*0fca6ea1SDimitry Andric // implemented for all targets. However, if the merged DL has column or line 1240*0fca6ea1SDimitry Andric // number, the scope and non-zero column and line number is same with that 1241*0fca6ea1SDimitry Andric // branch instruction so we can safely use it. 1242*0fca6ea1SDimitry Andric DebugLoc DL, MergedDL = findBranchDebugLoc(); 1243*0fca6ea1SDimitry Andric if (MergedDL && (MergedDL.getLine() || MergedDL.getCol())) 1244*0fca6ea1SDimitry Andric DL = MergedDL; 12450b57cec5SDimitry Andric TII->insertBranch(*NMBB, Succ, nullptr, Cond, DL); 12460b57cec5SDimitry Andric } 12470b57cec5SDimitry Andric 12488bcb0991SDimitry Andric // Fix PHI nodes in Succ so they refer to NMBB instead of this. 12498bcb0991SDimitry Andric Succ->replacePhiUsesWith(this, NMBB); 12500b57cec5SDimitry Andric 12510b57cec5SDimitry Andric // Inherit live-ins from the successor 12520b57cec5SDimitry Andric for (const auto &LI : Succ->liveins()) 12530b57cec5SDimitry Andric NMBB->addLiveIn(LI); 12540b57cec5SDimitry Andric 12550b57cec5SDimitry Andric // Update LiveVariables. 12560b57cec5SDimitry Andric const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); 12570b57cec5SDimitry Andric if (LV) { 12580b57cec5SDimitry Andric // Restore kills of virtual registers that were killed by the terminators. 12590b57cec5SDimitry Andric while (!KilledRegs.empty()) { 12605ffd83dbSDimitry Andric Register Reg = KilledRegs.pop_back_val(); 12610b57cec5SDimitry Andric for (instr_iterator I = instr_end(), E = instr_begin(); I != E;) { 12620b57cec5SDimitry Andric if (!(--I)->addRegisterKilled(Reg, TRI, /* AddIfNotFound= */ false)) 12630b57cec5SDimitry Andric continue; 1264bdd1243dSDimitry Andric if (Reg.isVirtual()) 12650b57cec5SDimitry Andric LV->getVarInfo(Reg).Kills.push_back(&*I); 12660b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Restored terminator kill: " << *I); 12670b57cec5SDimitry Andric break; 12680b57cec5SDimitry Andric } 12690b57cec5SDimitry Andric } 12700b57cec5SDimitry Andric // Update relevant live-through information. 12715ffd83dbSDimitry Andric if (LiveInSets != nullptr) 12725ffd83dbSDimitry Andric LV->addNewBlock(NMBB, this, Succ, *LiveInSets); 12735ffd83dbSDimitry Andric else 12740b57cec5SDimitry Andric LV->addNewBlock(NMBB, this, Succ); 12750b57cec5SDimitry Andric } 12760b57cec5SDimitry Andric 12770b57cec5SDimitry Andric if (LIS) { 12780b57cec5SDimitry Andric // After splitting the edge and updating SlotIndexes, live intervals may be 12790b57cec5SDimitry Andric // in one of two situations, depending on whether this block was the last in 12800b57cec5SDimitry Andric // the function. If the original block was the last in the function, all 12810b57cec5SDimitry Andric // live intervals will end prior to the beginning of the new split block. If 12820b57cec5SDimitry Andric // the original block was not at the end of the function, all live intervals 12830b57cec5SDimitry Andric // will extend to the end of the new split block. 12840b57cec5SDimitry Andric 12850b57cec5SDimitry Andric bool isLastMBB = 12860b57cec5SDimitry Andric std::next(MachineFunction::iterator(NMBB)) == getParent()->end(); 12870b57cec5SDimitry Andric 12880b57cec5SDimitry Andric SlotIndex StartIndex = Indexes->getMBBEndIdx(this); 12890b57cec5SDimitry Andric SlotIndex PrevIndex = StartIndex.getPrevSlot(); 12900b57cec5SDimitry Andric SlotIndex EndIndex = Indexes->getMBBEndIdx(NMBB); 12910b57cec5SDimitry Andric 12920b57cec5SDimitry Andric // Find the registers used from NMBB in PHIs in Succ. 12935ffd83dbSDimitry Andric SmallSet<Register, 8> PHISrcRegs; 12940b57cec5SDimitry Andric for (MachineBasicBlock::instr_iterator 12950b57cec5SDimitry Andric I = Succ->instr_begin(), E = Succ->instr_end(); 12960b57cec5SDimitry Andric I != E && I->isPHI(); ++I) { 12970b57cec5SDimitry Andric for (unsigned ni = 1, ne = I->getNumOperands(); ni != ne; ni += 2) { 12980b57cec5SDimitry Andric if (I->getOperand(ni+1).getMBB() == NMBB) { 12990b57cec5SDimitry Andric MachineOperand &MO = I->getOperand(ni); 13008bcb0991SDimitry Andric Register Reg = MO.getReg(); 13010b57cec5SDimitry Andric PHISrcRegs.insert(Reg); 13020b57cec5SDimitry Andric if (MO.isUndef()) 13030b57cec5SDimitry Andric continue; 13040b57cec5SDimitry Andric 13050b57cec5SDimitry Andric LiveInterval &LI = LIS->getInterval(Reg); 13060b57cec5SDimitry Andric VNInfo *VNI = LI.getVNInfoAt(PrevIndex); 13070b57cec5SDimitry Andric assert(VNI && 13080b57cec5SDimitry Andric "PHI sources should be live out of their predecessors."); 13090b57cec5SDimitry Andric LI.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI)); 13105f757f3fSDimitry Andric for (auto &SR : LI.subranges()) 13115f757f3fSDimitry Andric SR.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI)); 13120b57cec5SDimitry Andric } 13130b57cec5SDimitry Andric } 13140b57cec5SDimitry Andric } 13150b57cec5SDimitry Andric 13160b57cec5SDimitry Andric MachineRegisterInfo *MRI = &getParent()->getRegInfo(); 13170b57cec5SDimitry Andric for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { 13185ffd83dbSDimitry Andric Register Reg = Register::index2VirtReg(i); 13190b57cec5SDimitry Andric if (PHISrcRegs.count(Reg) || !LIS->hasInterval(Reg)) 13200b57cec5SDimitry Andric continue; 13210b57cec5SDimitry Andric 13220b57cec5SDimitry Andric LiveInterval &LI = LIS->getInterval(Reg); 13230b57cec5SDimitry Andric if (!LI.liveAt(PrevIndex)) 13240b57cec5SDimitry Andric continue; 13250b57cec5SDimitry Andric 13260b57cec5SDimitry Andric bool isLiveOut = LI.liveAt(LIS->getMBBStartIdx(Succ)); 13270b57cec5SDimitry Andric if (isLiveOut && isLastMBB) { 13280b57cec5SDimitry Andric VNInfo *VNI = LI.getVNInfoAt(PrevIndex); 13290b57cec5SDimitry Andric assert(VNI && "LiveInterval should have VNInfo where it is live."); 13300b57cec5SDimitry Andric LI.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI)); 13315f757f3fSDimitry Andric // Update subranges with live values 13325f757f3fSDimitry Andric for (auto &SR : LI.subranges()) { 13335f757f3fSDimitry Andric VNInfo *VNI = SR.getVNInfoAt(PrevIndex); 13345f757f3fSDimitry Andric if (VNI) 13355f757f3fSDimitry Andric SR.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI)); 13365f757f3fSDimitry Andric } 13370b57cec5SDimitry Andric } else if (!isLiveOut && !isLastMBB) { 13380b57cec5SDimitry Andric LI.removeSegment(StartIndex, EndIndex); 13395f757f3fSDimitry Andric for (auto &SR : LI.subranges()) 13405f757f3fSDimitry Andric SR.removeSegment(StartIndex, EndIndex); 13410b57cec5SDimitry Andric } 13420b57cec5SDimitry Andric } 13430b57cec5SDimitry Andric 13440b57cec5SDimitry Andric // Update all intervals for registers whose uses may have been modified by 13450b57cec5SDimitry Andric // updateTerminator(). 13460b57cec5SDimitry Andric LIS->repairIntervalsInRange(this, getFirstTerminator(), end(), UsedRegs); 13470b57cec5SDimitry Andric } 13480b57cec5SDimitry Andric 1349*0fca6ea1SDimitry Andric if (auto *MDT = GET_RESULT(MachineDominatorTree, getDomTree, )) 13500b57cec5SDimitry Andric MDT->recordSplitCriticalEdge(this, Succ, NMBB); 13510b57cec5SDimitry Andric 1352*0fca6ea1SDimitry Andric if (MachineLoopInfo *MLI = GET_RESULT(MachineLoop, getLI, Info)) 13530b57cec5SDimitry Andric if (MachineLoop *TIL = MLI->getLoopFor(this)) { 13540b57cec5SDimitry Andric // If one or the other blocks were not in a loop, the new block is not 13550b57cec5SDimitry Andric // either, and thus LI doesn't need to be updated. 13560b57cec5SDimitry Andric if (MachineLoop *DestLoop = MLI->getLoopFor(Succ)) { 13570b57cec5SDimitry Andric if (TIL == DestLoop) { 13580b57cec5SDimitry Andric // Both in the same loop, the NMBB joins loop. 1359*0fca6ea1SDimitry Andric DestLoop->addBasicBlockToLoop(NMBB, *MLI); 13600b57cec5SDimitry Andric } else if (TIL->contains(DestLoop)) { 13610b57cec5SDimitry Andric // Edge from an outer loop to an inner loop. Add to the outer loop. 1362*0fca6ea1SDimitry Andric TIL->addBasicBlockToLoop(NMBB, *MLI); 13630b57cec5SDimitry Andric } else if (DestLoop->contains(TIL)) { 13640b57cec5SDimitry Andric // Edge from an inner loop to an outer loop. Add to the outer loop. 1365*0fca6ea1SDimitry Andric DestLoop->addBasicBlockToLoop(NMBB, *MLI); 13660b57cec5SDimitry Andric } else { 13670b57cec5SDimitry Andric // Edge from two loops with no containment relation. Because these 13680b57cec5SDimitry Andric // are natural loops, we know that the destination block must be the 13690b57cec5SDimitry Andric // header of its loop (adding a branch into a loop elsewhere would 13700b57cec5SDimitry Andric // create an irreducible loop). 13710b57cec5SDimitry Andric assert(DestLoop->getHeader() == Succ && 13720b57cec5SDimitry Andric "Should not create irreducible loops!"); 13730b57cec5SDimitry Andric if (MachineLoop *P = DestLoop->getParentLoop()) 1374*0fca6ea1SDimitry Andric P->addBasicBlockToLoop(NMBB, *MLI); 13750b57cec5SDimitry Andric } 13760b57cec5SDimitry Andric } 13770b57cec5SDimitry Andric } 13780b57cec5SDimitry Andric 13790b57cec5SDimitry Andric return NMBB; 13800b57cec5SDimitry Andric } 13810b57cec5SDimitry Andric 13820b57cec5SDimitry Andric bool MachineBasicBlock::canSplitCriticalEdge( 13830b57cec5SDimitry Andric const MachineBasicBlock *Succ) const { 13840b57cec5SDimitry Andric // Splitting the critical edge to a landing pad block is non-trivial. Don't do 13850b57cec5SDimitry Andric // it in this generic function. 13860b57cec5SDimitry Andric if (Succ->isEHPad()) 13870b57cec5SDimitry Andric return false; 13880b57cec5SDimitry Andric 13895ffd83dbSDimitry Andric // Splitting the critical edge to a callbr's indirect block isn't advised. 13905ffd83dbSDimitry Andric // Don't do it in this generic function. 13915ffd83dbSDimitry Andric if (Succ->isInlineAsmBrIndirectTarget()) 13925ffd83dbSDimitry Andric return false; 13930b57cec5SDimitry Andric 13945ffd83dbSDimitry Andric const MachineFunction *MF = getParent(); 13950b57cec5SDimitry Andric // Performance might be harmed on HW that implements branching using exec mask 13960b57cec5SDimitry Andric // where both sides of the branches are always executed. 13970b57cec5SDimitry Andric if (MF->getTarget().requiresStructuredCFG()) 13980b57cec5SDimitry Andric return false; 13990b57cec5SDimitry Andric 140006c3fb27SDimitry Andric // Do we have an Indirect jump with a jumptable that we can rewrite? 140106c3fb27SDimitry Andric int JTI = findJumpTableIndex(*this); 140206c3fb27SDimitry Andric if (JTI >= 0 && !jumpTableHasOtherUses(*MF, *this, JTI)) 140306c3fb27SDimitry Andric return true; 140406c3fb27SDimitry Andric 14050b57cec5SDimitry Andric // We may need to update this's terminator, but we can't do that if 140606c3fb27SDimitry Andric // analyzeBranch fails. 14070b57cec5SDimitry Andric const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); 14080b57cec5SDimitry Andric MachineBasicBlock *TBB = nullptr, *FBB = nullptr; 14090b57cec5SDimitry Andric SmallVector<MachineOperand, 4> Cond; 14100b57cec5SDimitry Andric // AnalyzeBanch should modify this, since we did not allow modification. 14110b57cec5SDimitry Andric if (TII->analyzeBranch(*const_cast<MachineBasicBlock *>(this), TBB, FBB, Cond, 14120b57cec5SDimitry Andric /*AllowModify*/ false)) 14130b57cec5SDimitry Andric return false; 14140b57cec5SDimitry Andric 14150b57cec5SDimitry Andric // Avoid bugpoint weirdness: A block may end with a conditional branch but 14160b57cec5SDimitry Andric // jumps to the same MBB is either case. We have duplicate CFG edges in that 14170b57cec5SDimitry Andric // case that we can't handle. Since this never happens in properly optimized 14180b57cec5SDimitry Andric // code, just skip those edges. 14190b57cec5SDimitry Andric if (TBB && TBB == FBB) { 14200b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Won't split critical edge after degenerate " 14210b57cec5SDimitry Andric << printMBBReference(*this) << '\n'); 14220b57cec5SDimitry Andric return false; 14230b57cec5SDimitry Andric } 14240b57cec5SDimitry Andric return true; 14250b57cec5SDimitry Andric } 14260b57cec5SDimitry Andric 14270b57cec5SDimitry Andric /// Prepare MI to be removed from its bundle. This fixes bundle flags on MI's 14280b57cec5SDimitry Andric /// neighboring instructions so the bundle won't be broken by removing MI. 14290b57cec5SDimitry Andric static void unbundleSingleMI(MachineInstr *MI) { 14300b57cec5SDimitry Andric // Removing the first instruction in a bundle. 14310b57cec5SDimitry Andric if (MI->isBundledWithSucc() && !MI->isBundledWithPred()) 14320b57cec5SDimitry Andric MI->unbundleFromSucc(); 14330b57cec5SDimitry Andric // Removing the last instruction in a bundle. 14340b57cec5SDimitry Andric if (MI->isBundledWithPred() && !MI->isBundledWithSucc()) 14350b57cec5SDimitry Andric MI->unbundleFromPred(); 14360b57cec5SDimitry Andric // If MI is not bundled, or if it is internal to a bundle, the neighbor flags 14370b57cec5SDimitry Andric // are already fine. 14380b57cec5SDimitry Andric } 14390b57cec5SDimitry Andric 14400b57cec5SDimitry Andric MachineBasicBlock::instr_iterator 14410b57cec5SDimitry Andric MachineBasicBlock::erase(MachineBasicBlock::instr_iterator I) { 14420b57cec5SDimitry Andric unbundleSingleMI(&*I); 14430b57cec5SDimitry Andric return Insts.erase(I); 14440b57cec5SDimitry Andric } 14450b57cec5SDimitry Andric 14460b57cec5SDimitry Andric MachineInstr *MachineBasicBlock::remove_instr(MachineInstr *MI) { 14470b57cec5SDimitry Andric unbundleSingleMI(MI); 14480b57cec5SDimitry Andric MI->clearFlag(MachineInstr::BundledPred); 14490b57cec5SDimitry Andric MI->clearFlag(MachineInstr::BundledSucc); 14500b57cec5SDimitry Andric return Insts.remove(MI); 14510b57cec5SDimitry Andric } 14520b57cec5SDimitry Andric 14530b57cec5SDimitry Andric MachineBasicBlock::instr_iterator 14540b57cec5SDimitry Andric MachineBasicBlock::insert(instr_iterator I, MachineInstr *MI) { 14550b57cec5SDimitry Andric assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() && 14560b57cec5SDimitry Andric "Cannot insert instruction with bundle flags"); 14570b57cec5SDimitry Andric // Set the bundle flags when inserting inside a bundle. 14580b57cec5SDimitry Andric if (I != instr_end() && I->isBundledWithPred()) { 14590b57cec5SDimitry Andric MI->setFlag(MachineInstr::BundledPred); 14600b57cec5SDimitry Andric MI->setFlag(MachineInstr::BundledSucc); 14610b57cec5SDimitry Andric } 14620b57cec5SDimitry Andric return Insts.insert(I, MI); 14630b57cec5SDimitry Andric } 14640b57cec5SDimitry Andric 14650b57cec5SDimitry Andric /// This method unlinks 'this' from the containing function, and returns it, but 14660b57cec5SDimitry Andric /// does not delete it. 14670b57cec5SDimitry Andric MachineBasicBlock *MachineBasicBlock::removeFromParent() { 14680b57cec5SDimitry Andric assert(getParent() && "Not embedded in a function!"); 14690b57cec5SDimitry Andric getParent()->remove(this); 14700b57cec5SDimitry Andric return this; 14710b57cec5SDimitry Andric } 14720b57cec5SDimitry Andric 14730b57cec5SDimitry Andric /// This method unlinks 'this' from the containing function, and deletes it. 14740b57cec5SDimitry Andric void MachineBasicBlock::eraseFromParent() { 14750b57cec5SDimitry Andric assert(getParent() && "Not embedded in a function!"); 14760b57cec5SDimitry Andric getParent()->erase(this); 14770b57cec5SDimitry Andric } 14780b57cec5SDimitry Andric 14790b57cec5SDimitry Andric /// Given a machine basic block that branched to 'Old', change the code and CFG 14800b57cec5SDimitry Andric /// so that it branches to 'New' instead. 14810b57cec5SDimitry Andric void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old, 14820b57cec5SDimitry Andric MachineBasicBlock *New) { 14830b57cec5SDimitry Andric assert(Old != New && "Cannot replace self with self!"); 14840b57cec5SDimitry Andric 14850b57cec5SDimitry Andric MachineBasicBlock::instr_iterator I = instr_end(); 14860b57cec5SDimitry Andric while (I != instr_begin()) { 14870b57cec5SDimitry Andric --I; 14880b57cec5SDimitry Andric if (!I->isTerminator()) break; 14890b57cec5SDimitry Andric 14900b57cec5SDimitry Andric // Scan the operands of this machine instruction, replacing any uses of Old 14910b57cec5SDimitry Andric // with New. 1492*0fca6ea1SDimitry Andric for (MachineOperand &MO : I->operands()) 1493*0fca6ea1SDimitry Andric if (MO.isMBB() && MO.getMBB() == Old) 1494*0fca6ea1SDimitry Andric MO.setMBB(New); 14950b57cec5SDimitry Andric } 14960b57cec5SDimitry Andric 14970b57cec5SDimitry Andric // Update the successor information. 14980b57cec5SDimitry Andric replaceSuccessor(Old, New); 14990b57cec5SDimitry Andric } 15000b57cec5SDimitry Andric 15018bcb0991SDimitry Andric void MachineBasicBlock::replacePhiUsesWith(MachineBasicBlock *Old, 15028bcb0991SDimitry Andric MachineBasicBlock *New) { 15038bcb0991SDimitry Andric for (MachineInstr &MI : phis()) 15048bcb0991SDimitry Andric for (unsigned i = 2, e = MI.getNumOperands() + 1; i != e; i += 2) { 15058bcb0991SDimitry Andric MachineOperand &MO = MI.getOperand(i); 15068bcb0991SDimitry Andric if (MO.getMBB() == Old) 15078bcb0991SDimitry Andric MO.setMBB(New); 15088bcb0991SDimitry Andric } 15098bcb0991SDimitry Andric } 15108bcb0991SDimitry Andric 151106c3fb27SDimitry Andric /// Find the next valid DebugLoc starting at MBBI, skipping any debug 15120b57cec5SDimitry Andric /// instructions. Return UnknownLoc if there is none. 15130b57cec5SDimitry Andric DebugLoc 15140b57cec5SDimitry Andric MachineBasicBlock::findDebugLoc(instr_iterator MBBI) { 15150b57cec5SDimitry Andric // Skip debug declarations, we don't want a DebugLoc from them. 15160b57cec5SDimitry Andric MBBI = skipDebugInstructionsForward(MBBI, instr_end()); 15170b57cec5SDimitry Andric if (MBBI != instr_end()) 15180b57cec5SDimitry Andric return MBBI->getDebugLoc(); 15190b57cec5SDimitry Andric return {}; 15200b57cec5SDimitry Andric } 15210b57cec5SDimitry Andric 1522fe6060f1SDimitry Andric DebugLoc MachineBasicBlock::rfindDebugLoc(reverse_instr_iterator MBBI) { 152306c3fb27SDimitry Andric if (MBBI == instr_rend()) 152406c3fb27SDimitry Andric return findDebugLoc(instr_begin()); 1525fe6060f1SDimitry Andric // Skip debug declarations, we don't want a DebugLoc from them. 1526fe6060f1SDimitry Andric MBBI = skipDebugInstructionsBackward(MBBI, instr_rbegin()); 1527fe6060f1SDimitry Andric if (!MBBI->isDebugInstr()) 1528fe6060f1SDimitry Andric return MBBI->getDebugLoc(); 1529fe6060f1SDimitry Andric return {}; 1530fe6060f1SDimitry Andric } 1531fe6060f1SDimitry Andric 153206c3fb27SDimitry Andric /// Find the previous valid DebugLoc preceding MBBI, skipping any debug 15330b57cec5SDimitry Andric /// instructions. Return UnknownLoc if there is none. 15340b57cec5SDimitry Andric DebugLoc MachineBasicBlock::findPrevDebugLoc(instr_iterator MBBI) { 153506c3fb27SDimitry Andric if (MBBI == instr_begin()) 153606c3fb27SDimitry Andric return {}; 15375ffd83dbSDimitry Andric // Skip debug instructions, we don't want a DebugLoc from them. 15385ffd83dbSDimitry Andric MBBI = prev_nodbg(MBBI, instr_begin()); 153906c3fb27SDimitry Andric if (!MBBI->isDebugInstr()) 154006c3fb27SDimitry Andric return MBBI->getDebugLoc(); 15410b57cec5SDimitry Andric return {}; 15420b57cec5SDimitry Andric } 15430b57cec5SDimitry Andric 1544fe6060f1SDimitry Andric DebugLoc MachineBasicBlock::rfindPrevDebugLoc(reverse_instr_iterator MBBI) { 1545fe6060f1SDimitry Andric if (MBBI == instr_rend()) 1546fe6060f1SDimitry Andric return {}; 1547fe6060f1SDimitry Andric // Skip debug declarations, we don't want a DebugLoc from them. 1548fe6060f1SDimitry Andric MBBI = next_nodbg(MBBI, instr_rend()); 1549fe6060f1SDimitry Andric if (MBBI != instr_rend()) 1550fe6060f1SDimitry Andric return MBBI->getDebugLoc(); 1551fe6060f1SDimitry Andric return {}; 1552fe6060f1SDimitry Andric } 1553fe6060f1SDimitry Andric 15540b57cec5SDimitry Andric /// Find and return the merged DebugLoc of the branch instructions of the block. 15550b57cec5SDimitry Andric /// Return UnknownLoc if there is none. 15560b57cec5SDimitry Andric DebugLoc 15570b57cec5SDimitry Andric MachineBasicBlock::findBranchDebugLoc() { 15580b57cec5SDimitry Andric DebugLoc DL; 15590b57cec5SDimitry Andric auto TI = getFirstTerminator(); 15600b57cec5SDimitry Andric while (TI != end() && !TI->isBranch()) 15610b57cec5SDimitry Andric ++TI; 15620b57cec5SDimitry Andric 15630b57cec5SDimitry Andric if (TI != end()) { 15640b57cec5SDimitry Andric DL = TI->getDebugLoc(); 15650b57cec5SDimitry Andric for (++TI ; TI != end() ; ++TI) 15660b57cec5SDimitry Andric if (TI->isBranch()) 15670b57cec5SDimitry Andric DL = DILocation::getMergedLocation(DL, TI->getDebugLoc()); 15680b57cec5SDimitry Andric } 15690b57cec5SDimitry Andric return DL; 15700b57cec5SDimitry Andric } 15710b57cec5SDimitry Andric 15720b57cec5SDimitry Andric /// Return probability of the edge from this block to MBB. 15730b57cec5SDimitry Andric BranchProbability 15740b57cec5SDimitry Andric MachineBasicBlock::getSuccProbability(const_succ_iterator Succ) const { 15750b57cec5SDimitry Andric if (Probs.empty()) 15760b57cec5SDimitry Andric return BranchProbability(1, succ_size()); 15770b57cec5SDimitry Andric 15780b57cec5SDimitry Andric const auto &Prob = *getProbabilityIterator(Succ); 15790b57cec5SDimitry Andric if (Prob.isUnknown()) { 15800b57cec5SDimitry Andric // For unknown probabilities, collect the sum of all known ones, and evenly 15810b57cec5SDimitry Andric // ditribute the complemental of the sum to each unknown probability. 15820b57cec5SDimitry Andric unsigned KnownProbNum = 0; 15830b57cec5SDimitry Andric auto Sum = BranchProbability::getZero(); 1584fcaf7f86SDimitry Andric for (const auto &P : Probs) { 15850b57cec5SDimitry Andric if (!P.isUnknown()) { 15860b57cec5SDimitry Andric Sum += P; 15870b57cec5SDimitry Andric KnownProbNum++; 15880b57cec5SDimitry Andric } 15890b57cec5SDimitry Andric } 15900b57cec5SDimitry Andric return Sum.getCompl() / (Probs.size() - KnownProbNum); 15910b57cec5SDimitry Andric } else 15920b57cec5SDimitry Andric return Prob; 15930b57cec5SDimitry Andric } 15940b57cec5SDimitry Andric 15950b57cec5SDimitry Andric /// Set successor probability of a given iterator. 15960b57cec5SDimitry Andric void MachineBasicBlock::setSuccProbability(succ_iterator I, 15970b57cec5SDimitry Andric BranchProbability Prob) { 15980b57cec5SDimitry Andric assert(!Prob.isUnknown()); 15990b57cec5SDimitry Andric if (Probs.empty()) 16000b57cec5SDimitry Andric return; 16010b57cec5SDimitry Andric *getProbabilityIterator(I) = Prob; 16020b57cec5SDimitry Andric } 16030b57cec5SDimitry Andric 16040b57cec5SDimitry Andric /// Return probability iterator corresonding to the I successor iterator 16050b57cec5SDimitry Andric MachineBasicBlock::const_probability_iterator 16060b57cec5SDimitry Andric MachineBasicBlock::getProbabilityIterator( 16070b57cec5SDimitry Andric MachineBasicBlock::const_succ_iterator I) const { 16080b57cec5SDimitry Andric assert(Probs.size() == Successors.size() && "Async probability list!"); 16090b57cec5SDimitry Andric const size_t index = std::distance(Successors.begin(), I); 16100b57cec5SDimitry Andric assert(index < Probs.size() && "Not a current successor!"); 16110b57cec5SDimitry Andric return Probs.begin() + index; 16120b57cec5SDimitry Andric } 16130b57cec5SDimitry Andric 16140b57cec5SDimitry Andric /// Return probability iterator corresonding to the I successor iterator. 16150b57cec5SDimitry Andric MachineBasicBlock::probability_iterator 16160b57cec5SDimitry Andric MachineBasicBlock::getProbabilityIterator(MachineBasicBlock::succ_iterator I) { 16170b57cec5SDimitry Andric assert(Probs.size() == Successors.size() && "Async probability list!"); 16180b57cec5SDimitry Andric const size_t index = std::distance(Successors.begin(), I); 16190b57cec5SDimitry Andric assert(index < Probs.size() && "Not a current successor!"); 16200b57cec5SDimitry Andric return Probs.begin() + index; 16210b57cec5SDimitry Andric } 16220b57cec5SDimitry Andric 16230b57cec5SDimitry Andric /// Return whether (physical) register "Reg" has been <def>ined and not <kill>ed 16240b57cec5SDimitry Andric /// as of just before "MI". 16250b57cec5SDimitry Andric /// 16260b57cec5SDimitry Andric /// Search is localised to a neighborhood of 16270b57cec5SDimitry Andric /// Neighborhood instructions before (searching for defs or kills) and N 16280b57cec5SDimitry Andric /// instructions after (searching just for defs) MI. 16290b57cec5SDimitry Andric MachineBasicBlock::LivenessQueryResult 16300b57cec5SDimitry Andric MachineBasicBlock::computeRegisterLiveness(const TargetRegisterInfo *TRI, 16315ffd83dbSDimitry Andric MCRegister Reg, const_iterator Before, 16320b57cec5SDimitry Andric unsigned Neighborhood) const { 16330b57cec5SDimitry Andric unsigned N = Neighborhood; 16340b57cec5SDimitry Andric 16350b57cec5SDimitry Andric // Try searching forwards from Before, looking for reads or defs. 16360b57cec5SDimitry Andric const_iterator I(Before); 16370b57cec5SDimitry Andric for (; I != end() && N > 0; ++I) { 1638fe6060f1SDimitry Andric if (I->isDebugOrPseudoInstr()) 16390b57cec5SDimitry Andric continue; 16400b57cec5SDimitry Andric 16410b57cec5SDimitry Andric --N; 16420b57cec5SDimitry Andric 1643480093f4SDimitry Andric PhysRegInfo Info = AnalyzePhysRegInBundle(*I, Reg, TRI); 16440b57cec5SDimitry Andric 16450b57cec5SDimitry Andric // Register is live when we read it here. 16460b57cec5SDimitry Andric if (Info.Read) 16470b57cec5SDimitry Andric return LQR_Live; 16480b57cec5SDimitry Andric // Register is dead if we can fully overwrite or clobber it here. 16490b57cec5SDimitry Andric if (Info.FullyDefined || Info.Clobbered) 16500b57cec5SDimitry Andric return LQR_Dead; 16510b57cec5SDimitry Andric } 16520b57cec5SDimitry Andric 16530b57cec5SDimitry Andric // If we reached the end, it is safe to clobber Reg at the end of a block of 16540b57cec5SDimitry Andric // no successor has it live in. 16550b57cec5SDimitry Andric if (I == end()) { 16560b57cec5SDimitry Andric for (MachineBasicBlock *S : successors()) { 16570b57cec5SDimitry Andric for (const MachineBasicBlock::RegisterMaskPair &LI : S->liveins()) { 16580b57cec5SDimitry Andric if (TRI->regsOverlap(LI.PhysReg, Reg)) 16590b57cec5SDimitry Andric return LQR_Live; 16600b57cec5SDimitry Andric } 16610b57cec5SDimitry Andric } 16620b57cec5SDimitry Andric 16630b57cec5SDimitry Andric return LQR_Dead; 16640b57cec5SDimitry Andric } 16650b57cec5SDimitry Andric 16660b57cec5SDimitry Andric 16670b57cec5SDimitry Andric N = Neighborhood; 16680b57cec5SDimitry Andric 16690b57cec5SDimitry Andric // Start by searching backwards from Before, looking for kills, reads or defs. 16700b57cec5SDimitry Andric I = const_iterator(Before); 16710b57cec5SDimitry Andric // If this is the first insn in the block, don't search backwards. 16720b57cec5SDimitry Andric if (I != begin()) { 16730b57cec5SDimitry Andric do { 16740b57cec5SDimitry Andric --I; 16750b57cec5SDimitry Andric 1676fe6060f1SDimitry Andric if (I->isDebugOrPseudoInstr()) 16770b57cec5SDimitry Andric continue; 16780b57cec5SDimitry Andric 16790b57cec5SDimitry Andric --N; 16800b57cec5SDimitry Andric 1681480093f4SDimitry Andric PhysRegInfo Info = AnalyzePhysRegInBundle(*I, Reg, TRI); 16820b57cec5SDimitry Andric 16830b57cec5SDimitry Andric // Defs happen after uses so they take precedence if both are present. 16840b57cec5SDimitry Andric 16850b57cec5SDimitry Andric // Register is dead after a dead def of the full register. 16860b57cec5SDimitry Andric if (Info.DeadDef) 16870b57cec5SDimitry Andric return LQR_Dead; 16880b57cec5SDimitry Andric // Register is (at least partially) live after a def. 16890b57cec5SDimitry Andric if (Info.Defined) { 16900b57cec5SDimitry Andric if (!Info.PartialDeadDef) 16910b57cec5SDimitry Andric return LQR_Live; 16920b57cec5SDimitry Andric // As soon as we saw a partial definition (dead or not), 16930b57cec5SDimitry Andric // we cannot tell if the value is partial live without 16940b57cec5SDimitry Andric // tracking the lanemasks. We are not going to do this, 16950b57cec5SDimitry Andric // so fall back on the remaining of the analysis. 16960b57cec5SDimitry Andric break; 16970b57cec5SDimitry Andric } 16980b57cec5SDimitry Andric // Register is dead after a full kill or clobber and no def. 16990b57cec5SDimitry Andric if (Info.Killed || Info.Clobbered) 17000b57cec5SDimitry Andric return LQR_Dead; 17010b57cec5SDimitry Andric // Register must be live if we read it. 17020b57cec5SDimitry Andric if (Info.Read) 17030b57cec5SDimitry Andric return LQR_Live; 17040b57cec5SDimitry Andric 17050b57cec5SDimitry Andric } while (I != begin() && N > 0); 17060b57cec5SDimitry Andric } 17070b57cec5SDimitry Andric 1708480093f4SDimitry Andric // If all the instructions before this in the block are debug instructions, 1709480093f4SDimitry Andric // skip over them. 1710fe6060f1SDimitry Andric while (I != begin() && std::prev(I)->isDebugOrPseudoInstr()) 1711480093f4SDimitry Andric --I; 1712480093f4SDimitry Andric 17130b57cec5SDimitry Andric // Did we get to the start of the block? 17140b57cec5SDimitry Andric if (I == begin()) { 17150b57cec5SDimitry Andric // If so, the register's state is definitely defined by the live-in state. 17160b57cec5SDimitry Andric for (const MachineBasicBlock::RegisterMaskPair &LI : liveins()) 17170b57cec5SDimitry Andric if (TRI->regsOverlap(LI.PhysReg, Reg)) 17180b57cec5SDimitry Andric return LQR_Live; 17190b57cec5SDimitry Andric 17200b57cec5SDimitry Andric return LQR_Dead; 17210b57cec5SDimitry Andric } 17220b57cec5SDimitry Andric 17230b57cec5SDimitry Andric // At this point we have no idea of the liveness of the register. 17240b57cec5SDimitry Andric return LQR_Unknown; 17250b57cec5SDimitry Andric } 17260b57cec5SDimitry Andric 17270b57cec5SDimitry Andric const uint32_t * 17280b57cec5SDimitry Andric MachineBasicBlock::getBeginClobberMask(const TargetRegisterInfo *TRI) const { 17290b57cec5SDimitry Andric // EH funclet entry does not preserve any registers. 17300b57cec5SDimitry Andric return isEHFuncletEntry() ? TRI->getNoPreservedMask() : nullptr; 17310b57cec5SDimitry Andric } 17320b57cec5SDimitry Andric 17330b57cec5SDimitry Andric const uint32_t * 17340b57cec5SDimitry Andric MachineBasicBlock::getEndClobberMask(const TargetRegisterInfo *TRI) const { 17350b57cec5SDimitry Andric // If we see a return block with successors, this must be a funclet return, 17360b57cec5SDimitry Andric // which does not preserve any registers. If there are no successors, we don't 17370b57cec5SDimitry Andric // care what kind of return it is, putting a mask after it is a no-op. 17380b57cec5SDimitry Andric return isReturnBlock() && !succ_empty() ? TRI->getNoPreservedMask() : nullptr; 17390b57cec5SDimitry Andric } 17400b57cec5SDimitry Andric 17410b57cec5SDimitry Andric void MachineBasicBlock::clearLiveIns() { 17420b57cec5SDimitry Andric LiveIns.clear(); 17430b57cec5SDimitry Andric } 17440b57cec5SDimitry Andric 1745*0fca6ea1SDimitry Andric void MachineBasicBlock::clearLiveIns( 1746*0fca6ea1SDimitry Andric std::vector<RegisterMaskPair> &OldLiveIns) { 1747*0fca6ea1SDimitry Andric assert(OldLiveIns.empty() && "Vector must be empty"); 1748*0fca6ea1SDimitry Andric std::swap(LiveIns, OldLiveIns); 1749*0fca6ea1SDimitry Andric } 1750*0fca6ea1SDimitry Andric 17510b57cec5SDimitry Andric MachineBasicBlock::livein_iterator MachineBasicBlock::livein_begin() const { 17520b57cec5SDimitry Andric assert(getParent()->getProperties().hasProperty( 17530b57cec5SDimitry Andric MachineFunctionProperties::Property::TracksLiveness) && 17540b57cec5SDimitry Andric "Liveness information is accurate"); 17550b57cec5SDimitry Andric return LiveIns.begin(); 17560b57cec5SDimitry Andric } 17575ffd83dbSDimitry Andric 1758fe6060f1SDimitry Andric MachineBasicBlock::liveout_iterator MachineBasicBlock::liveout_begin() const { 1759fe6060f1SDimitry Andric const MachineFunction &MF = *getParent(); 1760fe6060f1SDimitry Andric assert(MF.getProperties().hasProperty( 1761fe6060f1SDimitry Andric MachineFunctionProperties::Property::TracksLiveness) && 1762fe6060f1SDimitry Andric "Liveness information is accurate"); 1763fe6060f1SDimitry Andric 1764fe6060f1SDimitry Andric const TargetLowering &TLI = *MF.getSubtarget().getTargetLowering(); 1765fe6060f1SDimitry Andric MCPhysReg ExceptionPointer = 0, ExceptionSelector = 0; 1766fe6060f1SDimitry Andric if (MF.getFunction().hasPersonalityFn()) { 1767fe6060f1SDimitry Andric auto PersonalityFn = MF.getFunction().getPersonalityFn(); 1768fe6060f1SDimitry Andric ExceptionPointer = TLI.getExceptionPointerRegister(PersonalityFn); 1769fe6060f1SDimitry Andric ExceptionSelector = TLI.getExceptionSelectorRegister(PersonalityFn); 1770fe6060f1SDimitry Andric } 1771fe6060f1SDimitry Andric 1772fe6060f1SDimitry Andric return liveout_iterator(*this, ExceptionPointer, ExceptionSelector, false); 1773fe6060f1SDimitry Andric } 1774fe6060f1SDimitry Andric 177581ad6265SDimitry Andric bool MachineBasicBlock::sizeWithoutDebugLargerThan(unsigned Limit) const { 177681ad6265SDimitry Andric unsigned Cntr = 0; 177781ad6265SDimitry Andric auto R = instructionsWithoutDebug(begin(), end()); 177881ad6265SDimitry Andric for (auto I = R.begin(), E = R.end(); I != E; ++I) { 177981ad6265SDimitry Andric if (++Cntr > Limit) 178081ad6265SDimitry Andric return true; 178181ad6265SDimitry Andric } 178281ad6265SDimitry Andric return false; 178381ad6265SDimitry Andric } 178481ad6265SDimitry Andric 17855ffd83dbSDimitry Andric const MBBSectionID MBBSectionID::ColdSectionID(MBBSectionID::SectionType::Cold); 17865ffd83dbSDimitry Andric const MBBSectionID 17875ffd83dbSDimitry Andric MBBSectionID::ExceptionSectionID(MBBSectionID::SectionType::Exception); 1788