1*0b57cec5SDimitry Andric //===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // Collect the sequence of machine instructions for a basic block. 10*0b57cec5SDimitry Andric // 11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12*0b57cec5SDimitry Andric 13*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 14*0b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h" 1581ad6265SDimitry Andric #include "llvm/CodeGen/LivePhysRegs.h" 16*0b57cec5SDimitry Andric #include "llvm/CodeGen/LiveVariables.h" 17*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h" 18*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 19*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 20*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h" 21*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 22*0b57cec5SDimitry Andric #include "llvm/CodeGen/SlotIndexes.h" 23*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 24fe6060f1SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 25*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 26*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 27*0b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h" 28*0b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 29*0b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 30*0b57cec5SDimitry Andric #include "llvm/IR/ModuleSlotTracker.h" 31*0b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h" 32*0b57cec5SDimitry Andric #include "llvm/MC/MCContext.h" 33*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 34*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 35*0b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 36*0b57cec5SDimitry Andric #include <algorithm> 37*0b57cec5SDimitry Andric using namespace llvm; 38*0b57cec5SDimitry Andric 39*0b57cec5SDimitry Andric #define DEBUG_TYPE "codegen" 40*0b57cec5SDimitry Andric 418bcb0991SDimitry Andric static cl::opt<bool> PrintSlotIndexes( 428bcb0991SDimitry Andric "print-slotindexes", 438bcb0991SDimitry Andric cl::desc("When printing machine IR, annotate instructions and blocks with " 448bcb0991SDimitry Andric "SlotIndexes when available"), 458bcb0991SDimitry Andric cl::init(true), cl::Hidden); 468bcb0991SDimitry Andric 47*0b57cec5SDimitry Andric MachineBasicBlock::MachineBasicBlock(MachineFunction &MF, const BasicBlock *B) 48*0b57cec5SDimitry Andric : BB(B), Number(-1), xParent(&MF) { 49*0b57cec5SDimitry Andric Insts.Parent = this; 50*0b57cec5SDimitry Andric if (B) 51*0b57cec5SDimitry Andric IrrLoopHeaderWeight = B->getIrrLoopHeaderWeight(); 52*0b57cec5SDimitry Andric } 53*0b57cec5SDimitry Andric 5481ad6265SDimitry Andric MachineBasicBlock::~MachineBasicBlock() = default; 55*0b57cec5SDimitry Andric 56*0b57cec5SDimitry Andric /// Return the MCSymbol for this basic block. 57*0b57cec5SDimitry Andric MCSymbol *MachineBasicBlock::getSymbol() const { 58*0b57cec5SDimitry Andric if (!CachedMCSymbol) { 59*0b57cec5SDimitry Andric const MachineFunction *MF = getParent(); 60*0b57cec5SDimitry Andric MCContext &Ctx = MF->getContext(); 615ffd83dbSDimitry Andric 62e8d8bef9SDimitry Andric // We emit a non-temporary symbol -- with a descriptive name -- if it begins 63e8d8bef9SDimitry Andric // a section (with basic block sections). Otherwise we fall back to use temp 64e8d8bef9SDimitry Andric // label. 65e8d8bef9SDimitry Andric if (MF->hasBBSections() && isBeginSection()) { 665ffd83dbSDimitry Andric SmallString<5> Suffix; 675ffd83dbSDimitry Andric if (SectionID == MBBSectionID::ColdSectionID) { 685ffd83dbSDimitry Andric Suffix += ".cold"; 695ffd83dbSDimitry Andric } else if (SectionID == MBBSectionID::ExceptionSectionID) { 705ffd83dbSDimitry Andric Suffix += ".eh"; 715ffd83dbSDimitry Andric } else { 72e8d8bef9SDimitry Andric // For symbols that represent basic block sections, we add ".__part." to 73e8d8bef9SDimitry Andric // allow tools like symbolizers to know that this represents a part of 74e8d8bef9SDimitry Andric // the original function. 75e8d8bef9SDimitry Andric Suffix = (Suffix + Twine(".__part.") + Twine(SectionID.Number)).str(); 765ffd83dbSDimitry Andric } 775ffd83dbSDimitry Andric CachedMCSymbol = Ctx.getOrCreateSymbol(MF->getName() + Suffix); 785ffd83dbSDimitry Andric } else { 79e8d8bef9SDimitry Andric const StringRef Prefix = Ctx.getAsmInfo()->getPrivateLabelPrefix(); 80*0b57cec5SDimitry Andric CachedMCSymbol = Ctx.getOrCreateSymbol(Twine(Prefix) + "BB" + 81*0b57cec5SDimitry Andric Twine(MF->getFunctionNumber()) + 82*0b57cec5SDimitry Andric "_" + Twine(getNumber())); 83*0b57cec5SDimitry Andric } 845ffd83dbSDimitry Andric } 85*0b57cec5SDimitry Andric return CachedMCSymbol; 86*0b57cec5SDimitry Andric } 87*0b57cec5SDimitry Andric 88fe6060f1SDimitry Andric MCSymbol *MachineBasicBlock::getEHCatchretSymbol() const { 89fe6060f1SDimitry Andric if (!CachedEHCatchretMCSymbol) { 90fe6060f1SDimitry Andric const MachineFunction *MF = getParent(); 91fe6060f1SDimitry Andric SmallString<128> SymbolName; 92fe6060f1SDimitry Andric raw_svector_ostream(SymbolName) 93fe6060f1SDimitry Andric << "$ehgcr_" << MF->getFunctionNumber() << '_' << getNumber(); 94fe6060f1SDimitry Andric CachedEHCatchretMCSymbol = MF->getContext().getOrCreateSymbol(SymbolName); 95fe6060f1SDimitry Andric } 96fe6060f1SDimitry Andric return CachedEHCatchretMCSymbol; 97fe6060f1SDimitry Andric } 98fe6060f1SDimitry Andric 99e8d8bef9SDimitry Andric MCSymbol *MachineBasicBlock::getEndSymbol() const { 100e8d8bef9SDimitry Andric if (!CachedEndMCSymbol) { 101e8d8bef9SDimitry Andric const MachineFunction *MF = getParent(); 102e8d8bef9SDimitry Andric MCContext &Ctx = MF->getContext(); 103e8d8bef9SDimitry Andric auto Prefix = Ctx.getAsmInfo()->getPrivateLabelPrefix(); 104e8d8bef9SDimitry Andric CachedEndMCSymbol = Ctx.getOrCreateSymbol(Twine(Prefix) + "BB_END" + 105e8d8bef9SDimitry Andric Twine(MF->getFunctionNumber()) + 106e8d8bef9SDimitry Andric "_" + Twine(getNumber())); 107e8d8bef9SDimitry Andric } 108e8d8bef9SDimitry Andric return CachedEndMCSymbol; 109e8d8bef9SDimitry Andric } 110*0b57cec5SDimitry Andric 111*0b57cec5SDimitry Andric raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) { 112*0b57cec5SDimitry Andric MBB.print(OS); 113*0b57cec5SDimitry Andric return OS; 114*0b57cec5SDimitry Andric } 115*0b57cec5SDimitry Andric 116*0b57cec5SDimitry Andric Printable llvm::printMBBReference(const MachineBasicBlock &MBB) { 117*0b57cec5SDimitry Andric return Printable([&MBB](raw_ostream &OS) { return MBB.printAsOperand(OS); }); 118*0b57cec5SDimitry Andric } 119*0b57cec5SDimitry Andric 120*0b57cec5SDimitry Andric /// When an MBB is added to an MF, we need to update the parent pointer of the 121*0b57cec5SDimitry Andric /// MBB, the MBB numbering, and any instructions in the MBB to be on the right 122*0b57cec5SDimitry Andric /// operand list for registers. 123*0b57cec5SDimitry Andric /// 124*0b57cec5SDimitry Andric /// MBBs start out as #-1. When a MBB is added to a MachineFunction, it 125*0b57cec5SDimitry Andric /// gets the next available unique MBB number. If it is removed from a 126*0b57cec5SDimitry Andric /// MachineFunction, it goes back to being #-1. 127*0b57cec5SDimitry Andric void ilist_callback_traits<MachineBasicBlock>::addNodeToList( 128*0b57cec5SDimitry Andric MachineBasicBlock *N) { 129*0b57cec5SDimitry Andric MachineFunction &MF = *N->getParent(); 130*0b57cec5SDimitry Andric N->Number = MF.addToMBBNumbering(N); 131*0b57cec5SDimitry Andric 132*0b57cec5SDimitry Andric // Make sure the instructions have their operands in the reginfo lists. 133*0b57cec5SDimitry Andric MachineRegisterInfo &RegInfo = MF.getRegInfo(); 134349cc55cSDimitry Andric for (MachineInstr &MI : N->instrs()) 13581ad6265SDimitry Andric MI.addRegOperandsToUseLists(RegInfo); 136*0b57cec5SDimitry Andric } 137*0b57cec5SDimitry Andric 138*0b57cec5SDimitry Andric void ilist_callback_traits<MachineBasicBlock>::removeNodeFromList( 139*0b57cec5SDimitry Andric MachineBasicBlock *N) { 140*0b57cec5SDimitry Andric N->getParent()->removeFromMBBNumbering(N->Number); 141*0b57cec5SDimitry Andric N->Number = -1; 142*0b57cec5SDimitry Andric } 143*0b57cec5SDimitry Andric 144*0b57cec5SDimitry Andric /// When we add an instruction to a basic block list, we update its parent 145*0b57cec5SDimitry Andric /// pointer and add its operands from reg use/def lists if appropriate. 146*0b57cec5SDimitry Andric void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) { 147*0b57cec5SDimitry Andric assert(!N->getParent() && "machine instruction already in a basic block"); 148*0b57cec5SDimitry Andric N->setParent(Parent); 149*0b57cec5SDimitry Andric 150*0b57cec5SDimitry Andric // Add the instruction's register operands to their corresponding 151*0b57cec5SDimitry Andric // use/def lists. 152*0b57cec5SDimitry Andric MachineFunction *MF = Parent->getParent(); 15381ad6265SDimitry Andric N->addRegOperandsToUseLists(MF->getRegInfo()); 154*0b57cec5SDimitry Andric MF->handleInsertion(*N); 155*0b57cec5SDimitry Andric } 156*0b57cec5SDimitry Andric 157*0b57cec5SDimitry Andric /// When we remove an instruction from a basic block list, we update its parent 158*0b57cec5SDimitry Andric /// pointer and remove its operands from reg use/def lists if appropriate. 159*0b57cec5SDimitry Andric void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) { 160*0b57cec5SDimitry Andric assert(N->getParent() && "machine instruction not in a basic block"); 161*0b57cec5SDimitry Andric 162*0b57cec5SDimitry Andric // Remove from the use/def lists. 163*0b57cec5SDimitry Andric if (MachineFunction *MF = N->getMF()) { 164*0b57cec5SDimitry Andric MF->handleRemoval(*N); 16581ad6265SDimitry Andric N->removeRegOperandsFromUseLists(MF->getRegInfo()); 166*0b57cec5SDimitry Andric } 167*0b57cec5SDimitry Andric 168*0b57cec5SDimitry Andric N->setParent(nullptr); 169*0b57cec5SDimitry Andric } 170*0b57cec5SDimitry Andric 171*0b57cec5SDimitry Andric /// When moving a range of instructions from one MBB list to another, we need to 172*0b57cec5SDimitry Andric /// update the parent pointers and the use/def lists. 173*0b57cec5SDimitry Andric void ilist_traits<MachineInstr>::transferNodesFromList(ilist_traits &FromList, 174*0b57cec5SDimitry Andric instr_iterator First, 175*0b57cec5SDimitry Andric instr_iterator Last) { 176*0b57cec5SDimitry Andric assert(Parent->getParent() == FromList.Parent->getParent() && 177*0b57cec5SDimitry Andric "cannot transfer MachineInstrs between MachineFunctions"); 178*0b57cec5SDimitry Andric 179*0b57cec5SDimitry Andric // If it's within the same BB, there's nothing to do. 180*0b57cec5SDimitry Andric if (this == &FromList) 181*0b57cec5SDimitry Andric return; 182*0b57cec5SDimitry Andric 183*0b57cec5SDimitry Andric assert(Parent != FromList.Parent && "Two lists have the same parent?"); 184*0b57cec5SDimitry Andric 185*0b57cec5SDimitry Andric // If splicing between two blocks within the same function, just update the 186*0b57cec5SDimitry Andric // parent pointers. 187*0b57cec5SDimitry Andric for (; First != Last; ++First) 188*0b57cec5SDimitry Andric First->setParent(Parent); 189*0b57cec5SDimitry Andric } 190*0b57cec5SDimitry Andric 191*0b57cec5SDimitry Andric void ilist_traits<MachineInstr>::deleteNode(MachineInstr *MI) { 192*0b57cec5SDimitry Andric assert(!MI->getParent() && "MI is still in a block!"); 1930eae32dcSDimitry Andric Parent->getParent()->deleteMachineInstr(MI); 194*0b57cec5SDimitry Andric } 195*0b57cec5SDimitry Andric 196*0b57cec5SDimitry Andric MachineBasicBlock::iterator MachineBasicBlock::getFirstNonPHI() { 197*0b57cec5SDimitry Andric instr_iterator I = instr_begin(), E = instr_end(); 198*0b57cec5SDimitry Andric while (I != E && I->isPHI()) 199*0b57cec5SDimitry Andric ++I; 200*0b57cec5SDimitry Andric assert((I == E || !I->isInsideBundle()) && 201*0b57cec5SDimitry Andric "First non-phi MI cannot be inside a bundle!"); 202*0b57cec5SDimitry Andric return I; 203*0b57cec5SDimitry Andric } 204*0b57cec5SDimitry Andric 205*0b57cec5SDimitry Andric MachineBasicBlock::iterator 206*0b57cec5SDimitry Andric MachineBasicBlock::SkipPHIsAndLabels(MachineBasicBlock::iterator I) { 207*0b57cec5SDimitry Andric const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo(); 208*0b57cec5SDimitry Andric 209*0b57cec5SDimitry Andric iterator E = end(); 210*0b57cec5SDimitry Andric while (I != E && (I->isPHI() || I->isPosition() || 211*0b57cec5SDimitry Andric TII->isBasicBlockPrologue(*I))) 212*0b57cec5SDimitry Andric ++I; 213*0b57cec5SDimitry Andric // FIXME: This needs to change if we wish to bundle labels 214*0b57cec5SDimitry Andric // inside the bundle. 215*0b57cec5SDimitry Andric assert((I == E || !I->isInsideBundle()) && 216*0b57cec5SDimitry Andric "First non-phi / non-label instruction is inside a bundle!"); 217*0b57cec5SDimitry Andric return I; 218*0b57cec5SDimitry Andric } 219*0b57cec5SDimitry Andric 220*0b57cec5SDimitry Andric MachineBasicBlock::iterator 221fe6060f1SDimitry Andric MachineBasicBlock::SkipPHIsLabelsAndDebug(MachineBasicBlock::iterator I, 222fe6060f1SDimitry Andric bool SkipPseudoOp) { 223*0b57cec5SDimitry Andric const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo(); 224*0b57cec5SDimitry Andric 225*0b57cec5SDimitry Andric iterator E = end(); 226*0b57cec5SDimitry Andric while (I != E && (I->isPHI() || I->isPosition() || I->isDebugInstr() || 227fe6060f1SDimitry Andric (SkipPseudoOp && I->isPseudoProbe()) || 228*0b57cec5SDimitry Andric TII->isBasicBlockPrologue(*I))) 229*0b57cec5SDimitry Andric ++I; 230*0b57cec5SDimitry Andric // FIXME: This needs to change if we wish to bundle labels / dbg_values 231*0b57cec5SDimitry Andric // inside the bundle. 232*0b57cec5SDimitry Andric assert((I == E || !I->isInsideBundle()) && 233*0b57cec5SDimitry Andric "First non-phi / non-label / non-debug " 234*0b57cec5SDimitry Andric "instruction is inside a bundle!"); 235*0b57cec5SDimitry Andric return I; 236*0b57cec5SDimitry Andric } 237*0b57cec5SDimitry Andric 238*0b57cec5SDimitry Andric MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { 239*0b57cec5SDimitry Andric iterator B = begin(), E = end(), I = E; 240*0b57cec5SDimitry Andric while (I != B && ((--I)->isTerminator() || I->isDebugInstr())) 241*0b57cec5SDimitry Andric ; /*noop */ 242*0b57cec5SDimitry Andric while (I != E && !I->isTerminator()) 243*0b57cec5SDimitry Andric ++I; 244*0b57cec5SDimitry Andric return I; 245*0b57cec5SDimitry Andric } 246*0b57cec5SDimitry Andric 247*0b57cec5SDimitry Andric MachineBasicBlock::instr_iterator MachineBasicBlock::getFirstInstrTerminator() { 248*0b57cec5SDimitry Andric instr_iterator B = instr_begin(), E = instr_end(), I = E; 249*0b57cec5SDimitry Andric while (I != B && ((--I)->isTerminator() || I->isDebugInstr())) 250*0b57cec5SDimitry Andric ; /*noop */ 251*0b57cec5SDimitry Andric while (I != E && !I->isTerminator()) 252*0b57cec5SDimitry Andric ++I; 253*0b57cec5SDimitry Andric return I; 254*0b57cec5SDimitry Andric } 255*0b57cec5SDimitry Andric 256fe6060f1SDimitry Andric MachineBasicBlock::iterator 257fe6060f1SDimitry Andric MachineBasicBlock::getFirstNonDebugInstr(bool SkipPseudoOp) { 258*0b57cec5SDimitry Andric // Skip over begin-of-block dbg_value instructions. 259fe6060f1SDimitry Andric return skipDebugInstructionsForward(begin(), end(), SkipPseudoOp); 260*0b57cec5SDimitry Andric } 261*0b57cec5SDimitry Andric 262fe6060f1SDimitry Andric MachineBasicBlock::iterator 263fe6060f1SDimitry Andric MachineBasicBlock::getLastNonDebugInstr(bool SkipPseudoOp) { 264*0b57cec5SDimitry Andric // Skip over end-of-block dbg_value instructions. 265*0b57cec5SDimitry Andric instr_iterator B = instr_begin(), I = instr_end(); 266*0b57cec5SDimitry Andric while (I != B) { 267*0b57cec5SDimitry Andric --I; 268*0b57cec5SDimitry Andric // Return instruction that starts a bundle. 269*0b57cec5SDimitry Andric if (I->isDebugInstr() || I->isInsideBundle()) 270*0b57cec5SDimitry Andric continue; 271fe6060f1SDimitry Andric if (SkipPseudoOp && I->isPseudoProbe()) 272fe6060f1SDimitry Andric continue; 273*0b57cec5SDimitry Andric return I; 274*0b57cec5SDimitry Andric } 275*0b57cec5SDimitry Andric // The block is all debug values. 276*0b57cec5SDimitry Andric return end(); 277*0b57cec5SDimitry Andric } 278*0b57cec5SDimitry Andric 279*0b57cec5SDimitry Andric bool MachineBasicBlock::hasEHPadSuccessor() const { 280349cc55cSDimitry Andric for (const MachineBasicBlock *Succ : successors()) 281349cc55cSDimitry Andric if (Succ->isEHPad()) 282*0b57cec5SDimitry Andric return true; 283*0b57cec5SDimitry Andric return false; 284*0b57cec5SDimitry Andric } 285*0b57cec5SDimitry Andric 286e8d8bef9SDimitry Andric bool MachineBasicBlock::isEntryBlock() const { 287e8d8bef9SDimitry Andric return getParent()->begin() == getIterator(); 288e8d8bef9SDimitry Andric } 289e8d8bef9SDimitry Andric 290*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 291*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineBasicBlock::dump() const { 292*0b57cec5SDimitry Andric print(dbgs()); 293*0b57cec5SDimitry Andric } 294*0b57cec5SDimitry Andric #endif 295*0b57cec5SDimitry Andric 2965ffd83dbSDimitry Andric bool MachineBasicBlock::mayHaveInlineAsmBr() const { 2975ffd83dbSDimitry Andric for (const MachineBasicBlock *Succ : successors()) { 2985ffd83dbSDimitry Andric if (Succ->isInlineAsmBrIndirectTarget()) 2995ffd83dbSDimitry Andric return true; 3005ffd83dbSDimitry Andric } 3015ffd83dbSDimitry Andric return false; 3025ffd83dbSDimitry Andric } 3035ffd83dbSDimitry Andric 304*0b57cec5SDimitry Andric bool MachineBasicBlock::isLegalToHoistInto() const { 3055ffd83dbSDimitry Andric if (isReturnBlock() || hasEHPadSuccessor() || mayHaveInlineAsmBr()) 306*0b57cec5SDimitry Andric return false; 307*0b57cec5SDimitry Andric return true; 308*0b57cec5SDimitry Andric } 309*0b57cec5SDimitry Andric 310*0b57cec5SDimitry Andric StringRef MachineBasicBlock::getName() const { 311*0b57cec5SDimitry Andric if (const BasicBlock *LBB = getBasicBlock()) 312*0b57cec5SDimitry Andric return LBB->getName(); 313*0b57cec5SDimitry Andric else 314*0b57cec5SDimitry Andric return StringRef("", 0); 315*0b57cec5SDimitry Andric } 316*0b57cec5SDimitry Andric 317*0b57cec5SDimitry Andric /// Return a hopefully unique identifier for this block. 318*0b57cec5SDimitry Andric std::string MachineBasicBlock::getFullName() const { 319*0b57cec5SDimitry Andric std::string Name; 320*0b57cec5SDimitry Andric if (getParent()) 321*0b57cec5SDimitry Andric Name = (getParent()->getName() + ":").str(); 322*0b57cec5SDimitry Andric if (getBasicBlock()) 323*0b57cec5SDimitry Andric Name += getBasicBlock()->getName(); 324*0b57cec5SDimitry Andric else 325*0b57cec5SDimitry Andric Name += ("BB" + Twine(getNumber())).str(); 326*0b57cec5SDimitry Andric return Name; 327*0b57cec5SDimitry Andric } 328*0b57cec5SDimitry Andric 329*0b57cec5SDimitry Andric void MachineBasicBlock::print(raw_ostream &OS, const SlotIndexes *Indexes, 330*0b57cec5SDimitry Andric bool IsStandalone) const { 331*0b57cec5SDimitry Andric const MachineFunction *MF = getParent(); 332*0b57cec5SDimitry Andric if (!MF) { 333*0b57cec5SDimitry Andric OS << "Can't print out MachineBasicBlock because parent MachineFunction" 334*0b57cec5SDimitry Andric << " is null\n"; 335*0b57cec5SDimitry Andric return; 336*0b57cec5SDimitry Andric } 337*0b57cec5SDimitry Andric const Function &F = MF->getFunction(); 338*0b57cec5SDimitry Andric const Module *M = F.getParent(); 339*0b57cec5SDimitry Andric ModuleSlotTracker MST(M); 340*0b57cec5SDimitry Andric MST.incorporateFunction(F); 341*0b57cec5SDimitry Andric print(OS, MST, Indexes, IsStandalone); 342*0b57cec5SDimitry Andric } 343*0b57cec5SDimitry Andric 344*0b57cec5SDimitry Andric void MachineBasicBlock::print(raw_ostream &OS, ModuleSlotTracker &MST, 345*0b57cec5SDimitry Andric const SlotIndexes *Indexes, 346*0b57cec5SDimitry Andric bool IsStandalone) const { 347*0b57cec5SDimitry Andric const MachineFunction *MF = getParent(); 348*0b57cec5SDimitry Andric if (!MF) { 349*0b57cec5SDimitry Andric OS << "Can't print out MachineBasicBlock because parent MachineFunction" 350*0b57cec5SDimitry Andric << " is null\n"; 351*0b57cec5SDimitry Andric return; 352*0b57cec5SDimitry Andric } 353*0b57cec5SDimitry Andric 3548bcb0991SDimitry Andric if (Indexes && PrintSlotIndexes) 355*0b57cec5SDimitry Andric OS << Indexes->getMBBStartIdx(this) << '\t'; 356*0b57cec5SDimitry Andric 357e8d8bef9SDimitry Andric printName(OS, PrintNameIr | PrintNameAttributes, &MST); 358*0b57cec5SDimitry Andric OS << ":\n"; 359*0b57cec5SDimitry Andric 360*0b57cec5SDimitry Andric const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); 361*0b57cec5SDimitry Andric const MachineRegisterInfo &MRI = MF->getRegInfo(); 362*0b57cec5SDimitry Andric const TargetInstrInfo &TII = *getParent()->getSubtarget().getInstrInfo(); 363*0b57cec5SDimitry Andric bool HasLineAttributes = false; 364*0b57cec5SDimitry Andric 365*0b57cec5SDimitry Andric // Print the preds of this block according to the CFG. 366*0b57cec5SDimitry Andric if (!pred_empty() && IsStandalone) { 367*0b57cec5SDimitry Andric if (Indexes) OS << '\t'; 368*0b57cec5SDimitry Andric // Don't indent(2), align with previous line attributes. 369*0b57cec5SDimitry Andric OS << "; predecessors: "; 370e8d8bef9SDimitry Andric ListSeparator LS; 371e8d8bef9SDimitry Andric for (auto *Pred : predecessors()) 372e8d8bef9SDimitry Andric OS << LS << printMBBReference(*Pred); 373*0b57cec5SDimitry Andric OS << '\n'; 374*0b57cec5SDimitry Andric HasLineAttributes = true; 375*0b57cec5SDimitry Andric } 376*0b57cec5SDimitry Andric 377*0b57cec5SDimitry Andric if (!succ_empty()) { 378*0b57cec5SDimitry Andric if (Indexes) OS << '\t'; 379*0b57cec5SDimitry Andric // Print the successors 380*0b57cec5SDimitry Andric OS.indent(2) << "successors: "; 381e8d8bef9SDimitry Andric ListSeparator LS; 382*0b57cec5SDimitry Andric for (auto I = succ_begin(), E = succ_end(); I != E; ++I) { 383e8d8bef9SDimitry Andric OS << LS << printMBBReference(**I); 384*0b57cec5SDimitry Andric if (!Probs.empty()) 385*0b57cec5SDimitry Andric OS << '(' 386*0b57cec5SDimitry Andric << format("0x%08" PRIx32, getSuccProbability(I).getNumerator()) 387*0b57cec5SDimitry Andric << ')'; 388*0b57cec5SDimitry Andric } 389*0b57cec5SDimitry Andric if (!Probs.empty() && IsStandalone) { 390*0b57cec5SDimitry Andric // Print human readable probabilities as comments. 391*0b57cec5SDimitry Andric OS << "; "; 392e8d8bef9SDimitry Andric ListSeparator LS; 393*0b57cec5SDimitry Andric for (auto I = succ_begin(), E = succ_end(); I != E; ++I) { 394*0b57cec5SDimitry Andric const BranchProbability &BP = getSuccProbability(I); 395e8d8bef9SDimitry Andric OS << LS << printMBBReference(**I) << '(' 396*0b57cec5SDimitry Andric << format("%.2f%%", 397*0b57cec5SDimitry Andric rint(((double)BP.getNumerator() / BP.getDenominator()) * 398*0b57cec5SDimitry Andric 100.0 * 100.0) / 399*0b57cec5SDimitry Andric 100.0) 400*0b57cec5SDimitry Andric << ')'; 401*0b57cec5SDimitry Andric } 402*0b57cec5SDimitry Andric } 403*0b57cec5SDimitry Andric 404*0b57cec5SDimitry Andric OS << '\n'; 405*0b57cec5SDimitry Andric HasLineAttributes = true; 406*0b57cec5SDimitry Andric } 407*0b57cec5SDimitry Andric 408*0b57cec5SDimitry Andric if (!livein_empty() && MRI.tracksLiveness()) { 409*0b57cec5SDimitry Andric if (Indexes) OS << '\t'; 410*0b57cec5SDimitry Andric OS.indent(2) << "liveins: "; 411*0b57cec5SDimitry Andric 412e8d8bef9SDimitry Andric ListSeparator LS; 413*0b57cec5SDimitry Andric for (const auto &LI : liveins()) { 414e8d8bef9SDimitry Andric OS << LS << printReg(LI.PhysReg, TRI); 415*0b57cec5SDimitry Andric if (!LI.LaneMask.all()) 416*0b57cec5SDimitry Andric OS << ":0x" << PrintLaneMask(LI.LaneMask); 417*0b57cec5SDimitry Andric } 418*0b57cec5SDimitry Andric HasLineAttributes = true; 419*0b57cec5SDimitry Andric } 420*0b57cec5SDimitry Andric 421*0b57cec5SDimitry Andric if (HasLineAttributes) 422*0b57cec5SDimitry Andric OS << '\n'; 423*0b57cec5SDimitry Andric 424*0b57cec5SDimitry Andric bool IsInBundle = false; 425*0b57cec5SDimitry Andric for (const MachineInstr &MI : instrs()) { 4268bcb0991SDimitry Andric if (Indexes && PrintSlotIndexes) { 427*0b57cec5SDimitry Andric if (Indexes->hasIndex(MI)) 428*0b57cec5SDimitry Andric OS << Indexes->getInstructionIndex(MI); 429*0b57cec5SDimitry Andric OS << '\t'; 430*0b57cec5SDimitry Andric } 431*0b57cec5SDimitry Andric 432*0b57cec5SDimitry Andric if (IsInBundle && !MI.isInsideBundle()) { 433*0b57cec5SDimitry Andric OS.indent(2) << "}\n"; 434*0b57cec5SDimitry Andric IsInBundle = false; 435*0b57cec5SDimitry Andric } 436*0b57cec5SDimitry Andric 437*0b57cec5SDimitry Andric OS.indent(IsInBundle ? 4 : 2); 438*0b57cec5SDimitry Andric MI.print(OS, MST, IsStandalone, /*SkipOpers=*/false, /*SkipDebugLoc=*/false, 439*0b57cec5SDimitry Andric /*AddNewLine=*/false, &TII); 440*0b57cec5SDimitry Andric 441*0b57cec5SDimitry Andric if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) { 442*0b57cec5SDimitry Andric OS << " {"; 443*0b57cec5SDimitry Andric IsInBundle = true; 444*0b57cec5SDimitry Andric } 445*0b57cec5SDimitry Andric OS << '\n'; 446*0b57cec5SDimitry Andric } 447*0b57cec5SDimitry Andric 448*0b57cec5SDimitry Andric if (IsInBundle) 449*0b57cec5SDimitry Andric OS.indent(2) << "}\n"; 450*0b57cec5SDimitry Andric 451*0b57cec5SDimitry Andric if (IrrLoopHeaderWeight && IsStandalone) { 452*0b57cec5SDimitry Andric if (Indexes) OS << '\t'; 453*0b57cec5SDimitry Andric OS.indent(2) << "; Irreducible loop header weight: " 454753f127fSDimitry Andric << IrrLoopHeaderWeight.value() << '\n'; 455*0b57cec5SDimitry Andric } 456*0b57cec5SDimitry Andric } 457*0b57cec5SDimitry Andric 458e8d8bef9SDimitry Andric /// Print the basic block's name as: 459e8d8bef9SDimitry Andric /// 460e8d8bef9SDimitry Andric /// bb.{number}[.{ir-name}] [(attributes...)] 461e8d8bef9SDimitry Andric /// 462e8d8bef9SDimitry Andric /// The {ir-name} is only printed when the \ref PrintNameIr flag is passed 463e8d8bef9SDimitry Andric /// (which is the default). If the IR block has no name, it is identified 464e8d8bef9SDimitry Andric /// numerically using the attribute syntax as "(%ir-block.{ir-slot})". 465e8d8bef9SDimitry Andric /// 466e8d8bef9SDimitry Andric /// When the \ref PrintNameAttributes flag is passed, additional attributes 467e8d8bef9SDimitry Andric /// of the block are printed when set. 468e8d8bef9SDimitry Andric /// 469e8d8bef9SDimitry Andric /// \param printNameFlags Combination of \ref PrintNameFlag flags indicating 470e8d8bef9SDimitry Andric /// the parts to print. 471e8d8bef9SDimitry Andric /// \param moduleSlotTracker Optional ModuleSlotTracker. This method will 472e8d8bef9SDimitry Andric /// incorporate its own tracker when necessary to 473e8d8bef9SDimitry Andric /// determine the block's IR name. 474e8d8bef9SDimitry Andric void MachineBasicBlock::printName(raw_ostream &os, unsigned printNameFlags, 475e8d8bef9SDimitry Andric ModuleSlotTracker *moduleSlotTracker) const { 476e8d8bef9SDimitry Andric os << "bb." << getNumber(); 477e8d8bef9SDimitry Andric bool hasAttributes = false; 478e8d8bef9SDimitry Andric 479e8d8bef9SDimitry Andric if (printNameFlags & PrintNameIr) { 480e8d8bef9SDimitry Andric if (const auto *bb = getBasicBlock()) { 481e8d8bef9SDimitry Andric if (bb->hasName()) { 482e8d8bef9SDimitry Andric os << '.' << bb->getName(); 483e8d8bef9SDimitry Andric } else { 484e8d8bef9SDimitry Andric hasAttributes = true; 485e8d8bef9SDimitry Andric os << " ("; 486e8d8bef9SDimitry Andric 487e8d8bef9SDimitry Andric int slot = -1; 488e8d8bef9SDimitry Andric 489e8d8bef9SDimitry Andric if (moduleSlotTracker) { 490e8d8bef9SDimitry Andric slot = moduleSlotTracker->getLocalSlot(bb); 491e8d8bef9SDimitry Andric } else if (bb->getParent()) { 492e8d8bef9SDimitry Andric ModuleSlotTracker tmpTracker(bb->getModule(), false); 493e8d8bef9SDimitry Andric tmpTracker.incorporateFunction(*bb->getParent()); 494e8d8bef9SDimitry Andric slot = tmpTracker.getLocalSlot(bb); 495e8d8bef9SDimitry Andric } 496e8d8bef9SDimitry Andric 497e8d8bef9SDimitry Andric if (slot == -1) 498e8d8bef9SDimitry Andric os << "<ir-block badref>"; 499e8d8bef9SDimitry Andric else 500e8d8bef9SDimitry Andric os << (Twine("%ir-block.") + Twine(slot)).str(); 501e8d8bef9SDimitry Andric } 502e8d8bef9SDimitry Andric } 503e8d8bef9SDimitry Andric } 504e8d8bef9SDimitry Andric 505e8d8bef9SDimitry Andric if (printNameFlags & PrintNameAttributes) { 506e8d8bef9SDimitry Andric if (hasAddressTaken()) { 507e8d8bef9SDimitry Andric os << (hasAttributes ? ", " : " ("); 508e8d8bef9SDimitry Andric os << "address-taken"; 509e8d8bef9SDimitry Andric hasAttributes = true; 510e8d8bef9SDimitry Andric } 511e8d8bef9SDimitry Andric if (isEHPad()) { 512e8d8bef9SDimitry Andric os << (hasAttributes ? ", " : " ("); 513e8d8bef9SDimitry Andric os << "landing-pad"; 514e8d8bef9SDimitry Andric hasAttributes = true; 515e8d8bef9SDimitry Andric } 516349cc55cSDimitry Andric if (isInlineAsmBrIndirectTarget()) { 517349cc55cSDimitry Andric os << (hasAttributes ? ", " : " ("); 518349cc55cSDimitry Andric os << "inlineasm-br-indirect-target"; 519349cc55cSDimitry Andric hasAttributes = true; 520349cc55cSDimitry Andric } 521e8d8bef9SDimitry Andric if (isEHFuncletEntry()) { 522e8d8bef9SDimitry Andric os << (hasAttributes ? ", " : " ("); 523e8d8bef9SDimitry Andric os << "ehfunclet-entry"; 524e8d8bef9SDimitry Andric hasAttributes = true; 525e8d8bef9SDimitry Andric } 526e8d8bef9SDimitry Andric if (getAlignment() != Align(1)) { 527e8d8bef9SDimitry Andric os << (hasAttributes ? ", " : " ("); 528e8d8bef9SDimitry Andric os << "align " << getAlignment().value(); 529e8d8bef9SDimitry Andric hasAttributes = true; 530e8d8bef9SDimitry Andric } 531e8d8bef9SDimitry Andric if (getSectionID() != MBBSectionID(0)) { 532e8d8bef9SDimitry Andric os << (hasAttributes ? ", " : " ("); 533e8d8bef9SDimitry Andric os << "bbsections "; 534e8d8bef9SDimitry Andric switch (getSectionID().Type) { 535e8d8bef9SDimitry Andric case MBBSectionID::SectionType::Exception: 536e8d8bef9SDimitry Andric os << "Exception"; 537e8d8bef9SDimitry Andric break; 538e8d8bef9SDimitry Andric case MBBSectionID::SectionType::Cold: 539e8d8bef9SDimitry Andric os << "Cold"; 540e8d8bef9SDimitry Andric break; 541e8d8bef9SDimitry Andric default: 542e8d8bef9SDimitry Andric os << getSectionID().Number; 543e8d8bef9SDimitry Andric } 544e8d8bef9SDimitry Andric hasAttributes = true; 545e8d8bef9SDimitry Andric } 546e8d8bef9SDimitry Andric } 547e8d8bef9SDimitry Andric 548e8d8bef9SDimitry Andric if (hasAttributes) 549e8d8bef9SDimitry Andric os << ')'; 550e8d8bef9SDimitry Andric } 551e8d8bef9SDimitry Andric 552*0b57cec5SDimitry Andric void MachineBasicBlock::printAsOperand(raw_ostream &OS, 553*0b57cec5SDimitry Andric bool /*PrintType*/) const { 554e8d8bef9SDimitry Andric OS << '%'; 555e8d8bef9SDimitry Andric printName(OS, 0); 556*0b57cec5SDimitry Andric } 557*0b57cec5SDimitry Andric 558*0b57cec5SDimitry Andric void MachineBasicBlock::removeLiveIn(MCPhysReg Reg, LaneBitmask LaneMask) { 559*0b57cec5SDimitry Andric LiveInVector::iterator I = find_if( 560*0b57cec5SDimitry Andric LiveIns, [Reg](const RegisterMaskPair &LI) { return LI.PhysReg == Reg; }); 561*0b57cec5SDimitry Andric if (I == LiveIns.end()) 562*0b57cec5SDimitry Andric return; 563*0b57cec5SDimitry Andric 564*0b57cec5SDimitry Andric I->LaneMask &= ~LaneMask; 565*0b57cec5SDimitry Andric if (I->LaneMask.none()) 566*0b57cec5SDimitry Andric LiveIns.erase(I); 567*0b57cec5SDimitry Andric } 568*0b57cec5SDimitry Andric 569*0b57cec5SDimitry Andric MachineBasicBlock::livein_iterator 570*0b57cec5SDimitry Andric MachineBasicBlock::removeLiveIn(MachineBasicBlock::livein_iterator I) { 571*0b57cec5SDimitry Andric // Get non-const version of iterator. 572*0b57cec5SDimitry Andric LiveInVector::iterator LI = LiveIns.begin() + (I - LiveIns.begin()); 573*0b57cec5SDimitry Andric return LiveIns.erase(LI); 574*0b57cec5SDimitry Andric } 575*0b57cec5SDimitry Andric 576*0b57cec5SDimitry Andric bool MachineBasicBlock::isLiveIn(MCPhysReg Reg, LaneBitmask LaneMask) const { 577*0b57cec5SDimitry Andric livein_iterator I = find_if( 578*0b57cec5SDimitry Andric LiveIns, [Reg](const RegisterMaskPair &LI) { return LI.PhysReg == Reg; }); 579*0b57cec5SDimitry Andric return I != livein_end() && (I->LaneMask & LaneMask).any(); 580*0b57cec5SDimitry Andric } 581*0b57cec5SDimitry Andric 582*0b57cec5SDimitry Andric void MachineBasicBlock::sortUniqueLiveIns() { 583*0b57cec5SDimitry Andric llvm::sort(LiveIns, 584*0b57cec5SDimitry Andric [](const RegisterMaskPair &LI0, const RegisterMaskPair &LI1) { 585*0b57cec5SDimitry Andric return LI0.PhysReg < LI1.PhysReg; 586*0b57cec5SDimitry Andric }); 587*0b57cec5SDimitry Andric // Liveins are sorted by physreg now we can merge their lanemasks. 588*0b57cec5SDimitry Andric LiveInVector::const_iterator I = LiveIns.begin(); 589*0b57cec5SDimitry Andric LiveInVector::const_iterator J; 590*0b57cec5SDimitry Andric LiveInVector::iterator Out = LiveIns.begin(); 591*0b57cec5SDimitry Andric for (; I != LiveIns.end(); ++Out, I = J) { 5925ffd83dbSDimitry Andric MCRegister PhysReg = I->PhysReg; 593*0b57cec5SDimitry Andric LaneBitmask LaneMask = I->LaneMask; 594*0b57cec5SDimitry Andric for (J = std::next(I); J != LiveIns.end() && J->PhysReg == PhysReg; ++J) 595*0b57cec5SDimitry Andric LaneMask |= J->LaneMask; 596*0b57cec5SDimitry Andric Out->PhysReg = PhysReg; 597*0b57cec5SDimitry Andric Out->LaneMask = LaneMask; 598*0b57cec5SDimitry Andric } 599*0b57cec5SDimitry Andric LiveIns.erase(Out, LiveIns.end()); 600*0b57cec5SDimitry Andric } 601*0b57cec5SDimitry Andric 6025ffd83dbSDimitry Andric Register 6038bcb0991SDimitry Andric MachineBasicBlock::addLiveIn(MCRegister PhysReg, const TargetRegisterClass *RC) { 604*0b57cec5SDimitry Andric assert(getParent() && "MBB must be inserted in function"); 605e8d8bef9SDimitry Andric assert(Register::isPhysicalRegister(PhysReg) && "Expected physreg"); 606*0b57cec5SDimitry Andric assert(RC && "Register class is required"); 607*0b57cec5SDimitry Andric assert((isEHPad() || this == &getParent()->front()) && 608*0b57cec5SDimitry Andric "Only the entry block and landing pads can have physreg live ins"); 609*0b57cec5SDimitry Andric 610*0b57cec5SDimitry Andric bool LiveIn = isLiveIn(PhysReg); 611*0b57cec5SDimitry Andric iterator I = SkipPHIsAndLabels(begin()), E = end(); 612*0b57cec5SDimitry Andric MachineRegisterInfo &MRI = getParent()->getRegInfo(); 613*0b57cec5SDimitry Andric const TargetInstrInfo &TII = *getParent()->getSubtarget().getInstrInfo(); 614*0b57cec5SDimitry Andric 615*0b57cec5SDimitry Andric // Look for an existing copy. 616*0b57cec5SDimitry Andric if (LiveIn) 617*0b57cec5SDimitry Andric for (;I != E && I->isCopy(); ++I) 618*0b57cec5SDimitry Andric if (I->getOperand(1).getReg() == PhysReg) { 6198bcb0991SDimitry Andric Register VirtReg = I->getOperand(0).getReg(); 620*0b57cec5SDimitry Andric if (!MRI.constrainRegClass(VirtReg, RC)) 621*0b57cec5SDimitry Andric llvm_unreachable("Incompatible live-in register class."); 622*0b57cec5SDimitry Andric return VirtReg; 623*0b57cec5SDimitry Andric } 624*0b57cec5SDimitry Andric 625*0b57cec5SDimitry Andric // No luck, create a virtual register. 6268bcb0991SDimitry Andric Register VirtReg = MRI.createVirtualRegister(RC); 627*0b57cec5SDimitry Andric BuildMI(*this, I, DebugLoc(), TII.get(TargetOpcode::COPY), VirtReg) 628*0b57cec5SDimitry Andric .addReg(PhysReg, RegState::Kill); 629*0b57cec5SDimitry Andric if (!LiveIn) 630*0b57cec5SDimitry Andric addLiveIn(PhysReg); 631*0b57cec5SDimitry Andric return VirtReg; 632*0b57cec5SDimitry Andric } 633*0b57cec5SDimitry Andric 634*0b57cec5SDimitry Andric void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) { 635*0b57cec5SDimitry Andric getParent()->splice(NewAfter->getIterator(), getIterator()); 636*0b57cec5SDimitry Andric } 637*0b57cec5SDimitry Andric 638*0b57cec5SDimitry Andric void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) { 639*0b57cec5SDimitry Andric getParent()->splice(++NewBefore->getIterator(), getIterator()); 640*0b57cec5SDimitry Andric } 641*0b57cec5SDimitry Andric 6425ffd83dbSDimitry Andric void MachineBasicBlock::updateTerminator( 6435ffd83dbSDimitry Andric MachineBasicBlock *PreviousLayoutSuccessor) { 6445ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Updating terminators on " << printMBBReference(*this) 6455ffd83dbSDimitry Andric << "\n"); 6465ffd83dbSDimitry Andric 647*0b57cec5SDimitry Andric const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo(); 648*0b57cec5SDimitry Andric // A block with no successors has no concerns with fall-through edges. 649*0b57cec5SDimitry Andric if (this->succ_empty()) 650*0b57cec5SDimitry Andric return; 651*0b57cec5SDimitry Andric 652*0b57cec5SDimitry Andric MachineBasicBlock *TBB = nullptr, *FBB = nullptr; 653*0b57cec5SDimitry Andric SmallVector<MachineOperand, 4> Cond; 654*0b57cec5SDimitry Andric DebugLoc DL = findBranchDebugLoc(); 655*0b57cec5SDimitry Andric bool B = TII->analyzeBranch(*this, TBB, FBB, Cond); 656*0b57cec5SDimitry Andric (void) B; 657*0b57cec5SDimitry Andric assert(!B && "UpdateTerminators requires analyzable predecessors!"); 658*0b57cec5SDimitry Andric if (Cond.empty()) { 659*0b57cec5SDimitry Andric if (TBB) { 660*0b57cec5SDimitry Andric // The block has an unconditional branch. If its successor is now its 661*0b57cec5SDimitry Andric // layout successor, delete the branch. 662*0b57cec5SDimitry Andric if (isLayoutSuccessor(TBB)) 663*0b57cec5SDimitry Andric TII->removeBranch(*this); 664*0b57cec5SDimitry Andric } else { 6655ffd83dbSDimitry Andric // The block has an unconditional fallthrough, or the end of the block is 6665ffd83dbSDimitry Andric // unreachable. 667*0b57cec5SDimitry Andric 6685ffd83dbSDimitry Andric // Unfortunately, whether the end of the block is unreachable is not 6695ffd83dbSDimitry Andric // immediately obvious; we must fall back to checking the successor list, 6705ffd83dbSDimitry Andric // and assuming that if the passed in block is in the succesor list and 6715ffd83dbSDimitry Andric // not an EHPad, it must be the intended target. 6725ffd83dbSDimitry Andric if (!PreviousLayoutSuccessor || !isSuccessor(PreviousLayoutSuccessor) || 6735ffd83dbSDimitry Andric PreviousLayoutSuccessor->isEHPad()) 674*0b57cec5SDimitry Andric return; 675*0b57cec5SDimitry Andric 6765ffd83dbSDimitry Andric // If the unconditional successor block is not the current layout 6775ffd83dbSDimitry Andric // successor, insert a branch to jump to it. 6785ffd83dbSDimitry Andric if (!isLayoutSuccessor(PreviousLayoutSuccessor)) 6795ffd83dbSDimitry Andric TII->insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL); 680*0b57cec5SDimitry Andric } 681*0b57cec5SDimitry Andric return; 682*0b57cec5SDimitry Andric } 683*0b57cec5SDimitry Andric 684*0b57cec5SDimitry Andric if (FBB) { 685*0b57cec5SDimitry Andric // The block has a non-fallthrough conditional branch. If one of its 686*0b57cec5SDimitry Andric // successors is its layout successor, rewrite it to a fallthrough 687*0b57cec5SDimitry Andric // conditional branch. 688*0b57cec5SDimitry Andric if (isLayoutSuccessor(TBB)) { 689*0b57cec5SDimitry Andric if (TII->reverseBranchCondition(Cond)) 690*0b57cec5SDimitry Andric return; 691*0b57cec5SDimitry Andric TII->removeBranch(*this); 692*0b57cec5SDimitry Andric TII->insertBranch(*this, FBB, nullptr, Cond, DL); 693*0b57cec5SDimitry Andric } else if (isLayoutSuccessor(FBB)) { 694*0b57cec5SDimitry Andric TII->removeBranch(*this); 695*0b57cec5SDimitry Andric TII->insertBranch(*this, TBB, nullptr, Cond, DL); 696*0b57cec5SDimitry Andric } 697*0b57cec5SDimitry Andric return; 698*0b57cec5SDimitry Andric } 699*0b57cec5SDimitry Andric 7005ffd83dbSDimitry Andric // We now know we're going to fallthrough to PreviousLayoutSuccessor. 7015ffd83dbSDimitry Andric assert(PreviousLayoutSuccessor); 7025ffd83dbSDimitry Andric assert(!PreviousLayoutSuccessor->isEHPad()); 7035ffd83dbSDimitry Andric assert(isSuccessor(PreviousLayoutSuccessor)); 704*0b57cec5SDimitry Andric 7055ffd83dbSDimitry Andric if (PreviousLayoutSuccessor == TBB) { 7065ffd83dbSDimitry Andric // We had a fallthrough to the same basic block as the conditional jump 7075ffd83dbSDimitry Andric // targets. Remove the conditional jump, leaving an unconditional 7085ffd83dbSDimitry Andric // fallthrough or an unconditional jump. 709*0b57cec5SDimitry Andric TII->removeBranch(*this); 7105ffd83dbSDimitry Andric if (!isLayoutSuccessor(TBB)) { 711*0b57cec5SDimitry Andric Cond.clear(); 712*0b57cec5SDimitry Andric TII->insertBranch(*this, TBB, nullptr, Cond, DL); 7135ffd83dbSDimitry Andric } 714*0b57cec5SDimitry Andric return; 715*0b57cec5SDimitry Andric } 716*0b57cec5SDimitry Andric 717*0b57cec5SDimitry Andric // The block has a fallthrough conditional branch. 718*0b57cec5SDimitry Andric if (isLayoutSuccessor(TBB)) { 719*0b57cec5SDimitry Andric if (TII->reverseBranchCondition(Cond)) { 720*0b57cec5SDimitry Andric // We can't reverse the condition, add an unconditional branch. 721*0b57cec5SDimitry Andric Cond.clear(); 7225ffd83dbSDimitry Andric TII->insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL); 723*0b57cec5SDimitry Andric return; 724*0b57cec5SDimitry Andric } 725*0b57cec5SDimitry Andric TII->removeBranch(*this); 7265ffd83dbSDimitry Andric TII->insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL); 7275ffd83dbSDimitry Andric } else if (!isLayoutSuccessor(PreviousLayoutSuccessor)) { 728*0b57cec5SDimitry Andric TII->removeBranch(*this); 7295ffd83dbSDimitry Andric TII->insertBranch(*this, TBB, PreviousLayoutSuccessor, Cond, DL); 730*0b57cec5SDimitry Andric } 731*0b57cec5SDimitry Andric } 732*0b57cec5SDimitry Andric 733*0b57cec5SDimitry Andric void MachineBasicBlock::validateSuccProbs() const { 734*0b57cec5SDimitry Andric #ifndef NDEBUG 735*0b57cec5SDimitry Andric int64_t Sum = 0; 736*0b57cec5SDimitry Andric for (auto Prob : Probs) 737*0b57cec5SDimitry Andric Sum += Prob.getNumerator(); 738*0b57cec5SDimitry Andric // Due to precision issue, we assume that the sum of probabilities is one if 739*0b57cec5SDimitry Andric // the difference between the sum of their numerators and the denominator is 740*0b57cec5SDimitry Andric // no greater than the number of successors. 741*0b57cec5SDimitry Andric assert((uint64_t)std::abs(Sum - BranchProbability::getDenominator()) <= 742*0b57cec5SDimitry Andric Probs.size() && 743*0b57cec5SDimitry Andric "The sum of successors's probabilities exceeds one."); 744*0b57cec5SDimitry Andric #endif // NDEBUG 745*0b57cec5SDimitry Andric } 746*0b57cec5SDimitry Andric 747*0b57cec5SDimitry Andric void MachineBasicBlock::addSuccessor(MachineBasicBlock *Succ, 748*0b57cec5SDimitry Andric BranchProbability Prob) { 749*0b57cec5SDimitry Andric // Probability list is either empty (if successor list isn't empty, this means 750*0b57cec5SDimitry Andric // disabled optimization) or has the same size as successor list. 751*0b57cec5SDimitry Andric if (!(Probs.empty() && !Successors.empty())) 752*0b57cec5SDimitry Andric Probs.push_back(Prob); 753*0b57cec5SDimitry Andric Successors.push_back(Succ); 754*0b57cec5SDimitry Andric Succ->addPredecessor(this); 755*0b57cec5SDimitry Andric } 756*0b57cec5SDimitry Andric 757*0b57cec5SDimitry Andric void MachineBasicBlock::addSuccessorWithoutProb(MachineBasicBlock *Succ) { 758*0b57cec5SDimitry Andric // We need to make sure probability list is either empty or has the same size 759*0b57cec5SDimitry Andric // of successor list. When this function is called, we can safely delete all 760*0b57cec5SDimitry Andric // probability in the list. 761*0b57cec5SDimitry Andric Probs.clear(); 762*0b57cec5SDimitry Andric Successors.push_back(Succ); 763*0b57cec5SDimitry Andric Succ->addPredecessor(this); 764*0b57cec5SDimitry Andric } 765*0b57cec5SDimitry Andric 766*0b57cec5SDimitry Andric void MachineBasicBlock::splitSuccessor(MachineBasicBlock *Old, 767*0b57cec5SDimitry Andric MachineBasicBlock *New, 768*0b57cec5SDimitry Andric bool NormalizeSuccProbs) { 769*0b57cec5SDimitry Andric succ_iterator OldI = llvm::find(successors(), Old); 770*0b57cec5SDimitry Andric assert(OldI != succ_end() && "Old is not a successor of this block!"); 771e8d8bef9SDimitry Andric assert(!llvm::is_contained(successors(), New) && 772*0b57cec5SDimitry Andric "New is already a successor of this block!"); 773*0b57cec5SDimitry Andric 774*0b57cec5SDimitry Andric // Add a new successor with equal probability as the original one. Note 775*0b57cec5SDimitry Andric // that we directly copy the probability using the iterator rather than 776*0b57cec5SDimitry Andric // getting a potentially synthetic probability computed when unknown. This 777*0b57cec5SDimitry Andric // preserves the probabilities as-is and then we can renormalize them and 778*0b57cec5SDimitry Andric // query them effectively afterward. 779*0b57cec5SDimitry Andric addSuccessor(New, Probs.empty() ? BranchProbability::getUnknown() 780*0b57cec5SDimitry Andric : *getProbabilityIterator(OldI)); 781*0b57cec5SDimitry Andric if (NormalizeSuccProbs) 782*0b57cec5SDimitry Andric normalizeSuccProbs(); 783*0b57cec5SDimitry Andric } 784*0b57cec5SDimitry Andric 785*0b57cec5SDimitry Andric void MachineBasicBlock::removeSuccessor(MachineBasicBlock *Succ, 786*0b57cec5SDimitry Andric bool NormalizeSuccProbs) { 787*0b57cec5SDimitry Andric succ_iterator I = find(Successors, Succ); 788*0b57cec5SDimitry Andric removeSuccessor(I, NormalizeSuccProbs); 789*0b57cec5SDimitry Andric } 790*0b57cec5SDimitry Andric 791*0b57cec5SDimitry Andric MachineBasicBlock::succ_iterator 792*0b57cec5SDimitry Andric MachineBasicBlock::removeSuccessor(succ_iterator I, bool NormalizeSuccProbs) { 793*0b57cec5SDimitry Andric assert(I != Successors.end() && "Not a current successor!"); 794*0b57cec5SDimitry Andric 795*0b57cec5SDimitry Andric // If probability list is empty it means we don't use it (disabled 796*0b57cec5SDimitry Andric // optimization). 797*0b57cec5SDimitry Andric if (!Probs.empty()) { 798*0b57cec5SDimitry Andric probability_iterator WI = getProbabilityIterator(I); 799*0b57cec5SDimitry Andric Probs.erase(WI); 800*0b57cec5SDimitry Andric if (NormalizeSuccProbs) 801*0b57cec5SDimitry Andric normalizeSuccProbs(); 802*0b57cec5SDimitry Andric } 803*0b57cec5SDimitry Andric 804*0b57cec5SDimitry Andric (*I)->removePredecessor(this); 805*0b57cec5SDimitry Andric return Successors.erase(I); 806*0b57cec5SDimitry Andric } 807*0b57cec5SDimitry Andric 808*0b57cec5SDimitry Andric void MachineBasicBlock::replaceSuccessor(MachineBasicBlock *Old, 809*0b57cec5SDimitry Andric MachineBasicBlock *New) { 810*0b57cec5SDimitry Andric if (Old == New) 811*0b57cec5SDimitry Andric return; 812*0b57cec5SDimitry Andric 813*0b57cec5SDimitry Andric succ_iterator E = succ_end(); 814*0b57cec5SDimitry Andric succ_iterator NewI = E; 815*0b57cec5SDimitry Andric succ_iterator OldI = E; 816*0b57cec5SDimitry Andric for (succ_iterator I = succ_begin(); I != E; ++I) { 817*0b57cec5SDimitry Andric if (*I == Old) { 818*0b57cec5SDimitry Andric OldI = I; 819*0b57cec5SDimitry Andric if (NewI != E) 820*0b57cec5SDimitry Andric break; 821*0b57cec5SDimitry Andric } 822*0b57cec5SDimitry Andric if (*I == New) { 823*0b57cec5SDimitry Andric NewI = I; 824*0b57cec5SDimitry Andric if (OldI != E) 825*0b57cec5SDimitry Andric break; 826*0b57cec5SDimitry Andric } 827*0b57cec5SDimitry Andric } 828*0b57cec5SDimitry Andric assert(OldI != E && "Old is not a successor of this block"); 829*0b57cec5SDimitry Andric 830*0b57cec5SDimitry Andric // If New isn't already a successor, let it take Old's place. 831*0b57cec5SDimitry Andric if (NewI == E) { 832*0b57cec5SDimitry Andric Old->removePredecessor(this); 833*0b57cec5SDimitry Andric New->addPredecessor(this); 834*0b57cec5SDimitry Andric *OldI = New; 835*0b57cec5SDimitry Andric return; 836*0b57cec5SDimitry Andric } 837*0b57cec5SDimitry Andric 838*0b57cec5SDimitry Andric // New is already a successor. 839*0b57cec5SDimitry Andric // Update its probability instead of adding a duplicate edge. 840*0b57cec5SDimitry Andric if (!Probs.empty()) { 841*0b57cec5SDimitry Andric auto ProbIter = getProbabilityIterator(NewI); 842*0b57cec5SDimitry Andric if (!ProbIter->isUnknown()) 843*0b57cec5SDimitry Andric *ProbIter += *getProbabilityIterator(OldI); 844*0b57cec5SDimitry Andric } 845*0b57cec5SDimitry Andric removeSuccessor(OldI); 846*0b57cec5SDimitry Andric } 847*0b57cec5SDimitry Andric 848*0b57cec5SDimitry Andric void MachineBasicBlock::copySuccessor(MachineBasicBlock *Orig, 849*0b57cec5SDimitry Andric succ_iterator I) { 850e8d8bef9SDimitry Andric if (!Orig->Probs.empty()) 851*0b57cec5SDimitry Andric addSuccessor(*I, Orig->getSuccProbability(I)); 852*0b57cec5SDimitry Andric else 853*0b57cec5SDimitry Andric addSuccessorWithoutProb(*I); 854*0b57cec5SDimitry Andric } 855*0b57cec5SDimitry Andric 856*0b57cec5SDimitry Andric void MachineBasicBlock::addPredecessor(MachineBasicBlock *Pred) { 857*0b57cec5SDimitry Andric Predecessors.push_back(Pred); 858*0b57cec5SDimitry Andric } 859*0b57cec5SDimitry Andric 860*0b57cec5SDimitry Andric void MachineBasicBlock::removePredecessor(MachineBasicBlock *Pred) { 861*0b57cec5SDimitry Andric pred_iterator I = find(Predecessors, Pred); 862*0b57cec5SDimitry Andric assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); 863*0b57cec5SDimitry Andric Predecessors.erase(I); 864*0b57cec5SDimitry Andric } 865*0b57cec5SDimitry Andric 866*0b57cec5SDimitry Andric void MachineBasicBlock::transferSuccessors(MachineBasicBlock *FromMBB) { 867*0b57cec5SDimitry Andric if (this == FromMBB) 868*0b57cec5SDimitry Andric return; 869*0b57cec5SDimitry Andric 870*0b57cec5SDimitry Andric while (!FromMBB->succ_empty()) { 871*0b57cec5SDimitry Andric MachineBasicBlock *Succ = *FromMBB->succ_begin(); 872*0b57cec5SDimitry Andric 8738bcb0991SDimitry Andric // If probability list is empty it means we don't use it (disabled 8748bcb0991SDimitry Andric // optimization). 875*0b57cec5SDimitry Andric if (!FromMBB->Probs.empty()) { 876*0b57cec5SDimitry Andric auto Prob = *FromMBB->Probs.begin(); 877*0b57cec5SDimitry Andric addSuccessor(Succ, Prob); 878*0b57cec5SDimitry Andric } else 879*0b57cec5SDimitry Andric addSuccessorWithoutProb(Succ); 880*0b57cec5SDimitry Andric 881*0b57cec5SDimitry Andric FromMBB->removeSuccessor(Succ); 882*0b57cec5SDimitry Andric } 883*0b57cec5SDimitry Andric } 884*0b57cec5SDimitry Andric 885*0b57cec5SDimitry Andric void 886*0b57cec5SDimitry Andric MachineBasicBlock::transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB) { 887*0b57cec5SDimitry Andric if (this == FromMBB) 888*0b57cec5SDimitry Andric return; 889*0b57cec5SDimitry Andric 890*0b57cec5SDimitry Andric while (!FromMBB->succ_empty()) { 891*0b57cec5SDimitry Andric MachineBasicBlock *Succ = *FromMBB->succ_begin(); 892*0b57cec5SDimitry Andric if (!FromMBB->Probs.empty()) { 893*0b57cec5SDimitry Andric auto Prob = *FromMBB->Probs.begin(); 894*0b57cec5SDimitry Andric addSuccessor(Succ, Prob); 895*0b57cec5SDimitry Andric } else 896*0b57cec5SDimitry Andric addSuccessorWithoutProb(Succ); 897*0b57cec5SDimitry Andric FromMBB->removeSuccessor(Succ); 898*0b57cec5SDimitry Andric 899*0b57cec5SDimitry Andric // Fix up any PHI nodes in the successor. 9008bcb0991SDimitry Andric Succ->replacePhiUsesWith(FromMBB, this); 901*0b57cec5SDimitry Andric } 902*0b57cec5SDimitry Andric normalizeSuccProbs(); 903*0b57cec5SDimitry Andric } 904*0b57cec5SDimitry Andric 905*0b57cec5SDimitry Andric bool MachineBasicBlock::isPredecessor(const MachineBasicBlock *MBB) const { 906*0b57cec5SDimitry Andric return is_contained(predecessors(), MBB); 907*0b57cec5SDimitry Andric } 908*0b57cec5SDimitry Andric 909*0b57cec5SDimitry Andric bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const { 910*0b57cec5SDimitry Andric return is_contained(successors(), MBB); 911*0b57cec5SDimitry Andric } 912*0b57cec5SDimitry Andric 913*0b57cec5SDimitry Andric bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const { 914*0b57cec5SDimitry Andric MachineFunction::const_iterator I(this); 915*0b57cec5SDimitry Andric return std::next(I) == MachineFunction::const_iterator(MBB); 916*0b57cec5SDimitry Andric } 917*0b57cec5SDimitry Andric 91881ad6265SDimitry Andric const MachineBasicBlock *MachineBasicBlock::getSingleSuccessor() const { 91981ad6265SDimitry Andric return Successors.size() == 1 ? Successors[0] : nullptr; 92081ad6265SDimitry Andric } 92181ad6265SDimitry Andric 922*0b57cec5SDimitry Andric MachineBasicBlock *MachineBasicBlock::getFallThrough() { 923*0b57cec5SDimitry Andric MachineFunction::iterator Fallthrough = getIterator(); 924*0b57cec5SDimitry Andric ++Fallthrough; 925*0b57cec5SDimitry Andric // If FallthroughBlock is off the end of the function, it can't fall through. 926*0b57cec5SDimitry Andric if (Fallthrough == getParent()->end()) 927*0b57cec5SDimitry Andric return nullptr; 928*0b57cec5SDimitry Andric 929*0b57cec5SDimitry Andric // If FallthroughBlock isn't a successor, no fallthrough is possible. 930*0b57cec5SDimitry Andric if (!isSuccessor(&*Fallthrough)) 931*0b57cec5SDimitry Andric return nullptr; 932*0b57cec5SDimitry Andric 933*0b57cec5SDimitry Andric // Analyze the branches, if any, at the end of the block. 934*0b57cec5SDimitry Andric MachineBasicBlock *TBB = nullptr, *FBB = nullptr; 935*0b57cec5SDimitry Andric SmallVector<MachineOperand, 4> Cond; 936*0b57cec5SDimitry Andric const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo(); 937*0b57cec5SDimitry Andric if (TII->analyzeBranch(*this, TBB, FBB, Cond)) { 938*0b57cec5SDimitry Andric // If we couldn't analyze the branch, examine the last instruction. 939*0b57cec5SDimitry Andric // If the block doesn't end in a known control barrier, assume fallthrough 940*0b57cec5SDimitry Andric // is possible. The isPredicated check is needed because this code can be 941*0b57cec5SDimitry Andric // called during IfConversion, where an instruction which is normally a 942*0b57cec5SDimitry Andric // Barrier is predicated and thus no longer an actual control barrier. 943*0b57cec5SDimitry Andric return (empty() || !back().isBarrier() || TII->isPredicated(back())) 944*0b57cec5SDimitry Andric ? &*Fallthrough 945*0b57cec5SDimitry Andric : nullptr; 946*0b57cec5SDimitry Andric } 947*0b57cec5SDimitry Andric 948*0b57cec5SDimitry Andric // If there is no branch, control always falls through. 949*0b57cec5SDimitry Andric if (!TBB) return &*Fallthrough; 950*0b57cec5SDimitry Andric 951*0b57cec5SDimitry Andric // If there is some explicit branch to the fallthrough block, it can obviously 952*0b57cec5SDimitry Andric // reach, even though the branch should get folded to fall through implicitly. 953*0b57cec5SDimitry Andric if (MachineFunction::iterator(TBB) == Fallthrough || 954*0b57cec5SDimitry Andric MachineFunction::iterator(FBB) == Fallthrough) 955*0b57cec5SDimitry Andric return &*Fallthrough; 956*0b57cec5SDimitry Andric 957*0b57cec5SDimitry Andric // If it's an unconditional branch to some block not the fall through, it 958*0b57cec5SDimitry Andric // doesn't fall through. 959*0b57cec5SDimitry Andric if (Cond.empty()) return nullptr; 960*0b57cec5SDimitry Andric 961*0b57cec5SDimitry Andric // Otherwise, if it is conditional and has no explicit false block, it falls 962*0b57cec5SDimitry Andric // through. 963*0b57cec5SDimitry Andric return (FBB == nullptr) ? &*Fallthrough : nullptr; 964*0b57cec5SDimitry Andric } 965*0b57cec5SDimitry Andric 966*0b57cec5SDimitry Andric bool MachineBasicBlock::canFallThrough() { 967*0b57cec5SDimitry Andric return getFallThrough() != nullptr; 968*0b57cec5SDimitry Andric } 969*0b57cec5SDimitry Andric 970e8d8bef9SDimitry Andric MachineBasicBlock *MachineBasicBlock::splitAt(MachineInstr &MI, 971e8d8bef9SDimitry Andric bool UpdateLiveIns, 972e8d8bef9SDimitry Andric LiveIntervals *LIS) { 973e8d8bef9SDimitry Andric MachineBasicBlock::iterator SplitPoint(&MI); 974e8d8bef9SDimitry Andric ++SplitPoint; 975e8d8bef9SDimitry Andric 976e8d8bef9SDimitry Andric if (SplitPoint == end()) { 977e8d8bef9SDimitry Andric // Don't bother with a new block. 978e8d8bef9SDimitry Andric return this; 979e8d8bef9SDimitry Andric } 980e8d8bef9SDimitry Andric 981e8d8bef9SDimitry Andric MachineFunction *MF = getParent(); 982e8d8bef9SDimitry Andric 983e8d8bef9SDimitry Andric LivePhysRegs LiveRegs; 984e8d8bef9SDimitry Andric if (UpdateLiveIns) { 985e8d8bef9SDimitry Andric // Make sure we add any physregs we define in the block as liveins to the 986e8d8bef9SDimitry Andric // new block. 987e8d8bef9SDimitry Andric MachineBasicBlock::iterator Prev(&MI); 988e8d8bef9SDimitry Andric LiveRegs.init(*MF->getSubtarget().getRegisterInfo()); 989e8d8bef9SDimitry Andric LiveRegs.addLiveOuts(*this); 990e8d8bef9SDimitry Andric for (auto I = rbegin(), E = Prev.getReverse(); I != E; ++I) 991e8d8bef9SDimitry Andric LiveRegs.stepBackward(*I); 992e8d8bef9SDimitry Andric } 993e8d8bef9SDimitry Andric 994e8d8bef9SDimitry Andric MachineBasicBlock *SplitBB = MF->CreateMachineBasicBlock(getBasicBlock()); 995e8d8bef9SDimitry Andric 996e8d8bef9SDimitry Andric MF->insert(++MachineFunction::iterator(this), SplitBB); 997e8d8bef9SDimitry Andric SplitBB->splice(SplitBB->begin(), this, SplitPoint, end()); 998e8d8bef9SDimitry Andric 999e8d8bef9SDimitry Andric SplitBB->transferSuccessorsAndUpdatePHIs(this); 1000e8d8bef9SDimitry Andric addSuccessor(SplitBB); 1001e8d8bef9SDimitry Andric 1002e8d8bef9SDimitry Andric if (UpdateLiveIns) 1003e8d8bef9SDimitry Andric addLiveIns(*SplitBB, LiveRegs); 1004e8d8bef9SDimitry Andric 1005e8d8bef9SDimitry Andric if (LIS) 1006e8d8bef9SDimitry Andric LIS->insertMBBInMaps(SplitBB); 1007e8d8bef9SDimitry Andric 1008e8d8bef9SDimitry Andric return SplitBB; 1009e8d8bef9SDimitry Andric } 1010e8d8bef9SDimitry Andric 10115ffd83dbSDimitry Andric MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge( 10125ffd83dbSDimitry Andric MachineBasicBlock *Succ, Pass &P, 10135ffd83dbSDimitry Andric std::vector<SparseBitVector<>> *LiveInSets) { 1014*0b57cec5SDimitry Andric if (!canSplitCriticalEdge(Succ)) 1015*0b57cec5SDimitry Andric return nullptr; 1016*0b57cec5SDimitry Andric 1017*0b57cec5SDimitry Andric MachineFunction *MF = getParent(); 10185ffd83dbSDimitry Andric MachineBasicBlock *PrevFallthrough = getNextNode(); 1019*0b57cec5SDimitry Andric DebugLoc DL; // FIXME: this is nowhere 1020*0b57cec5SDimitry Andric 1021*0b57cec5SDimitry Andric MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock(); 1022*0b57cec5SDimitry Andric MF->insert(std::next(MachineFunction::iterator(this)), NMBB); 1023*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Splitting critical edge: " << printMBBReference(*this) 1024*0b57cec5SDimitry Andric << " -- " << printMBBReference(*NMBB) << " -- " 1025*0b57cec5SDimitry Andric << printMBBReference(*Succ) << '\n'); 1026*0b57cec5SDimitry Andric 1027*0b57cec5SDimitry Andric LiveIntervals *LIS = P.getAnalysisIfAvailable<LiveIntervals>(); 1028*0b57cec5SDimitry Andric SlotIndexes *Indexes = P.getAnalysisIfAvailable<SlotIndexes>(); 1029*0b57cec5SDimitry Andric if (LIS) 1030*0b57cec5SDimitry Andric LIS->insertMBBInMaps(NMBB); 1031*0b57cec5SDimitry Andric else if (Indexes) 1032*0b57cec5SDimitry Andric Indexes->insertMBBInMaps(NMBB); 1033*0b57cec5SDimitry Andric 1034*0b57cec5SDimitry Andric // On some targets like Mips, branches may kill virtual registers. Make sure 1035*0b57cec5SDimitry Andric // that LiveVariables is properly updated after updateTerminator replaces the 1036*0b57cec5SDimitry Andric // terminators. 1037*0b57cec5SDimitry Andric LiveVariables *LV = P.getAnalysisIfAvailable<LiveVariables>(); 1038*0b57cec5SDimitry Andric 1039*0b57cec5SDimitry Andric // Collect a list of virtual registers killed by the terminators. 10405ffd83dbSDimitry Andric SmallVector<Register, 4> KilledRegs; 1041*0b57cec5SDimitry Andric if (LV) 10420eae32dcSDimitry Andric for (MachineInstr &MI : 10430eae32dcSDimitry Andric llvm::make_range(getFirstInstrTerminator(), instr_end())) { 10440eae32dcSDimitry Andric for (MachineOperand &MO : MI.operands()) { 1045349cc55cSDimitry Andric if (!MO.isReg() || MO.getReg() == 0 || !MO.isUse() || !MO.isKill() || 1046349cc55cSDimitry Andric MO.isUndef()) 1047*0b57cec5SDimitry Andric continue; 1048349cc55cSDimitry Andric Register Reg = MO.getReg(); 10498bcb0991SDimitry Andric if (Register::isPhysicalRegister(Reg) || 10500eae32dcSDimitry Andric LV->getVarInfo(Reg).removeKill(MI)) { 1051*0b57cec5SDimitry Andric KilledRegs.push_back(Reg); 1052349cc55cSDimitry Andric LLVM_DEBUG(dbgs() << "Removing terminator kill: " << MI); 1053349cc55cSDimitry Andric MO.setIsKill(false); 1054*0b57cec5SDimitry Andric } 1055*0b57cec5SDimitry Andric } 1056*0b57cec5SDimitry Andric } 1057*0b57cec5SDimitry Andric 10585ffd83dbSDimitry Andric SmallVector<Register, 4> UsedRegs; 1059*0b57cec5SDimitry Andric if (LIS) { 10600eae32dcSDimitry Andric for (MachineInstr &MI : 10610eae32dcSDimitry Andric llvm::make_range(getFirstInstrTerminator(), instr_end())) { 10620eae32dcSDimitry Andric for (const MachineOperand &MO : MI.operands()) { 1063349cc55cSDimitry Andric if (!MO.isReg() || MO.getReg() == 0) 1064*0b57cec5SDimitry Andric continue; 1065*0b57cec5SDimitry Andric 1066349cc55cSDimitry Andric Register Reg = MO.getReg(); 1067*0b57cec5SDimitry Andric if (!is_contained(UsedRegs, Reg)) 1068*0b57cec5SDimitry Andric UsedRegs.push_back(Reg); 1069*0b57cec5SDimitry Andric } 1070*0b57cec5SDimitry Andric } 1071*0b57cec5SDimitry Andric } 1072*0b57cec5SDimitry Andric 1073*0b57cec5SDimitry Andric ReplaceUsesOfBlockWith(Succ, NMBB); 1074*0b57cec5SDimitry Andric 1075*0b57cec5SDimitry Andric // If updateTerminator() removes instructions, we need to remove them from 1076*0b57cec5SDimitry Andric // SlotIndexes. 1077*0b57cec5SDimitry Andric SmallVector<MachineInstr*, 4> Terminators; 1078*0b57cec5SDimitry Andric if (Indexes) { 10790eae32dcSDimitry Andric for (MachineInstr &MI : 10800eae32dcSDimitry Andric llvm::make_range(getFirstInstrTerminator(), instr_end())) 10810eae32dcSDimitry Andric Terminators.push_back(&MI); 1082*0b57cec5SDimitry Andric } 1083*0b57cec5SDimitry Andric 10845ffd83dbSDimitry Andric // Since we replaced all uses of Succ with NMBB, that should also be treated 10855ffd83dbSDimitry Andric // as the fallthrough successor 10865ffd83dbSDimitry Andric if (Succ == PrevFallthrough) 10875ffd83dbSDimitry Andric PrevFallthrough = NMBB; 10885ffd83dbSDimitry Andric updateTerminator(PrevFallthrough); 1089*0b57cec5SDimitry Andric 1090*0b57cec5SDimitry Andric if (Indexes) { 1091*0b57cec5SDimitry Andric SmallVector<MachineInstr*, 4> NewTerminators; 10920eae32dcSDimitry Andric for (MachineInstr &MI : 10930eae32dcSDimitry Andric llvm::make_range(getFirstInstrTerminator(), instr_end())) 10940eae32dcSDimitry Andric NewTerminators.push_back(&MI); 1095*0b57cec5SDimitry Andric 1096fe6060f1SDimitry Andric for (MachineInstr *Terminator : Terminators) { 1097fe6060f1SDimitry Andric if (!is_contained(NewTerminators, Terminator)) 1098fe6060f1SDimitry Andric Indexes->removeMachineInstrFromMaps(*Terminator); 1099*0b57cec5SDimitry Andric } 1100*0b57cec5SDimitry Andric } 1101*0b57cec5SDimitry Andric 1102*0b57cec5SDimitry Andric // Insert unconditional "jump Succ" instruction in NMBB if necessary. 1103*0b57cec5SDimitry Andric NMBB->addSuccessor(Succ); 1104*0b57cec5SDimitry Andric if (!NMBB->isLayoutSuccessor(Succ)) { 1105*0b57cec5SDimitry Andric SmallVector<MachineOperand, 4> Cond; 1106*0b57cec5SDimitry Andric const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo(); 1107*0b57cec5SDimitry Andric TII->insertBranch(*NMBB, Succ, nullptr, Cond, DL); 1108*0b57cec5SDimitry Andric 1109*0b57cec5SDimitry Andric if (Indexes) { 1110*0b57cec5SDimitry Andric for (MachineInstr &MI : NMBB->instrs()) { 1111*0b57cec5SDimitry Andric // Some instructions may have been moved to NMBB by updateTerminator(), 1112*0b57cec5SDimitry Andric // so we first remove any instruction that already has an index. 1113*0b57cec5SDimitry Andric if (Indexes->hasIndex(MI)) 1114*0b57cec5SDimitry Andric Indexes->removeMachineInstrFromMaps(MI); 1115*0b57cec5SDimitry Andric Indexes->insertMachineInstrInMaps(MI); 1116*0b57cec5SDimitry Andric } 1117*0b57cec5SDimitry Andric } 1118*0b57cec5SDimitry Andric } 1119*0b57cec5SDimitry Andric 11208bcb0991SDimitry Andric // Fix PHI nodes in Succ so they refer to NMBB instead of this. 11218bcb0991SDimitry Andric Succ->replacePhiUsesWith(this, NMBB); 1122*0b57cec5SDimitry Andric 1123*0b57cec5SDimitry Andric // Inherit live-ins from the successor 1124*0b57cec5SDimitry Andric for (const auto &LI : Succ->liveins()) 1125*0b57cec5SDimitry Andric NMBB->addLiveIn(LI); 1126*0b57cec5SDimitry Andric 1127*0b57cec5SDimitry Andric // Update LiveVariables. 1128*0b57cec5SDimitry Andric const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); 1129*0b57cec5SDimitry Andric if (LV) { 1130*0b57cec5SDimitry Andric // Restore kills of virtual registers that were killed by the terminators. 1131*0b57cec5SDimitry Andric while (!KilledRegs.empty()) { 11325ffd83dbSDimitry Andric Register Reg = KilledRegs.pop_back_val(); 1133*0b57cec5SDimitry Andric for (instr_iterator I = instr_end(), E = instr_begin(); I != E;) { 1134*0b57cec5SDimitry Andric if (!(--I)->addRegisterKilled(Reg, TRI, /* AddIfNotFound= */ false)) 1135*0b57cec5SDimitry Andric continue; 11368bcb0991SDimitry Andric if (Register::isVirtualRegister(Reg)) 1137*0b57cec5SDimitry Andric LV->getVarInfo(Reg).Kills.push_back(&*I); 1138*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Restored terminator kill: " << *I); 1139*0b57cec5SDimitry Andric break; 1140*0b57cec5SDimitry Andric } 1141*0b57cec5SDimitry Andric } 1142*0b57cec5SDimitry Andric // Update relevant live-through information. 11435ffd83dbSDimitry Andric if (LiveInSets != nullptr) 11445ffd83dbSDimitry Andric LV->addNewBlock(NMBB, this, Succ, *LiveInSets); 11455ffd83dbSDimitry Andric else 1146*0b57cec5SDimitry Andric LV->addNewBlock(NMBB, this, Succ); 1147*0b57cec5SDimitry Andric } 1148*0b57cec5SDimitry Andric 1149*0b57cec5SDimitry Andric if (LIS) { 1150*0b57cec5SDimitry Andric // After splitting the edge and updating SlotIndexes, live intervals may be 1151*0b57cec5SDimitry Andric // in one of two situations, depending on whether this block was the last in 1152*0b57cec5SDimitry Andric // the function. If the original block was the last in the function, all 1153*0b57cec5SDimitry Andric // live intervals will end prior to the beginning of the new split block. If 1154*0b57cec5SDimitry Andric // the original block was not at the end of the function, all live intervals 1155*0b57cec5SDimitry Andric // will extend to the end of the new split block. 1156*0b57cec5SDimitry Andric 1157*0b57cec5SDimitry Andric bool isLastMBB = 1158*0b57cec5SDimitry Andric std::next(MachineFunction::iterator(NMBB)) == getParent()->end(); 1159*0b57cec5SDimitry Andric 1160*0b57cec5SDimitry Andric SlotIndex StartIndex = Indexes->getMBBEndIdx(this); 1161*0b57cec5SDimitry Andric SlotIndex PrevIndex = StartIndex.getPrevSlot(); 1162*0b57cec5SDimitry Andric SlotIndex EndIndex = Indexes->getMBBEndIdx(NMBB); 1163*0b57cec5SDimitry Andric 1164*0b57cec5SDimitry Andric // Find the registers used from NMBB in PHIs in Succ. 11655ffd83dbSDimitry Andric SmallSet<Register, 8> PHISrcRegs; 1166*0b57cec5SDimitry Andric for (MachineBasicBlock::instr_iterator 1167*0b57cec5SDimitry Andric I = Succ->instr_begin(), E = Succ->instr_end(); 1168*0b57cec5SDimitry Andric I != E && I->isPHI(); ++I) { 1169*0b57cec5SDimitry Andric for (unsigned ni = 1, ne = I->getNumOperands(); ni != ne; ni += 2) { 1170*0b57cec5SDimitry Andric if (I->getOperand(ni+1).getMBB() == NMBB) { 1171*0b57cec5SDimitry Andric MachineOperand &MO = I->getOperand(ni); 11728bcb0991SDimitry Andric Register Reg = MO.getReg(); 1173*0b57cec5SDimitry Andric PHISrcRegs.insert(Reg); 1174*0b57cec5SDimitry Andric if (MO.isUndef()) 1175*0b57cec5SDimitry Andric continue; 1176*0b57cec5SDimitry Andric 1177*0b57cec5SDimitry Andric LiveInterval &LI = LIS->getInterval(Reg); 1178*0b57cec5SDimitry Andric VNInfo *VNI = LI.getVNInfoAt(PrevIndex); 1179*0b57cec5SDimitry Andric assert(VNI && 1180*0b57cec5SDimitry Andric "PHI sources should be live out of their predecessors."); 1181*0b57cec5SDimitry Andric LI.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI)); 1182*0b57cec5SDimitry Andric } 1183*0b57cec5SDimitry Andric } 1184*0b57cec5SDimitry Andric } 1185*0b57cec5SDimitry Andric 1186*0b57cec5SDimitry Andric MachineRegisterInfo *MRI = &getParent()->getRegInfo(); 1187*0b57cec5SDimitry Andric for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { 11885ffd83dbSDimitry Andric Register Reg = Register::index2VirtReg(i); 1189*0b57cec5SDimitry Andric if (PHISrcRegs.count(Reg) || !LIS->hasInterval(Reg)) 1190*0b57cec5SDimitry Andric continue; 1191*0b57cec5SDimitry Andric 1192*0b57cec5SDimitry Andric LiveInterval &LI = LIS->getInterval(Reg); 1193*0b57cec5SDimitry Andric if (!LI.liveAt(PrevIndex)) 1194*0b57cec5SDimitry Andric continue; 1195*0b57cec5SDimitry Andric 1196*0b57cec5SDimitry Andric bool isLiveOut = LI.liveAt(LIS->getMBBStartIdx(Succ)); 1197*0b57cec5SDimitry Andric if (isLiveOut && isLastMBB) { 1198*0b57cec5SDimitry Andric VNInfo *VNI = LI.getVNInfoAt(PrevIndex); 1199*0b57cec5SDimitry Andric assert(VNI && "LiveInterval should have VNInfo where it is live."); 1200*0b57cec5SDimitry Andric LI.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI)); 1201*0b57cec5SDimitry Andric } else if (!isLiveOut && !isLastMBB) { 1202*0b57cec5SDimitry Andric LI.removeSegment(StartIndex, EndIndex); 1203*0b57cec5SDimitry Andric } 1204*0b57cec5SDimitry Andric } 1205*0b57cec5SDimitry Andric 1206*0b57cec5SDimitry Andric // Update all intervals for registers whose uses may have been modified by 1207*0b57cec5SDimitry Andric // updateTerminator(). 1208*0b57cec5SDimitry Andric LIS->repairIntervalsInRange(this, getFirstTerminator(), end(), UsedRegs); 1209*0b57cec5SDimitry Andric } 1210*0b57cec5SDimitry Andric 1211*0b57cec5SDimitry Andric if (MachineDominatorTree *MDT = 1212*0b57cec5SDimitry Andric P.getAnalysisIfAvailable<MachineDominatorTree>()) 1213*0b57cec5SDimitry Andric MDT->recordSplitCriticalEdge(this, Succ, NMBB); 1214*0b57cec5SDimitry Andric 1215*0b57cec5SDimitry Andric if (MachineLoopInfo *MLI = P.getAnalysisIfAvailable<MachineLoopInfo>()) 1216*0b57cec5SDimitry Andric if (MachineLoop *TIL = MLI->getLoopFor(this)) { 1217*0b57cec5SDimitry Andric // If one or the other blocks were not in a loop, the new block is not 1218*0b57cec5SDimitry Andric // either, and thus LI doesn't need to be updated. 1219*0b57cec5SDimitry Andric if (MachineLoop *DestLoop = MLI->getLoopFor(Succ)) { 1220*0b57cec5SDimitry Andric if (TIL == DestLoop) { 1221*0b57cec5SDimitry Andric // Both in the same loop, the NMBB joins loop. 1222*0b57cec5SDimitry Andric DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase()); 1223*0b57cec5SDimitry Andric } else if (TIL->contains(DestLoop)) { 1224*0b57cec5SDimitry Andric // Edge from an outer loop to an inner loop. Add to the outer loop. 1225*0b57cec5SDimitry Andric TIL->addBasicBlockToLoop(NMBB, MLI->getBase()); 1226*0b57cec5SDimitry Andric } else if (DestLoop->contains(TIL)) { 1227*0b57cec5SDimitry Andric // Edge from an inner loop to an outer loop. Add to the outer loop. 1228*0b57cec5SDimitry Andric DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase()); 1229*0b57cec5SDimitry Andric } else { 1230*0b57cec5SDimitry Andric // Edge from two loops with no containment relation. Because these 1231*0b57cec5SDimitry Andric // are natural loops, we know that the destination block must be the 1232*0b57cec5SDimitry Andric // header of its loop (adding a branch into a loop elsewhere would 1233*0b57cec5SDimitry Andric // create an irreducible loop). 1234*0b57cec5SDimitry Andric assert(DestLoop->getHeader() == Succ && 1235*0b57cec5SDimitry Andric "Should not create irreducible loops!"); 1236*0b57cec5SDimitry Andric if (MachineLoop *P = DestLoop->getParentLoop()) 1237*0b57cec5SDimitry Andric P->addBasicBlockToLoop(NMBB, MLI->getBase()); 1238*0b57cec5SDimitry Andric } 1239*0b57cec5SDimitry Andric } 1240*0b57cec5SDimitry Andric } 1241*0b57cec5SDimitry Andric 1242*0b57cec5SDimitry Andric return NMBB; 1243*0b57cec5SDimitry Andric } 1244*0b57cec5SDimitry Andric 1245*0b57cec5SDimitry Andric bool MachineBasicBlock::canSplitCriticalEdge( 1246*0b57cec5SDimitry Andric const MachineBasicBlock *Succ) const { 1247*0b57cec5SDimitry Andric // Splitting the critical edge to a landing pad block is non-trivial. Don't do 1248*0b57cec5SDimitry Andric // it in this generic function. 1249*0b57cec5SDimitry Andric if (Succ->isEHPad()) 1250*0b57cec5SDimitry Andric return false; 1251*0b57cec5SDimitry Andric 12525ffd83dbSDimitry Andric // Splitting the critical edge to a callbr's indirect block isn't advised. 12535ffd83dbSDimitry Andric // Don't do it in this generic function. 12545ffd83dbSDimitry Andric if (Succ->isInlineAsmBrIndirectTarget()) 12555ffd83dbSDimitry Andric return false; 1256*0b57cec5SDimitry Andric 12575ffd83dbSDimitry Andric const MachineFunction *MF = getParent(); 1258*0b57cec5SDimitry Andric // Performance might be harmed on HW that implements branching using exec mask 1259*0b57cec5SDimitry Andric // where both sides of the branches are always executed. 1260*0b57cec5SDimitry Andric if (MF->getTarget().requiresStructuredCFG()) 1261*0b57cec5SDimitry Andric return false; 1262*0b57cec5SDimitry Andric 1263*0b57cec5SDimitry Andric // We may need to update this's terminator, but we can't do that if 12645ffd83dbSDimitry Andric // analyzeBranch fails. If this uses a jump table, we won't touch it. 1265*0b57cec5SDimitry Andric const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); 1266*0b57cec5SDimitry Andric MachineBasicBlock *TBB = nullptr, *FBB = nullptr; 1267*0b57cec5SDimitry Andric SmallVector<MachineOperand, 4> Cond; 1268*0b57cec5SDimitry Andric // AnalyzeBanch should modify this, since we did not allow modification. 1269*0b57cec5SDimitry Andric if (TII->analyzeBranch(*const_cast<MachineBasicBlock *>(this), TBB, FBB, Cond, 1270*0b57cec5SDimitry Andric /*AllowModify*/ false)) 1271*0b57cec5SDimitry Andric return false; 1272*0b57cec5SDimitry Andric 1273*0b57cec5SDimitry Andric // Avoid bugpoint weirdness: A block may end with a conditional branch but 1274*0b57cec5SDimitry Andric // jumps to the same MBB is either case. We have duplicate CFG edges in that 1275*0b57cec5SDimitry Andric // case that we can't handle. Since this never happens in properly optimized 1276*0b57cec5SDimitry Andric // code, just skip those edges. 1277*0b57cec5SDimitry Andric if (TBB && TBB == FBB) { 1278*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Won't split critical edge after degenerate " 1279*0b57cec5SDimitry Andric << printMBBReference(*this) << '\n'); 1280*0b57cec5SDimitry Andric return false; 1281*0b57cec5SDimitry Andric } 1282*0b57cec5SDimitry Andric return true; 1283*0b57cec5SDimitry Andric } 1284*0b57cec5SDimitry Andric 1285*0b57cec5SDimitry Andric /// Prepare MI to be removed from its bundle. This fixes bundle flags on MI's 1286*0b57cec5SDimitry Andric /// neighboring instructions so the bundle won't be broken by removing MI. 1287*0b57cec5SDimitry Andric static void unbundleSingleMI(MachineInstr *MI) { 1288*0b57cec5SDimitry Andric // Removing the first instruction in a bundle. 1289*0b57cec5SDimitry Andric if (MI->isBundledWithSucc() && !MI->isBundledWithPred()) 1290*0b57cec5SDimitry Andric MI->unbundleFromSucc(); 1291*0b57cec5SDimitry Andric // Removing the last instruction in a bundle. 1292*0b57cec5SDimitry Andric if (MI->isBundledWithPred() && !MI->isBundledWithSucc()) 1293*0b57cec5SDimitry Andric MI->unbundleFromPred(); 1294*0b57cec5SDimitry Andric // If MI is not bundled, or if it is internal to a bundle, the neighbor flags 1295*0b57cec5SDimitry Andric // are already fine. 1296*0b57cec5SDimitry Andric } 1297*0b57cec5SDimitry Andric 1298*0b57cec5SDimitry Andric MachineBasicBlock::instr_iterator 1299*0b57cec5SDimitry Andric MachineBasicBlock::erase(MachineBasicBlock::instr_iterator I) { 1300*0b57cec5SDimitry Andric unbundleSingleMI(&*I); 1301*0b57cec5SDimitry Andric return Insts.erase(I); 1302*0b57cec5SDimitry Andric } 1303*0b57cec5SDimitry Andric 1304*0b57cec5SDimitry Andric MachineInstr *MachineBasicBlock::remove_instr(MachineInstr *MI) { 1305*0b57cec5SDimitry Andric unbundleSingleMI(MI); 1306*0b57cec5SDimitry Andric MI->clearFlag(MachineInstr::BundledPred); 1307*0b57cec5SDimitry Andric MI->clearFlag(MachineInstr::BundledSucc); 1308*0b57cec5SDimitry Andric return Insts.remove(MI); 1309*0b57cec5SDimitry Andric } 1310*0b57cec5SDimitry Andric 1311*0b57cec5SDimitry Andric MachineBasicBlock::instr_iterator 1312*0b57cec5SDimitry Andric MachineBasicBlock::insert(instr_iterator I, MachineInstr *MI) { 1313*0b57cec5SDimitry Andric assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() && 1314*0b57cec5SDimitry Andric "Cannot insert instruction with bundle flags"); 1315*0b57cec5SDimitry Andric // Set the bundle flags when inserting inside a bundle. 1316*0b57cec5SDimitry Andric if (I != instr_end() && I->isBundledWithPred()) { 1317*0b57cec5SDimitry Andric MI->setFlag(MachineInstr::BundledPred); 1318*0b57cec5SDimitry Andric MI->setFlag(MachineInstr::BundledSucc); 1319*0b57cec5SDimitry Andric } 1320*0b57cec5SDimitry Andric return Insts.insert(I, MI); 1321*0b57cec5SDimitry Andric } 1322*0b57cec5SDimitry Andric 1323*0b57cec5SDimitry Andric /// This method unlinks 'this' from the containing function, and returns it, but 1324*0b57cec5SDimitry Andric /// does not delete it. 1325*0b57cec5SDimitry Andric MachineBasicBlock *MachineBasicBlock::removeFromParent() { 1326*0b57cec5SDimitry Andric assert(getParent() && "Not embedded in a function!"); 1327*0b57cec5SDimitry Andric getParent()->remove(this); 1328*0b57cec5SDimitry Andric return this; 1329*0b57cec5SDimitry Andric } 1330*0b57cec5SDimitry Andric 1331*0b57cec5SDimitry Andric /// This method unlinks 'this' from the containing function, and deletes it. 1332*0b57cec5SDimitry Andric void MachineBasicBlock::eraseFromParent() { 1333*0b57cec5SDimitry Andric assert(getParent() && "Not embedded in a function!"); 1334*0b57cec5SDimitry Andric getParent()->erase(this); 1335*0b57cec5SDimitry Andric } 1336*0b57cec5SDimitry Andric 1337*0b57cec5SDimitry Andric /// Given a machine basic block that branched to 'Old', change the code and CFG 1338*0b57cec5SDimitry Andric /// so that it branches to 'New' instead. 1339*0b57cec5SDimitry Andric void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old, 1340*0b57cec5SDimitry Andric MachineBasicBlock *New) { 1341*0b57cec5SDimitry Andric assert(Old != New && "Cannot replace self with self!"); 1342*0b57cec5SDimitry Andric 1343*0b57cec5SDimitry Andric MachineBasicBlock::instr_iterator I = instr_end(); 1344*0b57cec5SDimitry Andric while (I != instr_begin()) { 1345*0b57cec5SDimitry Andric --I; 1346*0b57cec5SDimitry Andric if (!I->isTerminator()) break; 1347*0b57cec5SDimitry Andric 1348*0b57cec5SDimitry Andric // Scan the operands of this machine instruction, replacing any uses of Old 1349*0b57cec5SDimitry Andric // with New. 1350*0b57cec5SDimitry Andric for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 1351*0b57cec5SDimitry Andric if (I->getOperand(i).isMBB() && 1352*0b57cec5SDimitry Andric I->getOperand(i).getMBB() == Old) 1353*0b57cec5SDimitry Andric I->getOperand(i).setMBB(New); 1354*0b57cec5SDimitry Andric } 1355*0b57cec5SDimitry Andric 1356*0b57cec5SDimitry Andric // Update the successor information. 1357*0b57cec5SDimitry Andric replaceSuccessor(Old, New); 1358*0b57cec5SDimitry Andric } 1359*0b57cec5SDimitry Andric 13608bcb0991SDimitry Andric void MachineBasicBlock::replacePhiUsesWith(MachineBasicBlock *Old, 13618bcb0991SDimitry Andric MachineBasicBlock *New) { 13628bcb0991SDimitry Andric for (MachineInstr &MI : phis()) 13638bcb0991SDimitry Andric for (unsigned i = 2, e = MI.getNumOperands() + 1; i != e; i += 2) { 13648bcb0991SDimitry Andric MachineOperand &MO = MI.getOperand(i); 13658bcb0991SDimitry Andric if (MO.getMBB() == Old) 13668bcb0991SDimitry Andric MO.setMBB(New); 13678bcb0991SDimitry Andric } 13688bcb0991SDimitry Andric } 13698bcb0991SDimitry Andric 1370*0b57cec5SDimitry Andric /// Find the next valid DebugLoc starting at MBBI, skipping any DBG_VALUE 1371*0b57cec5SDimitry Andric /// instructions. Return UnknownLoc if there is none. 1372*0b57cec5SDimitry Andric DebugLoc 1373*0b57cec5SDimitry Andric MachineBasicBlock::findDebugLoc(instr_iterator MBBI) { 1374*0b57cec5SDimitry Andric // Skip debug declarations, we don't want a DebugLoc from them. 1375*0b57cec5SDimitry Andric MBBI = skipDebugInstructionsForward(MBBI, instr_end()); 1376*0b57cec5SDimitry Andric if (MBBI != instr_end()) 1377*0b57cec5SDimitry Andric return MBBI->getDebugLoc(); 1378*0b57cec5SDimitry Andric return {}; 1379*0b57cec5SDimitry Andric } 1380*0b57cec5SDimitry Andric 1381fe6060f1SDimitry Andric DebugLoc MachineBasicBlock::rfindDebugLoc(reverse_instr_iterator MBBI) { 1382fe6060f1SDimitry Andric // Skip debug declarations, we don't want a DebugLoc from them. 1383fe6060f1SDimitry Andric MBBI = skipDebugInstructionsBackward(MBBI, instr_rbegin()); 1384fe6060f1SDimitry Andric if (!MBBI->isDebugInstr()) 1385fe6060f1SDimitry Andric return MBBI->getDebugLoc(); 1386fe6060f1SDimitry Andric return {}; 1387fe6060f1SDimitry Andric } 1388fe6060f1SDimitry Andric 1389*0b57cec5SDimitry Andric /// Find the previous valid DebugLoc preceding MBBI, skipping and DBG_VALUE 1390*0b57cec5SDimitry Andric /// instructions. Return UnknownLoc if there is none. 1391*0b57cec5SDimitry Andric DebugLoc MachineBasicBlock::findPrevDebugLoc(instr_iterator MBBI) { 1392*0b57cec5SDimitry Andric if (MBBI == instr_begin()) return {}; 13935ffd83dbSDimitry Andric // Skip debug instructions, we don't want a DebugLoc from them. 13945ffd83dbSDimitry Andric MBBI = prev_nodbg(MBBI, instr_begin()); 1395*0b57cec5SDimitry Andric if (!MBBI->isDebugInstr()) return MBBI->getDebugLoc(); 1396*0b57cec5SDimitry Andric return {}; 1397*0b57cec5SDimitry Andric } 1398*0b57cec5SDimitry Andric 1399fe6060f1SDimitry Andric DebugLoc MachineBasicBlock::rfindPrevDebugLoc(reverse_instr_iterator MBBI) { 1400fe6060f1SDimitry Andric if (MBBI == instr_rend()) 1401fe6060f1SDimitry Andric return {}; 1402fe6060f1SDimitry Andric // Skip debug declarations, we don't want a DebugLoc from them. 1403fe6060f1SDimitry Andric MBBI = next_nodbg(MBBI, instr_rend()); 1404fe6060f1SDimitry Andric if (MBBI != instr_rend()) 1405fe6060f1SDimitry Andric return MBBI->getDebugLoc(); 1406fe6060f1SDimitry Andric return {}; 1407fe6060f1SDimitry Andric } 1408fe6060f1SDimitry Andric 1409*0b57cec5SDimitry Andric /// Find and return the merged DebugLoc of the branch instructions of the block. 1410*0b57cec5SDimitry Andric /// Return UnknownLoc if there is none. 1411*0b57cec5SDimitry Andric DebugLoc 1412*0b57cec5SDimitry Andric MachineBasicBlock::findBranchDebugLoc() { 1413*0b57cec5SDimitry Andric DebugLoc DL; 1414*0b57cec5SDimitry Andric auto TI = getFirstTerminator(); 1415*0b57cec5SDimitry Andric while (TI != end() && !TI->isBranch()) 1416*0b57cec5SDimitry Andric ++TI; 1417*0b57cec5SDimitry Andric 1418*0b57cec5SDimitry Andric if (TI != end()) { 1419*0b57cec5SDimitry Andric DL = TI->getDebugLoc(); 1420*0b57cec5SDimitry Andric for (++TI ; TI != end() ; ++TI) 1421*0b57cec5SDimitry Andric if (TI->isBranch()) 1422*0b57cec5SDimitry Andric DL = DILocation::getMergedLocation(DL, TI->getDebugLoc()); 1423*0b57cec5SDimitry Andric } 1424*0b57cec5SDimitry Andric return DL; 1425*0b57cec5SDimitry Andric } 1426*0b57cec5SDimitry Andric 1427*0b57cec5SDimitry Andric /// Return probability of the edge from this block to MBB. 1428*0b57cec5SDimitry Andric BranchProbability 1429*0b57cec5SDimitry Andric MachineBasicBlock::getSuccProbability(const_succ_iterator Succ) const { 1430*0b57cec5SDimitry Andric if (Probs.empty()) 1431*0b57cec5SDimitry Andric return BranchProbability(1, succ_size()); 1432*0b57cec5SDimitry Andric 1433*0b57cec5SDimitry Andric const auto &Prob = *getProbabilityIterator(Succ); 1434*0b57cec5SDimitry Andric if (Prob.isUnknown()) { 1435*0b57cec5SDimitry Andric // For unknown probabilities, collect the sum of all known ones, and evenly 1436*0b57cec5SDimitry Andric // ditribute the complemental of the sum to each unknown probability. 1437*0b57cec5SDimitry Andric unsigned KnownProbNum = 0; 1438*0b57cec5SDimitry Andric auto Sum = BranchProbability::getZero(); 1439fcaf7f86SDimitry Andric for (const auto &P : Probs) { 1440*0b57cec5SDimitry Andric if (!P.isUnknown()) { 1441*0b57cec5SDimitry Andric Sum += P; 1442*0b57cec5SDimitry Andric KnownProbNum++; 1443*0b57cec5SDimitry Andric } 1444*0b57cec5SDimitry Andric } 1445*0b57cec5SDimitry Andric return Sum.getCompl() / (Probs.size() - KnownProbNum); 1446*0b57cec5SDimitry Andric } else 1447*0b57cec5SDimitry Andric return Prob; 1448*0b57cec5SDimitry Andric } 1449*0b57cec5SDimitry Andric 1450*0b57cec5SDimitry Andric /// Set successor probability of a given iterator. 1451*0b57cec5SDimitry Andric void MachineBasicBlock::setSuccProbability(succ_iterator I, 1452*0b57cec5SDimitry Andric BranchProbability Prob) { 1453*0b57cec5SDimitry Andric assert(!Prob.isUnknown()); 1454*0b57cec5SDimitry Andric if (Probs.empty()) 1455*0b57cec5SDimitry Andric return; 1456*0b57cec5SDimitry Andric *getProbabilityIterator(I) = Prob; 1457*0b57cec5SDimitry Andric } 1458*0b57cec5SDimitry Andric 1459*0b57cec5SDimitry Andric /// Return probability iterator corresonding to the I successor iterator 1460*0b57cec5SDimitry Andric MachineBasicBlock::const_probability_iterator 1461*0b57cec5SDimitry Andric MachineBasicBlock::getProbabilityIterator( 1462*0b57cec5SDimitry Andric MachineBasicBlock::const_succ_iterator I) const { 1463*0b57cec5SDimitry Andric assert(Probs.size() == Successors.size() && "Async probability list!"); 1464*0b57cec5SDimitry Andric const size_t index = std::distance(Successors.begin(), I); 1465*0b57cec5SDimitry Andric assert(index < Probs.size() && "Not a current successor!"); 1466*0b57cec5SDimitry Andric return Probs.begin() + index; 1467*0b57cec5SDimitry Andric } 1468*0b57cec5SDimitry Andric 1469*0b57cec5SDimitry Andric /// Return probability iterator corresonding to the I successor iterator. 1470*0b57cec5SDimitry Andric MachineBasicBlock::probability_iterator 1471*0b57cec5SDimitry Andric MachineBasicBlock::getProbabilityIterator(MachineBasicBlock::succ_iterator I) { 1472*0b57cec5SDimitry Andric assert(Probs.size() == Successors.size() && "Async probability list!"); 1473*0b57cec5SDimitry Andric const size_t index = std::distance(Successors.begin(), I); 1474*0b57cec5SDimitry Andric assert(index < Probs.size() && "Not a current successor!"); 1475*0b57cec5SDimitry Andric return Probs.begin() + index; 1476*0b57cec5SDimitry Andric } 1477*0b57cec5SDimitry Andric 1478*0b57cec5SDimitry Andric /// Return whether (physical) register "Reg" has been <def>ined and not <kill>ed 1479*0b57cec5SDimitry Andric /// as of just before "MI". 1480*0b57cec5SDimitry Andric /// 1481*0b57cec5SDimitry Andric /// Search is localised to a neighborhood of 1482*0b57cec5SDimitry Andric /// Neighborhood instructions before (searching for defs or kills) and N 1483*0b57cec5SDimitry Andric /// instructions after (searching just for defs) MI. 1484*0b57cec5SDimitry Andric MachineBasicBlock::LivenessQueryResult 1485*0b57cec5SDimitry Andric MachineBasicBlock::computeRegisterLiveness(const TargetRegisterInfo *TRI, 14865ffd83dbSDimitry Andric MCRegister Reg, const_iterator Before, 1487*0b57cec5SDimitry Andric unsigned Neighborhood) const { 1488*0b57cec5SDimitry Andric unsigned N = Neighborhood; 1489*0b57cec5SDimitry Andric 1490*0b57cec5SDimitry Andric // Try searching forwards from Before, looking for reads or defs. 1491*0b57cec5SDimitry Andric const_iterator I(Before); 1492*0b57cec5SDimitry Andric for (; I != end() && N > 0; ++I) { 1493fe6060f1SDimitry Andric if (I->isDebugOrPseudoInstr()) 1494*0b57cec5SDimitry Andric continue; 1495*0b57cec5SDimitry Andric 1496*0b57cec5SDimitry Andric --N; 1497*0b57cec5SDimitry Andric 1498480093f4SDimitry Andric PhysRegInfo Info = AnalyzePhysRegInBundle(*I, Reg, TRI); 1499*0b57cec5SDimitry Andric 1500*0b57cec5SDimitry Andric // Register is live when we read it here. 1501*0b57cec5SDimitry Andric if (Info.Read) 1502*0b57cec5SDimitry Andric return LQR_Live; 1503*0b57cec5SDimitry Andric // Register is dead if we can fully overwrite or clobber it here. 1504*0b57cec5SDimitry Andric if (Info.FullyDefined || Info.Clobbered) 1505*0b57cec5SDimitry Andric return LQR_Dead; 1506*0b57cec5SDimitry Andric } 1507*0b57cec5SDimitry Andric 1508*0b57cec5SDimitry Andric // If we reached the end, it is safe to clobber Reg at the end of a block of 1509*0b57cec5SDimitry Andric // no successor has it live in. 1510*0b57cec5SDimitry Andric if (I == end()) { 1511*0b57cec5SDimitry Andric for (MachineBasicBlock *S : successors()) { 1512*0b57cec5SDimitry Andric for (const MachineBasicBlock::RegisterMaskPair &LI : S->liveins()) { 1513*0b57cec5SDimitry Andric if (TRI->regsOverlap(LI.PhysReg, Reg)) 1514*0b57cec5SDimitry Andric return LQR_Live; 1515*0b57cec5SDimitry Andric } 1516*0b57cec5SDimitry Andric } 1517*0b57cec5SDimitry Andric 1518*0b57cec5SDimitry Andric return LQR_Dead; 1519*0b57cec5SDimitry Andric } 1520*0b57cec5SDimitry Andric 1521*0b57cec5SDimitry Andric 1522*0b57cec5SDimitry Andric N = Neighborhood; 1523*0b57cec5SDimitry Andric 1524*0b57cec5SDimitry Andric // Start by searching backwards from Before, looking for kills, reads or defs. 1525*0b57cec5SDimitry Andric I = const_iterator(Before); 1526*0b57cec5SDimitry Andric // If this is the first insn in the block, don't search backwards. 1527*0b57cec5SDimitry Andric if (I != begin()) { 1528*0b57cec5SDimitry Andric do { 1529*0b57cec5SDimitry Andric --I; 1530*0b57cec5SDimitry Andric 1531fe6060f1SDimitry Andric if (I->isDebugOrPseudoInstr()) 1532*0b57cec5SDimitry Andric continue; 1533*0b57cec5SDimitry Andric 1534*0b57cec5SDimitry Andric --N; 1535*0b57cec5SDimitry Andric 1536480093f4SDimitry Andric PhysRegInfo Info = AnalyzePhysRegInBundle(*I, Reg, TRI); 1537*0b57cec5SDimitry Andric 1538*0b57cec5SDimitry Andric // Defs happen after uses so they take precedence if both are present. 1539*0b57cec5SDimitry Andric 1540*0b57cec5SDimitry Andric // Register is dead after a dead def of the full register. 1541*0b57cec5SDimitry Andric if (Info.DeadDef) 1542*0b57cec5SDimitry Andric return LQR_Dead; 1543*0b57cec5SDimitry Andric // Register is (at least partially) live after a def. 1544*0b57cec5SDimitry Andric if (Info.Defined) { 1545*0b57cec5SDimitry Andric if (!Info.PartialDeadDef) 1546*0b57cec5SDimitry Andric return LQR_Live; 1547*0b57cec5SDimitry Andric // As soon as we saw a partial definition (dead or not), 1548*0b57cec5SDimitry Andric // we cannot tell if the value is partial live without 1549*0b57cec5SDimitry Andric // tracking the lanemasks. We are not going to do this, 1550*0b57cec5SDimitry Andric // so fall back on the remaining of the analysis. 1551*0b57cec5SDimitry Andric break; 1552*0b57cec5SDimitry Andric } 1553*0b57cec5SDimitry Andric // Register is dead after a full kill or clobber and no def. 1554*0b57cec5SDimitry Andric if (Info.Killed || Info.Clobbered) 1555*0b57cec5SDimitry Andric return LQR_Dead; 1556*0b57cec5SDimitry Andric // Register must be live if we read it. 1557*0b57cec5SDimitry Andric if (Info.Read) 1558*0b57cec5SDimitry Andric return LQR_Live; 1559*0b57cec5SDimitry Andric 1560*0b57cec5SDimitry Andric } while (I != begin() && N > 0); 1561*0b57cec5SDimitry Andric } 1562*0b57cec5SDimitry Andric 1563480093f4SDimitry Andric // If all the instructions before this in the block are debug instructions, 1564480093f4SDimitry Andric // skip over them. 1565fe6060f1SDimitry Andric while (I != begin() && std::prev(I)->isDebugOrPseudoInstr()) 1566480093f4SDimitry Andric --I; 1567480093f4SDimitry Andric 1568*0b57cec5SDimitry Andric // Did we get to the start of the block? 1569*0b57cec5SDimitry Andric if (I == begin()) { 1570*0b57cec5SDimitry Andric // If so, the register's state is definitely defined by the live-in state. 1571*0b57cec5SDimitry Andric for (const MachineBasicBlock::RegisterMaskPair &LI : liveins()) 1572*0b57cec5SDimitry Andric if (TRI->regsOverlap(LI.PhysReg, Reg)) 1573*0b57cec5SDimitry Andric return LQR_Live; 1574*0b57cec5SDimitry Andric 1575*0b57cec5SDimitry Andric return LQR_Dead; 1576*0b57cec5SDimitry Andric } 1577*0b57cec5SDimitry Andric 1578*0b57cec5SDimitry Andric // At this point we have no idea of the liveness of the register. 1579*0b57cec5SDimitry Andric return LQR_Unknown; 1580*0b57cec5SDimitry Andric } 1581*0b57cec5SDimitry Andric 1582*0b57cec5SDimitry Andric const uint32_t * 1583*0b57cec5SDimitry Andric MachineBasicBlock::getBeginClobberMask(const TargetRegisterInfo *TRI) const { 1584*0b57cec5SDimitry Andric // EH funclet entry does not preserve any registers. 1585*0b57cec5SDimitry Andric return isEHFuncletEntry() ? TRI->getNoPreservedMask() : nullptr; 1586*0b57cec5SDimitry Andric } 1587*0b57cec5SDimitry Andric 1588*0b57cec5SDimitry Andric const uint32_t * 1589*0b57cec5SDimitry Andric MachineBasicBlock::getEndClobberMask(const TargetRegisterInfo *TRI) const { 1590*0b57cec5SDimitry Andric // If we see a return block with successors, this must be a funclet return, 1591*0b57cec5SDimitry Andric // which does not preserve any registers. If there are no successors, we don't 1592*0b57cec5SDimitry Andric // care what kind of return it is, putting a mask after it is a no-op. 1593*0b57cec5SDimitry Andric return isReturnBlock() && !succ_empty() ? TRI->getNoPreservedMask() : nullptr; 1594*0b57cec5SDimitry Andric } 1595*0b57cec5SDimitry Andric 1596*0b57cec5SDimitry Andric void MachineBasicBlock::clearLiveIns() { 1597*0b57cec5SDimitry Andric LiveIns.clear(); 1598*0b57cec5SDimitry Andric } 1599*0b57cec5SDimitry Andric 1600*0b57cec5SDimitry Andric MachineBasicBlock::livein_iterator MachineBasicBlock::livein_begin() const { 1601*0b57cec5SDimitry Andric assert(getParent()->getProperties().hasProperty( 1602*0b57cec5SDimitry Andric MachineFunctionProperties::Property::TracksLiveness) && 1603*0b57cec5SDimitry Andric "Liveness information is accurate"); 1604*0b57cec5SDimitry Andric return LiveIns.begin(); 1605*0b57cec5SDimitry Andric } 16065ffd83dbSDimitry Andric 1607fe6060f1SDimitry Andric MachineBasicBlock::liveout_iterator MachineBasicBlock::liveout_begin() const { 1608fe6060f1SDimitry Andric const MachineFunction &MF = *getParent(); 1609fe6060f1SDimitry Andric assert(MF.getProperties().hasProperty( 1610fe6060f1SDimitry Andric MachineFunctionProperties::Property::TracksLiveness) && 1611fe6060f1SDimitry Andric "Liveness information is accurate"); 1612fe6060f1SDimitry Andric 1613fe6060f1SDimitry Andric const TargetLowering &TLI = *MF.getSubtarget().getTargetLowering(); 1614fe6060f1SDimitry Andric MCPhysReg ExceptionPointer = 0, ExceptionSelector = 0; 1615fe6060f1SDimitry Andric if (MF.getFunction().hasPersonalityFn()) { 1616fe6060f1SDimitry Andric auto PersonalityFn = MF.getFunction().getPersonalityFn(); 1617fe6060f1SDimitry Andric ExceptionPointer = TLI.getExceptionPointerRegister(PersonalityFn); 1618fe6060f1SDimitry Andric ExceptionSelector = TLI.getExceptionSelectorRegister(PersonalityFn); 1619fe6060f1SDimitry Andric } 1620fe6060f1SDimitry Andric 1621fe6060f1SDimitry Andric return liveout_iterator(*this, ExceptionPointer, ExceptionSelector, false); 1622fe6060f1SDimitry Andric } 1623fe6060f1SDimitry Andric 162481ad6265SDimitry Andric bool MachineBasicBlock::sizeWithoutDebugLargerThan(unsigned Limit) const { 162581ad6265SDimitry Andric unsigned Cntr = 0; 162681ad6265SDimitry Andric auto R = instructionsWithoutDebug(begin(), end()); 162781ad6265SDimitry Andric for (auto I = R.begin(), E = R.end(); I != E; ++I) { 162881ad6265SDimitry Andric if (++Cntr > Limit) 162981ad6265SDimitry Andric return true; 163081ad6265SDimitry Andric } 163181ad6265SDimitry Andric return false; 163281ad6265SDimitry Andric } 163381ad6265SDimitry Andric 16345ffd83dbSDimitry Andric const MBBSectionID MBBSectionID::ColdSectionID(MBBSectionID::SectionType::Cold); 16355ffd83dbSDimitry Andric const MBBSectionID 16365ffd83dbSDimitry Andric MBBSectionID::ExceptionSectionID(MBBSectionID::SectionType::Exception); 1637