10b57cec5SDimitry Andric //===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Collect the sequence of machine instructions for a basic block.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
140b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
150b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/LiveVariables.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/SlotIndexes.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
24*5f7ddb14SDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
270b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
280b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
290b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
300b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
310b57cec5SDimitry Andric #include "llvm/IR/ModuleSlotTracker.h"
320b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
330b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
340b57cec5SDimitry Andric #include "llvm/Support/DataTypes.h"
350b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
360b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
370b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
380b57cec5SDimitry Andric #include <algorithm>
390b57cec5SDimitry Andric using namespace llvm;
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric #define DEBUG_TYPE "codegen"
420b57cec5SDimitry Andric 
438bcb0991SDimitry Andric static cl::opt<bool> PrintSlotIndexes(
448bcb0991SDimitry Andric     "print-slotindexes",
458bcb0991SDimitry Andric     cl::desc("When printing machine IR, annotate instructions and blocks with "
468bcb0991SDimitry Andric              "SlotIndexes when available"),
478bcb0991SDimitry Andric     cl::init(true), cl::Hidden);
488bcb0991SDimitry Andric 
MachineBasicBlock(MachineFunction & MF,const BasicBlock * B)490b57cec5SDimitry Andric MachineBasicBlock::MachineBasicBlock(MachineFunction &MF, const BasicBlock *B)
500b57cec5SDimitry Andric     : BB(B), Number(-1), xParent(&MF) {
510b57cec5SDimitry Andric   Insts.Parent = this;
520b57cec5SDimitry Andric   if (B)
530b57cec5SDimitry Andric     IrrLoopHeaderWeight = B->getIrrLoopHeaderWeight();
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric 
~MachineBasicBlock()560b57cec5SDimitry Andric MachineBasicBlock::~MachineBasicBlock() {
570b57cec5SDimitry Andric }
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric /// Return the MCSymbol for this basic block.
getSymbol() const600b57cec5SDimitry Andric MCSymbol *MachineBasicBlock::getSymbol() const {
610b57cec5SDimitry Andric   if (!CachedMCSymbol) {
620b57cec5SDimitry Andric     const MachineFunction *MF = getParent();
630b57cec5SDimitry Andric     MCContext &Ctx = MF->getContext();
645ffd83dbSDimitry Andric 
65af732203SDimitry Andric     // We emit a non-temporary symbol -- with a descriptive name -- if it begins
66af732203SDimitry Andric     // a section (with basic block sections). Otherwise we fall back to use temp
67af732203SDimitry Andric     // label.
68af732203SDimitry Andric     if (MF->hasBBSections() && isBeginSection()) {
695ffd83dbSDimitry Andric       SmallString<5> Suffix;
705ffd83dbSDimitry Andric       if (SectionID == MBBSectionID::ColdSectionID) {
715ffd83dbSDimitry Andric         Suffix += ".cold";
725ffd83dbSDimitry Andric       } else if (SectionID == MBBSectionID::ExceptionSectionID) {
735ffd83dbSDimitry Andric         Suffix += ".eh";
745ffd83dbSDimitry Andric       } else {
75af732203SDimitry Andric         // For symbols that represent basic block sections, we add ".__part." to
76af732203SDimitry Andric         // allow tools like symbolizers to know that this represents a part of
77af732203SDimitry Andric         // the original function.
78af732203SDimitry Andric         Suffix = (Suffix + Twine(".__part.") + Twine(SectionID.Number)).str();
795ffd83dbSDimitry Andric       }
805ffd83dbSDimitry Andric       CachedMCSymbol = Ctx.getOrCreateSymbol(MF->getName() + Suffix);
815ffd83dbSDimitry Andric     } else {
82af732203SDimitry Andric       const StringRef Prefix = Ctx.getAsmInfo()->getPrivateLabelPrefix();
830b57cec5SDimitry Andric       CachedMCSymbol = Ctx.getOrCreateSymbol(Twine(Prefix) + "BB" +
840b57cec5SDimitry Andric                                              Twine(MF->getFunctionNumber()) +
850b57cec5SDimitry Andric                                              "_" + Twine(getNumber()));
860b57cec5SDimitry Andric     }
875ffd83dbSDimitry Andric   }
880b57cec5SDimitry Andric   return CachedMCSymbol;
890b57cec5SDimitry Andric }
900b57cec5SDimitry Andric 
getEHCatchretSymbol() const91*5f7ddb14SDimitry Andric MCSymbol *MachineBasicBlock::getEHCatchretSymbol() const {
92*5f7ddb14SDimitry Andric   if (!CachedEHCatchretMCSymbol) {
93*5f7ddb14SDimitry Andric     const MachineFunction *MF = getParent();
94*5f7ddb14SDimitry Andric     SmallString<128> SymbolName;
95*5f7ddb14SDimitry Andric     raw_svector_ostream(SymbolName)
96*5f7ddb14SDimitry Andric         << "$ehgcr_" << MF->getFunctionNumber() << '_' << getNumber();
97*5f7ddb14SDimitry Andric     CachedEHCatchretMCSymbol = MF->getContext().getOrCreateSymbol(SymbolName);
98*5f7ddb14SDimitry Andric   }
99*5f7ddb14SDimitry Andric   return CachedEHCatchretMCSymbol;
100*5f7ddb14SDimitry Andric }
101*5f7ddb14SDimitry Andric 
getEndSymbol() const102af732203SDimitry Andric MCSymbol *MachineBasicBlock::getEndSymbol() const {
103af732203SDimitry Andric   if (!CachedEndMCSymbol) {
104af732203SDimitry Andric     const MachineFunction *MF = getParent();
105af732203SDimitry Andric     MCContext &Ctx = MF->getContext();
106af732203SDimitry Andric     auto Prefix = Ctx.getAsmInfo()->getPrivateLabelPrefix();
107af732203SDimitry Andric     CachedEndMCSymbol = Ctx.getOrCreateSymbol(Twine(Prefix) + "BB_END" +
108af732203SDimitry Andric                                               Twine(MF->getFunctionNumber()) +
109af732203SDimitry Andric                                               "_" + Twine(getNumber()));
110af732203SDimitry Andric   }
111af732203SDimitry Andric   return CachedEndMCSymbol;
112af732203SDimitry Andric }
1130b57cec5SDimitry Andric 
operator <<(raw_ostream & OS,const MachineBasicBlock & MBB)1140b57cec5SDimitry Andric raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) {
1150b57cec5SDimitry Andric   MBB.print(OS);
1160b57cec5SDimitry Andric   return OS;
1170b57cec5SDimitry Andric }
1180b57cec5SDimitry Andric 
printMBBReference(const MachineBasicBlock & MBB)1190b57cec5SDimitry Andric Printable llvm::printMBBReference(const MachineBasicBlock &MBB) {
1200b57cec5SDimitry Andric   return Printable([&MBB](raw_ostream &OS) { return MBB.printAsOperand(OS); });
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric /// When an MBB is added to an MF, we need to update the parent pointer of the
1240b57cec5SDimitry Andric /// MBB, the MBB numbering, and any instructions in the MBB to be on the right
1250b57cec5SDimitry Andric /// operand list for registers.
1260b57cec5SDimitry Andric ///
1270b57cec5SDimitry Andric /// MBBs start out as #-1. When a MBB is added to a MachineFunction, it
1280b57cec5SDimitry Andric /// gets the next available unique MBB number. If it is removed from a
1290b57cec5SDimitry Andric /// MachineFunction, it goes back to being #-1.
addNodeToList(MachineBasicBlock * N)1300b57cec5SDimitry Andric void ilist_callback_traits<MachineBasicBlock>::addNodeToList(
1310b57cec5SDimitry Andric     MachineBasicBlock *N) {
1320b57cec5SDimitry Andric   MachineFunction &MF = *N->getParent();
1330b57cec5SDimitry Andric   N->Number = MF.addToMBBNumbering(N);
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric   // Make sure the instructions have their operands in the reginfo lists.
1360b57cec5SDimitry Andric   MachineRegisterInfo &RegInfo = MF.getRegInfo();
1370b57cec5SDimitry Andric   for (MachineBasicBlock::instr_iterator
1380b57cec5SDimitry Andric          I = N->instr_begin(), E = N->instr_end(); I != E; ++I)
1390b57cec5SDimitry Andric     I->AddRegOperandsToUseLists(RegInfo);
1400b57cec5SDimitry Andric }
1410b57cec5SDimitry Andric 
removeNodeFromList(MachineBasicBlock * N)1420b57cec5SDimitry Andric void ilist_callback_traits<MachineBasicBlock>::removeNodeFromList(
1430b57cec5SDimitry Andric     MachineBasicBlock *N) {
1440b57cec5SDimitry Andric   N->getParent()->removeFromMBBNumbering(N->Number);
1450b57cec5SDimitry Andric   N->Number = -1;
1460b57cec5SDimitry Andric }
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric /// When we add an instruction to a basic block list, we update its parent
1490b57cec5SDimitry Andric /// pointer and add its operands from reg use/def lists if appropriate.
addNodeToList(MachineInstr * N)1500b57cec5SDimitry Andric void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) {
1510b57cec5SDimitry Andric   assert(!N->getParent() && "machine instruction already in a basic block");
1520b57cec5SDimitry Andric   N->setParent(Parent);
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric   // Add the instruction's register operands to their corresponding
1550b57cec5SDimitry Andric   // use/def lists.
1560b57cec5SDimitry Andric   MachineFunction *MF = Parent->getParent();
1570b57cec5SDimitry Andric   N->AddRegOperandsToUseLists(MF->getRegInfo());
1580b57cec5SDimitry Andric   MF->handleInsertion(*N);
1590b57cec5SDimitry Andric }
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric /// When we remove an instruction from a basic block list, we update its parent
1620b57cec5SDimitry Andric /// pointer and remove its operands from reg use/def lists if appropriate.
removeNodeFromList(MachineInstr * N)1630b57cec5SDimitry Andric void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) {
1640b57cec5SDimitry Andric   assert(N->getParent() && "machine instruction not in a basic block");
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric   // Remove from the use/def lists.
1670b57cec5SDimitry Andric   if (MachineFunction *MF = N->getMF()) {
1680b57cec5SDimitry Andric     MF->handleRemoval(*N);
1690b57cec5SDimitry Andric     N->RemoveRegOperandsFromUseLists(MF->getRegInfo());
1700b57cec5SDimitry Andric   }
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric   N->setParent(nullptr);
1730b57cec5SDimitry Andric }
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric /// When moving a range of instructions from one MBB list to another, we need to
1760b57cec5SDimitry Andric /// update the parent pointers and the use/def lists.
transferNodesFromList(ilist_traits & FromList,instr_iterator First,instr_iterator Last)1770b57cec5SDimitry Andric void ilist_traits<MachineInstr>::transferNodesFromList(ilist_traits &FromList,
1780b57cec5SDimitry Andric                                                        instr_iterator First,
1790b57cec5SDimitry Andric                                                        instr_iterator Last) {
1800b57cec5SDimitry Andric   assert(Parent->getParent() == FromList.Parent->getParent() &&
1810b57cec5SDimitry Andric          "cannot transfer MachineInstrs between MachineFunctions");
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric   // If it's within the same BB, there's nothing to do.
1840b57cec5SDimitry Andric   if (this == &FromList)
1850b57cec5SDimitry Andric     return;
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric   assert(Parent != FromList.Parent && "Two lists have the same parent?");
1880b57cec5SDimitry Andric 
1890b57cec5SDimitry Andric   // If splicing between two blocks within the same function, just update the
1900b57cec5SDimitry Andric   // parent pointers.
1910b57cec5SDimitry Andric   for (; First != Last; ++First)
1920b57cec5SDimitry Andric     First->setParent(Parent);
1930b57cec5SDimitry Andric }
1940b57cec5SDimitry Andric 
deleteNode(MachineInstr * MI)1950b57cec5SDimitry Andric void ilist_traits<MachineInstr>::deleteNode(MachineInstr *MI) {
1960b57cec5SDimitry Andric   assert(!MI->getParent() && "MI is still in a block!");
1970b57cec5SDimitry Andric   Parent->getParent()->DeleteMachineInstr(MI);
1980b57cec5SDimitry Andric }
1990b57cec5SDimitry Andric 
getFirstNonPHI()2000b57cec5SDimitry Andric MachineBasicBlock::iterator MachineBasicBlock::getFirstNonPHI() {
2010b57cec5SDimitry Andric   instr_iterator I = instr_begin(), E = instr_end();
2020b57cec5SDimitry Andric   while (I != E && I->isPHI())
2030b57cec5SDimitry Andric     ++I;
2040b57cec5SDimitry Andric   assert((I == E || !I->isInsideBundle()) &&
2050b57cec5SDimitry Andric          "First non-phi MI cannot be inside a bundle!");
2060b57cec5SDimitry Andric   return I;
2070b57cec5SDimitry Andric }
2080b57cec5SDimitry Andric 
2090b57cec5SDimitry Andric MachineBasicBlock::iterator
SkipPHIsAndLabels(MachineBasicBlock::iterator I)2100b57cec5SDimitry Andric MachineBasicBlock::SkipPHIsAndLabels(MachineBasicBlock::iterator I) {
2110b57cec5SDimitry Andric   const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo();
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric   iterator E = end();
2140b57cec5SDimitry Andric   while (I != E && (I->isPHI() || I->isPosition() ||
2150b57cec5SDimitry Andric                     TII->isBasicBlockPrologue(*I)))
2160b57cec5SDimitry Andric     ++I;
2170b57cec5SDimitry Andric   // FIXME: This needs to change if we wish to bundle labels
2180b57cec5SDimitry Andric   // inside the bundle.
2190b57cec5SDimitry Andric   assert((I == E || !I->isInsideBundle()) &&
2200b57cec5SDimitry Andric          "First non-phi / non-label instruction is inside a bundle!");
2210b57cec5SDimitry Andric   return I;
2220b57cec5SDimitry Andric }
2230b57cec5SDimitry Andric 
2240b57cec5SDimitry Andric MachineBasicBlock::iterator
SkipPHIsLabelsAndDebug(MachineBasicBlock::iterator I,bool SkipPseudoOp)225*5f7ddb14SDimitry Andric MachineBasicBlock::SkipPHIsLabelsAndDebug(MachineBasicBlock::iterator I,
226*5f7ddb14SDimitry Andric                                           bool SkipPseudoOp) {
2270b57cec5SDimitry Andric   const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo();
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric   iterator E = end();
2300b57cec5SDimitry Andric   while (I != E && (I->isPHI() || I->isPosition() || I->isDebugInstr() ||
231*5f7ddb14SDimitry Andric                     (SkipPseudoOp && I->isPseudoProbe()) ||
2320b57cec5SDimitry Andric                     TII->isBasicBlockPrologue(*I)))
2330b57cec5SDimitry Andric     ++I;
2340b57cec5SDimitry Andric   // FIXME: This needs to change if we wish to bundle labels / dbg_values
2350b57cec5SDimitry Andric   // inside the bundle.
2360b57cec5SDimitry Andric   assert((I == E || !I->isInsideBundle()) &&
2370b57cec5SDimitry Andric          "First non-phi / non-label / non-debug "
2380b57cec5SDimitry Andric          "instruction is inside a bundle!");
2390b57cec5SDimitry Andric   return I;
2400b57cec5SDimitry Andric }
2410b57cec5SDimitry Andric 
getFirstTerminator()2420b57cec5SDimitry Andric MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
2430b57cec5SDimitry Andric   iterator B = begin(), E = end(), I = E;
2440b57cec5SDimitry Andric   while (I != B && ((--I)->isTerminator() || I->isDebugInstr()))
2450b57cec5SDimitry Andric     ; /*noop */
2460b57cec5SDimitry Andric   while (I != E && !I->isTerminator())
2470b57cec5SDimitry Andric     ++I;
2480b57cec5SDimitry Andric   return I;
2490b57cec5SDimitry Andric }
2500b57cec5SDimitry Andric 
getFirstInstrTerminator()2510b57cec5SDimitry Andric MachineBasicBlock::instr_iterator MachineBasicBlock::getFirstInstrTerminator() {
2520b57cec5SDimitry Andric   instr_iterator B = instr_begin(), E = instr_end(), I = E;
2530b57cec5SDimitry Andric   while (I != B && ((--I)->isTerminator() || I->isDebugInstr()))
2540b57cec5SDimitry Andric     ; /*noop */
2550b57cec5SDimitry Andric   while (I != E && !I->isTerminator())
2560b57cec5SDimitry Andric     ++I;
2570b57cec5SDimitry Andric   return I;
2580b57cec5SDimitry Andric }
2590b57cec5SDimitry Andric 
260*5f7ddb14SDimitry Andric MachineBasicBlock::iterator
getFirstNonDebugInstr(bool SkipPseudoOp)261*5f7ddb14SDimitry Andric MachineBasicBlock::getFirstNonDebugInstr(bool SkipPseudoOp) {
2620b57cec5SDimitry Andric   // Skip over begin-of-block dbg_value instructions.
263*5f7ddb14SDimitry Andric   return skipDebugInstructionsForward(begin(), end(), SkipPseudoOp);
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric 
266*5f7ddb14SDimitry Andric MachineBasicBlock::iterator
getLastNonDebugInstr(bool SkipPseudoOp)267*5f7ddb14SDimitry Andric MachineBasicBlock::getLastNonDebugInstr(bool SkipPseudoOp) {
2680b57cec5SDimitry Andric   // Skip over end-of-block dbg_value instructions.
2690b57cec5SDimitry Andric   instr_iterator B = instr_begin(), I = instr_end();
2700b57cec5SDimitry Andric   while (I != B) {
2710b57cec5SDimitry Andric     --I;
2720b57cec5SDimitry Andric     // Return instruction that starts a bundle.
2730b57cec5SDimitry Andric     if (I->isDebugInstr() || I->isInsideBundle())
2740b57cec5SDimitry Andric       continue;
275*5f7ddb14SDimitry Andric     if (SkipPseudoOp && I->isPseudoProbe())
276*5f7ddb14SDimitry Andric       continue;
2770b57cec5SDimitry Andric     return I;
2780b57cec5SDimitry Andric   }
2790b57cec5SDimitry Andric   // The block is all debug values.
2800b57cec5SDimitry Andric   return end();
2810b57cec5SDimitry Andric }
2820b57cec5SDimitry Andric 
hasEHPadSuccessor() const2830b57cec5SDimitry Andric bool MachineBasicBlock::hasEHPadSuccessor() const {
2840b57cec5SDimitry Andric   for (const_succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I)
2850b57cec5SDimitry Andric     if ((*I)->isEHPad())
2860b57cec5SDimitry Andric       return true;
2870b57cec5SDimitry Andric   return false;
2880b57cec5SDimitry Andric }
2890b57cec5SDimitry Andric 
isEntryBlock() const290af732203SDimitry Andric bool MachineBasicBlock::isEntryBlock() const {
291af732203SDimitry Andric   return getParent()->begin() == getIterator();
292af732203SDimitry Andric }
293af732203SDimitry Andric 
2940b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const2950b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineBasicBlock::dump() const {
2960b57cec5SDimitry Andric   print(dbgs());
2970b57cec5SDimitry Andric }
2980b57cec5SDimitry Andric #endif
2990b57cec5SDimitry Andric 
mayHaveInlineAsmBr() const3005ffd83dbSDimitry Andric bool MachineBasicBlock::mayHaveInlineAsmBr() const {
3015ffd83dbSDimitry Andric   for (const MachineBasicBlock *Succ : successors()) {
3025ffd83dbSDimitry Andric     if (Succ->isInlineAsmBrIndirectTarget())
3035ffd83dbSDimitry Andric       return true;
3045ffd83dbSDimitry Andric   }
3055ffd83dbSDimitry Andric   return false;
3065ffd83dbSDimitry Andric }
3075ffd83dbSDimitry Andric 
isLegalToHoistInto() const3080b57cec5SDimitry Andric bool MachineBasicBlock::isLegalToHoistInto() const {
3095ffd83dbSDimitry Andric   if (isReturnBlock() || hasEHPadSuccessor() || mayHaveInlineAsmBr())
3100b57cec5SDimitry Andric     return false;
3110b57cec5SDimitry Andric   return true;
3120b57cec5SDimitry Andric }
3130b57cec5SDimitry Andric 
getName() const3140b57cec5SDimitry Andric StringRef MachineBasicBlock::getName() const {
3150b57cec5SDimitry Andric   if (const BasicBlock *LBB = getBasicBlock())
3160b57cec5SDimitry Andric     return LBB->getName();
3170b57cec5SDimitry Andric   else
3180b57cec5SDimitry Andric     return StringRef("", 0);
3190b57cec5SDimitry Andric }
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric /// Return a hopefully unique identifier for this block.
getFullName() const3220b57cec5SDimitry Andric std::string MachineBasicBlock::getFullName() const {
3230b57cec5SDimitry Andric   std::string Name;
3240b57cec5SDimitry Andric   if (getParent())
3250b57cec5SDimitry Andric     Name = (getParent()->getName() + ":").str();
3260b57cec5SDimitry Andric   if (getBasicBlock())
3270b57cec5SDimitry Andric     Name += getBasicBlock()->getName();
3280b57cec5SDimitry Andric   else
3290b57cec5SDimitry Andric     Name += ("BB" + Twine(getNumber())).str();
3300b57cec5SDimitry Andric   return Name;
3310b57cec5SDimitry Andric }
3320b57cec5SDimitry Andric 
print(raw_ostream & OS,const SlotIndexes * Indexes,bool IsStandalone) const3330b57cec5SDimitry Andric void MachineBasicBlock::print(raw_ostream &OS, const SlotIndexes *Indexes,
3340b57cec5SDimitry Andric                               bool IsStandalone) const {
3350b57cec5SDimitry Andric   const MachineFunction *MF = getParent();
3360b57cec5SDimitry Andric   if (!MF) {
3370b57cec5SDimitry Andric     OS << "Can't print out MachineBasicBlock because parent MachineFunction"
3380b57cec5SDimitry Andric        << " is null\n";
3390b57cec5SDimitry Andric     return;
3400b57cec5SDimitry Andric   }
3410b57cec5SDimitry Andric   const Function &F = MF->getFunction();
3420b57cec5SDimitry Andric   const Module *M = F.getParent();
3430b57cec5SDimitry Andric   ModuleSlotTracker MST(M);
3440b57cec5SDimitry Andric   MST.incorporateFunction(F);
3450b57cec5SDimitry Andric   print(OS, MST, Indexes, IsStandalone);
3460b57cec5SDimitry Andric }
3470b57cec5SDimitry Andric 
print(raw_ostream & OS,ModuleSlotTracker & MST,const SlotIndexes * Indexes,bool IsStandalone) const3480b57cec5SDimitry Andric void MachineBasicBlock::print(raw_ostream &OS, ModuleSlotTracker &MST,
3490b57cec5SDimitry Andric                               const SlotIndexes *Indexes,
3500b57cec5SDimitry Andric                               bool IsStandalone) const {
3510b57cec5SDimitry Andric   const MachineFunction *MF = getParent();
3520b57cec5SDimitry Andric   if (!MF) {
3530b57cec5SDimitry Andric     OS << "Can't print out MachineBasicBlock because parent MachineFunction"
3540b57cec5SDimitry Andric        << " is null\n";
3550b57cec5SDimitry Andric     return;
3560b57cec5SDimitry Andric   }
3570b57cec5SDimitry Andric 
3588bcb0991SDimitry Andric   if (Indexes && PrintSlotIndexes)
3590b57cec5SDimitry Andric     OS << Indexes->getMBBStartIdx(this) << '\t';
3600b57cec5SDimitry Andric 
361af732203SDimitry Andric   printName(OS, PrintNameIr | PrintNameAttributes, &MST);
3620b57cec5SDimitry Andric   OS << ":\n";
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
3650b57cec5SDimitry Andric   const MachineRegisterInfo &MRI = MF->getRegInfo();
3660b57cec5SDimitry Andric   const TargetInstrInfo &TII = *getParent()->getSubtarget().getInstrInfo();
3670b57cec5SDimitry Andric   bool HasLineAttributes = false;
3680b57cec5SDimitry Andric 
3690b57cec5SDimitry Andric   // Print the preds of this block according to the CFG.
3700b57cec5SDimitry Andric   if (!pred_empty() && IsStandalone) {
3710b57cec5SDimitry Andric     if (Indexes) OS << '\t';
3720b57cec5SDimitry Andric     // Don't indent(2), align with previous line attributes.
3730b57cec5SDimitry Andric     OS << "; predecessors: ";
374af732203SDimitry Andric     ListSeparator LS;
375af732203SDimitry Andric     for (auto *Pred : predecessors())
376af732203SDimitry Andric       OS << LS << printMBBReference(*Pred);
3770b57cec5SDimitry Andric     OS << '\n';
3780b57cec5SDimitry Andric     HasLineAttributes = true;
3790b57cec5SDimitry Andric   }
3800b57cec5SDimitry Andric 
3810b57cec5SDimitry Andric   if (!succ_empty()) {
3820b57cec5SDimitry Andric     if (Indexes) OS << '\t';
3830b57cec5SDimitry Andric     // Print the successors
3840b57cec5SDimitry Andric     OS.indent(2) << "successors: ";
385af732203SDimitry Andric     ListSeparator LS;
3860b57cec5SDimitry Andric     for (auto I = succ_begin(), E = succ_end(); I != E; ++I) {
387af732203SDimitry Andric       OS << LS << printMBBReference(**I);
3880b57cec5SDimitry Andric       if (!Probs.empty())
3890b57cec5SDimitry Andric         OS << '('
3900b57cec5SDimitry Andric            << format("0x%08" PRIx32, getSuccProbability(I).getNumerator())
3910b57cec5SDimitry Andric            << ')';
3920b57cec5SDimitry Andric     }
3930b57cec5SDimitry Andric     if (!Probs.empty() && IsStandalone) {
3940b57cec5SDimitry Andric       // Print human readable probabilities as comments.
3950b57cec5SDimitry Andric       OS << "; ";
396af732203SDimitry Andric       ListSeparator LS;
3970b57cec5SDimitry Andric       for (auto I = succ_begin(), E = succ_end(); I != E; ++I) {
3980b57cec5SDimitry Andric         const BranchProbability &BP = getSuccProbability(I);
399af732203SDimitry Andric         OS << LS << printMBBReference(**I) << '('
4000b57cec5SDimitry Andric            << format("%.2f%%",
4010b57cec5SDimitry Andric                      rint(((double)BP.getNumerator() / BP.getDenominator()) *
4020b57cec5SDimitry Andric                           100.0 * 100.0) /
4030b57cec5SDimitry Andric                          100.0)
4040b57cec5SDimitry Andric            << ')';
4050b57cec5SDimitry Andric       }
4060b57cec5SDimitry Andric     }
4070b57cec5SDimitry Andric 
4080b57cec5SDimitry Andric     OS << '\n';
4090b57cec5SDimitry Andric     HasLineAttributes = true;
4100b57cec5SDimitry Andric   }
4110b57cec5SDimitry Andric 
4120b57cec5SDimitry Andric   if (!livein_empty() && MRI.tracksLiveness()) {
4130b57cec5SDimitry Andric     if (Indexes) OS << '\t';
4140b57cec5SDimitry Andric     OS.indent(2) << "liveins: ";
4150b57cec5SDimitry Andric 
416af732203SDimitry Andric     ListSeparator LS;
4170b57cec5SDimitry Andric     for (const auto &LI : liveins()) {
418af732203SDimitry Andric       OS << LS << printReg(LI.PhysReg, TRI);
4190b57cec5SDimitry Andric       if (!LI.LaneMask.all())
4200b57cec5SDimitry Andric         OS << ":0x" << PrintLaneMask(LI.LaneMask);
4210b57cec5SDimitry Andric     }
4220b57cec5SDimitry Andric     HasLineAttributes = true;
4230b57cec5SDimitry Andric   }
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric   if (HasLineAttributes)
4260b57cec5SDimitry Andric     OS << '\n';
4270b57cec5SDimitry Andric 
4280b57cec5SDimitry Andric   bool IsInBundle = false;
4290b57cec5SDimitry Andric   for (const MachineInstr &MI : instrs()) {
4308bcb0991SDimitry Andric     if (Indexes && PrintSlotIndexes) {
4310b57cec5SDimitry Andric       if (Indexes->hasIndex(MI))
4320b57cec5SDimitry Andric         OS << Indexes->getInstructionIndex(MI);
4330b57cec5SDimitry Andric       OS << '\t';
4340b57cec5SDimitry Andric     }
4350b57cec5SDimitry Andric 
4360b57cec5SDimitry Andric     if (IsInBundle && !MI.isInsideBundle()) {
4370b57cec5SDimitry Andric       OS.indent(2) << "}\n";
4380b57cec5SDimitry Andric       IsInBundle = false;
4390b57cec5SDimitry Andric     }
4400b57cec5SDimitry Andric 
4410b57cec5SDimitry Andric     OS.indent(IsInBundle ? 4 : 2);
4420b57cec5SDimitry Andric     MI.print(OS, MST, IsStandalone, /*SkipOpers=*/false, /*SkipDebugLoc=*/false,
4430b57cec5SDimitry Andric              /*AddNewLine=*/false, &TII);
4440b57cec5SDimitry Andric 
4450b57cec5SDimitry Andric     if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
4460b57cec5SDimitry Andric       OS << " {";
4470b57cec5SDimitry Andric       IsInBundle = true;
4480b57cec5SDimitry Andric     }
4490b57cec5SDimitry Andric     OS << '\n';
4500b57cec5SDimitry Andric   }
4510b57cec5SDimitry Andric 
4520b57cec5SDimitry Andric   if (IsInBundle)
4530b57cec5SDimitry Andric     OS.indent(2) << "}\n";
4540b57cec5SDimitry Andric 
4550b57cec5SDimitry Andric   if (IrrLoopHeaderWeight && IsStandalone) {
4560b57cec5SDimitry Andric     if (Indexes) OS << '\t';
4570b57cec5SDimitry Andric     OS.indent(2) << "; Irreducible loop header weight: "
4580b57cec5SDimitry Andric                  << IrrLoopHeaderWeight.getValue() << '\n';
4590b57cec5SDimitry Andric   }
4600b57cec5SDimitry Andric }
4610b57cec5SDimitry Andric 
462af732203SDimitry Andric /// Print the basic block's name as:
463af732203SDimitry Andric ///
464af732203SDimitry Andric ///    bb.{number}[.{ir-name}] [(attributes...)]
465af732203SDimitry Andric ///
466af732203SDimitry Andric /// The {ir-name} is only printed when the \ref PrintNameIr flag is passed
467af732203SDimitry Andric /// (which is the default). If the IR block has no name, it is identified
468af732203SDimitry Andric /// numerically using the attribute syntax as "(%ir-block.{ir-slot})".
469af732203SDimitry Andric ///
470af732203SDimitry Andric /// When the \ref PrintNameAttributes flag is passed, additional attributes
471af732203SDimitry Andric /// of the block are printed when set.
472af732203SDimitry Andric ///
473af732203SDimitry Andric /// \param printNameFlags Combination of \ref PrintNameFlag flags indicating
474af732203SDimitry Andric ///                       the parts to print.
475af732203SDimitry Andric /// \param moduleSlotTracker Optional ModuleSlotTracker. This method will
476af732203SDimitry Andric ///                          incorporate its own tracker when necessary to
477af732203SDimitry Andric ///                          determine the block's IR name.
printName(raw_ostream & os,unsigned printNameFlags,ModuleSlotTracker * moduleSlotTracker) const478af732203SDimitry Andric void MachineBasicBlock::printName(raw_ostream &os, unsigned printNameFlags,
479af732203SDimitry Andric                                   ModuleSlotTracker *moduleSlotTracker) const {
480af732203SDimitry Andric   os << "bb." << getNumber();
481af732203SDimitry Andric   bool hasAttributes = false;
482af732203SDimitry Andric 
483af732203SDimitry Andric   if (printNameFlags & PrintNameIr) {
484af732203SDimitry Andric     if (const auto *bb = getBasicBlock()) {
485af732203SDimitry Andric       if (bb->hasName()) {
486af732203SDimitry Andric         os << '.' << bb->getName();
487af732203SDimitry Andric       } else {
488af732203SDimitry Andric         hasAttributes = true;
489af732203SDimitry Andric         os << " (";
490af732203SDimitry Andric 
491af732203SDimitry Andric         int slot = -1;
492af732203SDimitry Andric 
493af732203SDimitry Andric         if (moduleSlotTracker) {
494af732203SDimitry Andric           slot = moduleSlotTracker->getLocalSlot(bb);
495af732203SDimitry Andric         } else if (bb->getParent()) {
496af732203SDimitry Andric           ModuleSlotTracker tmpTracker(bb->getModule(), false);
497af732203SDimitry Andric           tmpTracker.incorporateFunction(*bb->getParent());
498af732203SDimitry Andric           slot = tmpTracker.getLocalSlot(bb);
499af732203SDimitry Andric         }
500af732203SDimitry Andric 
501af732203SDimitry Andric         if (slot == -1)
502af732203SDimitry Andric           os << "<ir-block badref>";
503af732203SDimitry Andric         else
504af732203SDimitry Andric           os << (Twine("%ir-block.") + Twine(slot)).str();
505af732203SDimitry Andric       }
506af732203SDimitry Andric     }
507af732203SDimitry Andric   }
508af732203SDimitry Andric 
509af732203SDimitry Andric   if (printNameFlags & PrintNameAttributes) {
510af732203SDimitry Andric     if (hasAddressTaken()) {
511af732203SDimitry Andric       os << (hasAttributes ? ", " : " (");
512af732203SDimitry Andric       os << "address-taken";
513af732203SDimitry Andric       hasAttributes = true;
514af732203SDimitry Andric     }
515af732203SDimitry Andric     if (isEHPad()) {
516af732203SDimitry Andric       os << (hasAttributes ? ", " : " (");
517af732203SDimitry Andric       os << "landing-pad";
518af732203SDimitry Andric       hasAttributes = true;
519af732203SDimitry Andric     }
520af732203SDimitry Andric     if (isEHFuncletEntry()) {
521af732203SDimitry Andric       os << (hasAttributes ? ", " : " (");
522af732203SDimitry Andric       os << "ehfunclet-entry";
523af732203SDimitry Andric       hasAttributes = true;
524af732203SDimitry Andric     }
525af732203SDimitry Andric     if (getAlignment() != Align(1)) {
526af732203SDimitry Andric       os << (hasAttributes ? ", " : " (");
527af732203SDimitry Andric       os << "align " << getAlignment().value();
528af732203SDimitry Andric       hasAttributes = true;
529af732203SDimitry Andric     }
530af732203SDimitry Andric     if (getSectionID() != MBBSectionID(0)) {
531af732203SDimitry Andric       os << (hasAttributes ? ", " : " (");
532af732203SDimitry Andric       os << "bbsections ";
533af732203SDimitry Andric       switch (getSectionID().Type) {
534af732203SDimitry Andric       case MBBSectionID::SectionType::Exception:
535af732203SDimitry Andric         os << "Exception";
536af732203SDimitry Andric         break;
537af732203SDimitry Andric       case MBBSectionID::SectionType::Cold:
538af732203SDimitry Andric         os << "Cold";
539af732203SDimitry Andric         break;
540af732203SDimitry Andric       default:
541af732203SDimitry Andric         os << getSectionID().Number;
542af732203SDimitry Andric       }
543af732203SDimitry Andric       hasAttributes = true;
544af732203SDimitry Andric     }
545af732203SDimitry Andric   }
546af732203SDimitry Andric 
547af732203SDimitry Andric   if (hasAttributes)
548af732203SDimitry Andric     os << ')';
549af732203SDimitry Andric }
550af732203SDimitry Andric 
printAsOperand(raw_ostream & OS,bool) const5510b57cec5SDimitry Andric void MachineBasicBlock::printAsOperand(raw_ostream &OS,
5520b57cec5SDimitry Andric                                        bool /*PrintType*/) const {
553af732203SDimitry Andric   OS << '%';
554af732203SDimitry Andric   printName(OS, 0);
5550b57cec5SDimitry Andric }
5560b57cec5SDimitry Andric 
removeLiveIn(MCPhysReg Reg,LaneBitmask LaneMask)5570b57cec5SDimitry Andric void MachineBasicBlock::removeLiveIn(MCPhysReg Reg, LaneBitmask LaneMask) {
5580b57cec5SDimitry Andric   LiveInVector::iterator I = find_if(
5590b57cec5SDimitry Andric       LiveIns, [Reg](const RegisterMaskPair &LI) { return LI.PhysReg == Reg; });
5600b57cec5SDimitry Andric   if (I == LiveIns.end())
5610b57cec5SDimitry Andric     return;
5620b57cec5SDimitry Andric 
5630b57cec5SDimitry Andric   I->LaneMask &= ~LaneMask;
5640b57cec5SDimitry Andric   if (I->LaneMask.none())
5650b57cec5SDimitry Andric     LiveIns.erase(I);
5660b57cec5SDimitry Andric }
5670b57cec5SDimitry Andric 
5680b57cec5SDimitry Andric MachineBasicBlock::livein_iterator
removeLiveIn(MachineBasicBlock::livein_iterator I)5690b57cec5SDimitry Andric MachineBasicBlock::removeLiveIn(MachineBasicBlock::livein_iterator I) {
5700b57cec5SDimitry Andric   // Get non-const version of iterator.
5710b57cec5SDimitry Andric   LiveInVector::iterator LI = LiveIns.begin() + (I - LiveIns.begin());
5720b57cec5SDimitry Andric   return LiveIns.erase(LI);
5730b57cec5SDimitry Andric }
5740b57cec5SDimitry Andric 
isLiveIn(MCPhysReg Reg,LaneBitmask LaneMask) const5750b57cec5SDimitry Andric bool MachineBasicBlock::isLiveIn(MCPhysReg Reg, LaneBitmask LaneMask) const {
5760b57cec5SDimitry Andric   livein_iterator I = find_if(
5770b57cec5SDimitry Andric       LiveIns, [Reg](const RegisterMaskPair &LI) { return LI.PhysReg == Reg; });
5780b57cec5SDimitry Andric   return I != livein_end() && (I->LaneMask & LaneMask).any();
5790b57cec5SDimitry Andric }
5800b57cec5SDimitry Andric 
sortUniqueLiveIns()5810b57cec5SDimitry Andric void MachineBasicBlock::sortUniqueLiveIns() {
5820b57cec5SDimitry Andric   llvm::sort(LiveIns,
5830b57cec5SDimitry Andric              [](const RegisterMaskPair &LI0, const RegisterMaskPair &LI1) {
5840b57cec5SDimitry Andric                return LI0.PhysReg < LI1.PhysReg;
5850b57cec5SDimitry Andric              });
5860b57cec5SDimitry Andric   // Liveins are sorted by physreg now we can merge their lanemasks.
5870b57cec5SDimitry Andric   LiveInVector::const_iterator I = LiveIns.begin();
5880b57cec5SDimitry Andric   LiveInVector::const_iterator J;
5890b57cec5SDimitry Andric   LiveInVector::iterator Out = LiveIns.begin();
5900b57cec5SDimitry Andric   for (; I != LiveIns.end(); ++Out, I = J) {
5915ffd83dbSDimitry Andric     MCRegister PhysReg = I->PhysReg;
5920b57cec5SDimitry Andric     LaneBitmask LaneMask = I->LaneMask;
5930b57cec5SDimitry Andric     for (J = std::next(I); J != LiveIns.end() && J->PhysReg == PhysReg; ++J)
5940b57cec5SDimitry Andric       LaneMask |= J->LaneMask;
5950b57cec5SDimitry Andric     Out->PhysReg = PhysReg;
5960b57cec5SDimitry Andric     Out->LaneMask = LaneMask;
5970b57cec5SDimitry Andric   }
5980b57cec5SDimitry Andric   LiveIns.erase(Out, LiveIns.end());
5990b57cec5SDimitry Andric }
6000b57cec5SDimitry Andric 
6015ffd83dbSDimitry Andric Register
addLiveIn(MCRegister PhysReg,const TargetRegisterClass * RC)6028bcb0991SDimitry Andric MachineBasicBlock::addLiveIn(MCRegister PhysReg, const TargetRegisterClass *RC) {
6030b57cec5SDimitry Andric   assert(getParent() && "MBB must be inserted in function");
604af732203SDimitry Andric   assert(Register::isPhysicalRegister(PhysReg) && "Expected physreg");
6050b57cec5SDimitry Andric   assert(RC && "Register class is required");
6060b57cec5SDimitry Andric   assert((isEHPad() || this == &getParent()->front()) &&
6070b57cec5SDimitry Andric          "Only the entry block and landing pads can have physreg live ins");
6080b57cec5SDimitry Andric 
6090b57cec5SDimitry Andric   bool LiveIn = isLiveIn(PhysReg);
6100b57cec5SDimitry Andric   iterator I = SkipPHIsAndLabels(begin()), E = end();
6110b57cec5SDimitry Andric   MachineRegisterInfo &MRI = getParent()->getRegInfo();
6120b57cec5SDimitry Andric   const TargetInstrInfo &TII = *getParent()->getSubtarget().getInstrInfo();
6130b57cec5SDimitry Andric 
6140b57cec5SDimitry Andric   // Look for an existing copy.
6150b57cec5SDimitry Andric   if (LiveIn)
6160b57cec5SDimitry Andric     for (;I != E && I->isCopy(); ++I)
6170b57cec5SDimitry Andric       if (I->getOperand(1).getReg() == PhysReg) {
6188bcb0991SDimitry Andric         Register VirtReg = I->getOperand(0).getReg();
6190b57cec5SDimitry Andric         if (!MRI.constrainRegClass(VirtReg, RC))
6200b57cec5SDimitry Andric           llvm_unreachable("Incompatible live-in register class.");
6210b57cec5SDimitry Andric         return VirtReg;
6220b57cec5SDimitry Andric       }
6230b57cec5SDimitry Andric 
6240b57cec5SDimitry Andric   // No luck, create a virtual register.
6258bcb0991SDimitry Andric   Register VirtReg = MRI.createVirtualRegister(RC);
6260b57cec5SDimitry Andric   BuildMI(*this, I, DebugLoc(), TII.get(TargetOpcode::COPY), VirtReg)
6270b57cec5SDimitry Andric     .addReg(PhysReg, RegState::Kill);
6280b57cec5SDimitry Andric   if (!LiveIn)
6290b57cec5SDimitry Andric     addLiveIn(PhysReg);
6300b57cec5SDimitry Andric   return VirtReg;
6310b57cec5SDimitry Andric }
6320b57cec5SDimitry Andric 
moveBefore(MachineBasicBlock * NewAfter)6330b57cec5SDimitry Andric void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
6340b57cec5SDimitry Andric   getParent()->splice(NewAfter->getIterator(), getIterator());
6350b57cec5SDimitry Andric }
6360b57cec5SDimitry Andric 
moveAfter(MachineBasicBlock * NewBefore)6370b57cec5SDimitry Andric void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) {
6380b57cec5SDimitry Andric   getParent()->splice(++NewBefore->getIterator(), getIterator());
6390b57cec5SDimitry Andric }
6400b57cec5SDimitry Andric 
updateTerminator(MachineBasicBlock * PreviousLayoutSuccessor)6415ffd83dbSDimitry Andric void MachineBasicBlock::updateTerminator(
6425ffd83dbSDimitry Andric     MachineBasicBlock *PreviousLayoutSuccessor) {
6435ffd83dbSDimitry Andric   LLVM_DEBUG(dbgs() << "Updating terminators on " << printMBBReference(*this)
6445ffd83dbSDimitry Andric                     << "\n");
6455ffd83dbSDimitry Andric 
6460b57cec5SDimitry Andric   const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo();
6470b57cec5SDimitry Andric   // A block with no successors has no concerns with fall-through edges.
6480b57cec5SDimitry Andric   if (this->succ_empty())
6490b57cec5SDimitry Andric     return;
6500b57cec5SDimitry Andric 
6510b57cec5SDimitry Andric   MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
6520b57cec5SDimitry Andric   SmallVector<MachineOperand, 4> Cond;
6530b57cec5SDimitry Andric   DebugLoc DL = findBranchDebugLoc();
6540b57cec5SDimitry Andric   bool B = TII->analyzeBranch(*this, TBB, FBB, Cond);
6550b57cec5SDimitry Andric   (void) B;
6560b57cec5SDimitry Andric   assert(!B && "UpdateTerminators requires analyzable predecessors!");
6570b57cec5SDimitry Andric   if (Cond.empty()) {
6580b57cec5SDimitry Andric     if (TBB) {
6590b57cec5SDimitry Andric       // The block has an unconditional branch. If its successor is now its
6600b57cec5SDimitry Andric       // layout successor, delete the branch.
6610b57cec5SDimitry Andric       if (isLayoutSuccessor(TBB))
6620b57cec5SDimitry Andric         TII->removeBranch(*this);
6630b57cec5SDimitry Andric     } else {
6645ffd83dbSDimitry Andric       // The block has an unconditional fallthrough, or the end of the block is
6655ffd83dbSDimitry Andric       // unreachable.
6660b57cec5SDimitry Andric 
6675ffd83dbSDimitry Andric       // Unfortunately, whether the end of the block is unreachable is not
6685ffd83dbSDimitry Andric       // immediately obvious; we must fall back to checking the successor list,
6695ffd83dbSDimitry Andric       // and assuming that if the passed in block is in the succesor list and
6705ffd83dbSDimitry Andric       // not an EHPad, it must be the intended target.
6715ffd83dbSDimitry Andric       if (!PreviousLayoutSuccessor || !isSuccessor(PreviousLayoutSuccessor) ||
6725ffd83dbSDimitry Andric           PreviousLayoutSuccessor->isEHPad())
6730b57cec5SDimitry Andric         return;
6740b57cec5SDimitry Andric 
6755ffd83dbSDimitry Andric       // If the unconditional successor block is not the current layout
6765ffd83dbSDimitry Andric       // successor, insert a branch to jump to it.
6775ffd83dbSDimitry Andric       if (!isLayoutSuccessor(PreviousLayoutSuccessor))
6785ffd83dbSDimitry Andric         TII->insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL);
6790b57cec5SDimitry Andric     }
6800b57cec5SDimitry Andric     return;
6810b57cec5SDimitry Andric   }
6820b57cec5SDimitry Andric 
6830b57cec5SDimitry Andric   if (FBB) {
6840b57cec5SDimitry Andric     // The block has a non-fallthrough conditional branch. If one of its
6850b57cec5SDimitry Andric     // successors is its layout successor, rewrite it to a fallthrough
6860b57cec5SDimitry Andric     // conditional branch.
6870b57cec5SDimitry Andric     if (isLayoutSuccessor(TBB)) {
6880b57cec5SDimitry Andric       if (TII->reverseBranchCondition(Cond))
6890b57cec5SDimitry Andric         return;
6900b57cec5SDimitry Andric       TII->removeBranch(*this);
6910b57cec5SDimitry Andric       TII->insertBranch(*this, FBB, nullptr, Cond, DL);
6920b57cec5SDimitry Andric     } else if (isLayoutSuccessor(FBB)) {
6930b57cec5SDimitry Andric       TII->removeBranch(*this);
6940b57cec5SDimitry Andric       TII->insertBranch(*this, TBB, nullptr, Cond, DL);
6950b57cec5SDimitry Andric     }
6960b57cec5SDimitry Andric     return;
6970b57cec5SDimitry Andric   }
6980b57cec5SDimitry Andric 
6995ffd83dbSDimitry Andric   // We now know we're going to fallthrough to PreviousLayoutSuccessor.
7005ffd83dbSDimitry Andric   assert(PreviousLayoutSuccessor);
7015ffd83dbSDimitry Andric   assert(!PreviousLayoutSuccessor->isEHPad());
7025ffd83dbSDimitry Andric   assert(isSuccessor(PreviousLayoutSuccessor));
7030b57cec5SDimitry Andric 
7045ffd83dbSDimitry Andric   if (PreviousLayoutSuccessor == TBB) {
7055ffd83dbSDimitry Andric     // We had a fallthrough to the same basic block as the conditional jump
7065ffd83dbSDimitry Andric     // targets.  Remove the conditional jump, leaving an unconditional
7075ffd83dbSDimitry Andric     // fallthrough or an unconditional jump.
7080b57cec5SDimitry Andric     TII->removeBranch(*this);
7095ffd83dbSDimitry Andric     if (!isLayoutSuccessor(TBB)) {
7100b57cec5SDimitry Andric       Cond.clear();
7110b57cec5SDimitry Andric       TII->insertBranch(*this, TBB, nullptr, Cond, DL);
7125ffd83dbSDimitry Andric     }
7130b57cec5SDimitry Andric     return;
7140b57cec5SDimitry Andric   }
7150b57cec5SDimitry Andric 
7160b57cec5SDimitry Andric   // The block has a fallthrough conditional branch.
7170b57cec5SDimitry Andric   if (isLayoutSuccessor(TBB)) {
7180b57cec5SDimitry Andric     if (TII->reverseBranchCondition(Cond)) {
7190b57cec5SDimitry Andric       // We can't reverse the condition, add an unconditional branch.
7200b57cec5SDimitry Andric       Cond.clear();
7215ffd83dbSDimitry Andric       TII->insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL);
7220b57cec5SDimitry Andric       return;
7230b57cec5SDimitry Andric     }
7240b57cec5SDimitry Andric     TII->removeBranch(*this);
7255ffd83dbSDimitry Andric     TII->insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL);
7265ffd83dbSDimitry Andric   } else if (!isLayoutSuccessor(PreviousLayoutSuccessor)) {
7270b57cec5SDimitry Andric     TII->removeBranch(*this);
7285ffd83dbSDimitry Andric     TII->insertBranch(*this, TBB, PreviousLayoutSuccessor, Cond, DL);
7290b57cec5SDimitry Andric   }
7300b57cec5SDimitry Andric }
7310b57cec5SDimitry Andric 
validateSuccProbs() const7320b57cec5SDimitry Andric void MachineBasicBlock::validateSuccProbs() const {
7330b57cec5SDimitry Andric #ifndef NDEBUG
7340b57cec5SDimitry Andric   int64_t Sum = 0;
7350b57cec5SDimitry Andric   for (auto Prob : Probs)
7360b57cec5SDimitry Andric     Sum += Prob.getNumerator();
7370b57cec5SDimitry Andric   // Due to precision issue, we assume that the sum of probabilities is one if
7380b57cec5SDimitry Andric   // the difference between the sum of their numerators and the denominator is
7390b57cec5SDimitry Andric   // no greater than the number of successors.
7400b57cec5SDimitry Andric   assert((uint64_t)std::abs(Sum - BranchProbability::getDenominator()) <=
7410b57cec5SDimitry Andric              Probs.size() &&
7420b57cec5SDimitry Andric          "The sum of successors's probabilities exceeds one.");
7430b57cec5SDimitry Andric #endif // NDEBUG
7440b57cec5SDimitry Andric }
7450b57cec5SDimitry Andric 
addSuccessor(MachineBasicBlock * Succ,BranchProbability Prob)7460b57cec5SDimitry Andric void MachineBasicBlock::addSuccessor(MachineBasicBlock *Succ,
7470b57cec5SDimitry Andric                                      BranchProbability Prob) {
7480b57cec5SDimitry Andric   // Probability list is either empty (if successor list isn't empty, this means
7490b57cec5SDimitry Andric   // disabled optimization) or has the same size as successor list.
7500b57cec5SDimitry Andric   if (!(Probs.empty() && !Successors.empty()))
7510b57cec5SDimitry Andric     Probs.push_back(Prob);
7520b57cec5SDimitry Andric   Successors.push_back(Succ);
7530b57cec5SDimitry Andric   Succ->addPredecessor(this);
7540b57cec5SDimitry Andric }
7550b57cec5SDimitry Andric 
addSuccessorWithoutProb(MachineBasicBlock * Succ)7560b57cec5SDimitry Andric void MachineBasicBlock::addSuccessorWithoutProb(MachineBasicBlock *Succ) {
7570b57cec5SDimitry Andric   // We need to make sure probability list is either empty or has the same size
7580b57cec5SDimitry Andric   // of successor list. When this function is called, we can safely delete all
7590b57cec5SDimitry Andric   // probability in the list.
7600b57cec5SDimitry Andric   Probs.clear();
7610b57cec5SDimitry Andric   Successors.push_back(Succ);
7620b57cec5SDimitry Andric   Succ->addPredecessor(this);
7630b57cec5SDimitry Andric }
7640b57cec5SDimitry Andric 
splitSuccessor(MachineBasicBlock * Old,MachineBasicBlock * New,bool NormalizeSuccProbs)7650b57cec5SDimitry Andric void MachineBasicBlock::splitSuccessor(MachineBasicBlock *Old,
7660b57cec5SDimitry Andric                                        MachineBasicBlock *New,
7670b57cec5SDimitry Andric                                        bool NormalizeSuccProbs) {
7680b57cec5SDimitry Andric   succ_iterator OldI = llvm::find(successors(), Old);
7690b57cec5SDimitry Andric   assert(OldI != succ_end() && "Old is not a successor of this block!");
770af732203SDimitry Andric   assert(!llvm::is_contained(successors(), New) &&
7710b57cec5SDimitry Andric          "New is already a successor of this block!");
7720b57cec5SDimitry Andric 
7730b57cec5SDimitry Andric   // Add a new successor with equal probability as the original one. Note
7740b57cec5SDimitry Andric   // that we directly copy the probability using the iterator rather than
7750b57cec5SDimitry Andric   // getting a potentially synthetic probability computed when unknown. This
7760b57cec5SDimitry Andric   // preserves the probabilities as-is and then we can renormalize them and
7770b57cec5SDimitry Andric   // query them effectively afterward.
7780b57cec5SDimitry Andric   addSuccessor(New, Probs.empty() ? BranchProbability::getUnknown()
7790b57cec5SDimitry Andric                                   : *getProbabilityIterator(OldI));
7800b57cec5SDimitry Andric   if (NormalizeSuccProbs)
7810b57cec5SDimitry Andric     normalizeSuccProbs();
7820b57cec5SDimitry Andric }
7830b57cec5SDimitry Andric 
removeSuccessor(MachineBasicBlock * Succ,bool NormalizeSuccProbs)7840b57cec5SDimitry Andric void MachineBasicBlock::removeSuccessor(MachineBasicBlock *Succ,
7850b57cec5SDimitry Andric                                         bool NormalizeSuccProbs) {
7860b57cec5SDimitry Andric   succ_iterator I = find(Successors, Succ);
7870b57cec5SDimitry Andric   removeSuccessor(I, NormalizeSuccProbs);
7880b57cec5SDimitry Andric }
7890b57cec5SDimitry Andric 
7900b57cec5SDimitry Andric MachineBasicBlock::succ_iterator
removeSuccessor(succ_iterator I,bool NormalizeSuccProbs)7910b57cec5SDimitry Andric MachineBasicBlock::removeSuccessor(succ_iterator I, bool NormalizeSuccProbs) {
7920b57cec5SDimitry Andric   assert(I != Successors.end() && "Not a current successor!");
7930b57cec5SDimitry Andric 
7940b57cec5SDimitry Andric   // If probability list is empty it means we don't use it (disabled
7950b57cec5SDimitry Andric   // optimization).
7960b57cec5SDimitry Andric   if (!Probs.empty()) {
7970b57cec5SDimitry Andric     probability_iterator WI = getProbabilityIterator(I);
7980b57cec5SDimitry Andric     Probs.erase(WI);
7990b57cec5SDimitry Andric     if (NormalizeSuccProbs)
8000b57cec5SDimitry Andric       normalizeSuccProbs();
8010b57cec5SDimitry Andric   }
8020b57cec5SDimitry Andric 
8030b57cec5SDimitry Andric   (*I)->removePredecessor(this);
8040b57cec5SDimitry Andric   return Successors.erase(I);
8050b57cec5SDimitry Andric }
8060b57cec5SDimitry Andric 
replaceSuccessor(MachineBasicBlock * Old,MachineBasicBlock * New)8070b57cec5SDimitry Andric void MachineBasicBlock::replaceSuccessor(MachineBasicBlock *Old,
8080b57cec5SDimitry Andric                                          MachineBasicBlock *New) {
8090b57cec5SDimitry Andric   if (Old == New)
8100b57cec5SDimitry Andric     return;
8110b57cec5SDimitry Andric 
8120b57cec5SDimitry Andric   succ_iterator E = succ_end();
8130b57cec5SDimitry Andric   succ_iterator NewI = E;
8140b57cec5SDimitry Andric   succ_iterator OldI = E;
8150b57cec5SDimitry Andric   for (succ_iterator I = succ_begin(); I != E; ++I) {
8160b57cec5SDimitry Andric     if (*I == Old) {
8170b57cec5SDimitry Andric       OldI = I;
8180b57cec5SDimitry Andric       if (NewI != E)
8190b57cec5SDimitry Andric         break;
8200b57cec5SDimitry Andric     }
8210b57cec5SDimitry Andric     if (*I == New) {
8220b57cec5SDimitry Andric       NewI = I;
8230b57cec5SDimitry Andric       if (OldI != E)
8240b57cec5SDimitry Andric         break;
8250b57cec5SDimitry Andric     }
8260b57cec5SDimitry Andric   }
8270b57cec5SDimitry Andric   assert(OldI != E && "Old is not a successor of this block");
8280b57cec5SDimitry Andric 
8290b57cec5SDimitry Andric   // If New isn't already a successor, let it take Old's place.
8300b57cec5SDimitry Andric   if (NewI == E) {
8310b57cec5SDimitry Andric     Old->removePredecessor(this);
8320b57cec5SDimitry Andric     New->addPredecessor(this);
8330b57cec5SDimitry Andric     *OldI = New;
8340b57cec5SDimitry Andric     return;
8350b57cec5SDimitry Andric   }
8360b57cec5SDimitry Andric 
8370b57cec5SDimitry Andric   // New is already a successor.
8380b57cec5SDimitry Andric   // Update its probability instead of adding a duplicate edge.
8390b57cec5SDimitry Andric   if (!Probs.empty()) {
8400b57cec5SDimitry Andric     auto ProbIter = getProbabilityIterator(NewI);
8410b57cec5SDimitry Andric     if (!ProbIter->isUnknown())
8420b57cec5SDimitry Andric       *ProbIter += *getProbabilityIterator(OldI);
8430b57cec5SDimitry Andric   }
8440b57cec5SDimitry Andric   removeSuccessor(OldI);
8450b57cec5SDimitry Andric }
8460b57cec5SDimitry Andric 
copySuccessor(MachineBasicBlock * Orig,succ_iterator I)8470b57cec5SDimitry Andric void MachineBasicBlock::copySuccessor(MachineBasicBlock *Orig,
8480b57cec5SDimitry Andric                                       succ_iterator I) {
849af732203SDimitry Andric   if (!Orig->Probs.empty())
8500b57cec5SDimitry Andric     addSuccessor(*I, Orig->getSuccProbability(I));
8510b57cec5SDimitry Andric   else
8520b57cec5SDimitry Andric     addSuccessorWithoutProb(*I);
8530b57cec5SDimitry Andric }
8540b57cec5SDimitry Andric 
addPredecessor(MachineBasicBlock * Pred)8550b57cec5SDimitry Andric void MachineBasicBlock::addPredecessor(MachineBasicBlock *Pred) {
8560b57cec5SDimitry Andric   Predecessors.push_back(Pred);
8570b57cec5SDimitry Andric }
8580b57cec5SDimitry Andric 
removePredecessor(MachineBasicBlock * Pred)8590b57cec5SDimitry Andric void MachineBasicBlock::removePredecessor(MachineBasicBlock *Pred) {
8600b57cec5SDimitry Andric   pred_iterator I = find(Predecessors, Pred);
8610b57cec5SDimitry Andric   assert(I != Predecessors.end() && "Pred is not a predecessor of this block!");
8620b57cec5SDimitry Andric   Predecessors.erase(I);
8630b57cec5SDimitry Andric }
8640b57cec5SDimitry Andric 
transferSuccessors(MachineBasicBlock * FromMBB)8650b57cec5SDimitry Andric void MachineBasicBlock::transferSuccessors(MachineBasicBlock *FromMBB) {
8660b57cec5SDimitry Andric   if (this == FromMBB)
8670b57cec5SDimitry Andric     return;
8680b57cec5SDimitry Andric 
8690b57cec5SDimitry Andric   while (!FromMBB->succ_empty()) {
8700b57cec5SDimitry Andric     MachineBasicBlock *Succ = *FromMBB->succ_begin();
8710b57cec5SDimitry Andric 
8728bcb0991SDimitry Andric     // If probability list is empty it means we don't use it (disabled
8738bcb0991SDimitry Andric     // optimization).
8740b57cec5SDimitry Andric     if (!FromMBB->Probs.empty()) {
8750b57cec5SDimitry Andric       auto Prob = *FromMBB->Probs.begin();
8760b57cec5SDimitry Andric       addSuccessor(Succ, Prob);
8770b57cec5SDimitry Andric     } else
8780b57cec5SDimitry Andric       addSuccessorWithoutProb(Succ);
8790b57cec5SDimitry Andric 
8800b57cec5SDimitry Andric     FromMBB->removeSuccessor(Succ);
8810b57cec5SDimitry Andric   }
8820b57cec5SDimitry Andric }
8830b57cec5SDimitry Andric 
8840b57cec5SDimitry Andric void
transferSuccessorsAndUpdatePHIs(MachineBasicBlock * FromMBB)8850b57cec5SDimitry Andric MachineBasicBlock::transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB) {
8860b57cec5SDimitry Andric   if (this == FromMBB)
8870b57cec5SDimitry Andric     return;
8880b57cec5SDimitry Andric 
8890b57cec5SDimitry Andric   while (!FromMBB->succ_empty()) {
8900b57cec5SDimitry Andric     MachineBasicBlock *Succ = *FromMBB->succ_begin();
8910b57cec5SDimitry Andric     if (!FromMBB->Probs.empty()) {
8920b57cec5SDimitry Andric       auto Prob = *FromMBB->Probs.begin();
8930b57cec5SDimitry Andric       addSuccessor(Succ, Prob);
8940b57cec5SDimitry Andric     } else
8950b57cec5SDimitry Andric       addSuccessorWithoutProb(Succ);
8960b57cec5SDimitry Andric     FromMBB->removeSuccessor(Succ);
8970b57cec5SDimitry Andric 
8980b57cec5SDimitry Andric     // Fix up any PHI nodes in the successor.
8998bcb0991SDimitry Andric     Succ->replacePhiUsesWith(FromMBB, this);
9000b57cec5SDimitry Andric   }
9010b57cec5SDimitry Andric   normalizeSuccProbs();
9020b57cec5SDimitry Andric }
9030b57cec5SDimitry Andric 
isPredecessor(const MachineBasicBlock * MBB) const9040b57cec5SDimitry Andric bool MachineBasicBlock::isPredecessor(const MachineBasicBlock *MBB) const {
9050b57cec5SDimitry Andric   return is_contained(predecessors(), MBB);
9060b57cec5SDimitry Andric }
9070b57cec5SDimitry Andric 
isSuccessor(const MachineBasicBlock * MBB) const9080b57cec5SDimitry Andric bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const {
9090b57cec5SDimitry Andric   return is_contained(successors(), MBB);
9100b57cec5SDimitry Andric }
9110b57cec5SDimitry Andric 
isLayoutSuccessor(const MachineBasicBlock * MBB) const9120b57cec5SDimitry Andric bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const {
9130b57cec5SDimitry Andric   MachineFunction::const_iterator I(this);
9140b57cec5SDimitry Andric   return std::next(I) == MachineFunction::const_iterator(MBB);
9150b57cec5SDimitry Andric }
9160b57cec5SDimitry Andric 
getFallThrough()9170b57cec5SDimitry Andric MachineBasicBlock *MachineBasicBlock::getFallThrough() {
9180b57cec5SDimitry Andric   MachineFunction::iterator Fallthrough = getIterator();
9190b57cec5SDimitry Andric   ++Fallthrough;
9200b57cec5SDimitry Andric   // If FallthroughBlock is off the end of the function, it can't fall through.
9210b57cec5SDimitry Andric   if (Fallthrough == getParent()->end())
9220b57cec5SDimitry Andric     return nullptr;
9230b57cec5SDimitry Andric 
9240b57cec5SDimitry Andric   // If FallthroughBlock isn't a successor, no fallthrough is possible.
9250b57cec5SDimitry Andric   if (!isSuccessor(&*Fallthrough))
9260b57cec5SDimitry Andric     return nullptr;
9270b57cec5SDimitry Andric 
9280b57cec5SDimitry Andric   // Analyze the branches, if any, at the end of the block.
9290b57cec5SDimitry Andric   MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
9300b57cec5SDimitry Andric   SmallVector<MachineOperand, 4> Cond;
9310b57cec5SDimitry Andric   const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo();
9320b57cec5SDimitry Andric   if (TII->analyzeBranch(*this, TBB, FBB, Cond)) {
9330b57cec5SDimitry Andric     // If we couldn't analyze the branch, examine the last instruction.
9340b57cec5SDimitry Andric     // If the block doesn't end in a known control barrier, assume fallthrough
9350b57cec5SDimitry Andric     // is possible. The isPredicated check is needed because this code can be
9360b57cec5SDimitry Andric     // called during IfConversion, where an instruction which is normally a
9370b57cec5SDimitry Andric     // Barrier is predicated and thus no longer an actual control barrier.
9380b57cec5SDimitry Andric     return (empty() || !back().isBarrier() || TII->isPredicated(back()))
9390b57cec5SDimitry Andric                ? &*Fallthrough
9400b57cec5SDimitry Andric                : nullptr;
9410b57cec5SDimitry Andric   }
9420b57cec5SDimitry Andric 
9430b57cec5SDimitry Andric   // If there is no branch, control always falls through.
9440b57cec5SDimitry Andric   if (!TBB) return &*Fallthrough;
9450b57cec5SDimitry Andric 
9460b57cec5SDimitry Andric   // If there is some explicit branch to the fallthrough block, it can obviously
9470b57cec5SDimitry Andric   // reach, even though the branch should get folded to fall through implicitly.
9480b57cec5SDimitry Andric   if (MachineFunction::iterator(TBB) == Fallthrough ||
9490b57cec5SDimitry Andric       MachineFunction::iterator(FBB) == Fallthrough)
9500b57cec5SDimitry Andric     return &*Fallthrough;
9510b57cec5SDimitry Andric 
9520b57cec5SDimitry Andric   // If it's an unconditional branch to some block not the fall through, it
9530b57cec5SDimitry Andric   // doesn't fall through.
9540b57cec5SDimitry Andric   if (Cond.empty()) return nullptr;
9550b57cec5SDimitry Andric 
9560b57cec5SDimitry Andric   // Otherwise, if it is conditional and has no explicit false block, it falls
9570b57cec5SDimitry Andric   // through.
9580b57cec5SDimitry Andric   return (FBB == nullptr) ? &*Fallthrough : nullptr;
9590b57cec5SDimitry Andric }
9600b57cec5SDimitry Andric 
canFallThrough()9610b57cec5SDimitry Andric bool MachineBasicBlock::canFallThrough() {
9620b57cec5SDimitry Andric   return getFallThrough() != nullptr;
9630b57cec5SDimitry Andric }
9640b57cec5SDimitry Andric 
splitAt(MachineInstr & MI,bool UpdateLiveIns,LiveIntervals * LIS)965af732203SDimitry Andric MachineBasicBlock *MachineBasicBlock::splitAt(MachineInstr &MI,
966af732203SDimitry Andric                                               bool UpdateLiveIns,
967af732203SDimitry Andric                                               LiveIntervals *LIS) {
968af732203SDimitry Andric   MachineBasicBlock::iterator SplitPoint(&MI);
969af732203SDimitry Andric   ++SplitPoint;
970af732203SDimitry Andric 
971af732203SDimitry Andric   if (SplitPoint == end()) {
972af732203SDimitry Andric     // Don't bother with a new block.
973af732203SDimitry Andric     return this;
974af732203SDimitry Andric   }
975af732203SDimitry Andric 
976af732203SDimitry Andric   MachineFunction *MF = getParent();
977af732203SDimitry Andric 
978af732203SDimitry Andric   LivePhysRegs LiveRegs;
979af732203SDimitry Andric   if (UpdateLiveIns) {
980af732203SDimitry Andric     // Make sure we add any physregs we define in the block as liveins to the
981af732203SDimitry Andric     // new block.
982af732203SDimitry Andric     MachineBasicBlock::iterator Prev(&MI);
983af732203SDimitry Andric     LiveRegs.init(*MF->getSubtarget().getRegisterInfo());
984af732203SDimitry Andric     LiveRegs.addLiveOuts(*this);
985af732203SDimitry Andric     for (auto I = rbegin(), E = Prev.getReverse(); I != E; ++I)
986af732203SDimitry Andric       LiveRegs.stepBackward(*I);
987af732203SDimitry Andric   }
988af732203SDimitry Andric 
989af732203SDimitry Andric   MachineBasicBlock *SplitBB = MF->CreateMachineBasicBlock(getBasicBlock());
990af732203SDimitry Andric 
991af732203SDimitry Andric   MF->insert(++MachineFunction::iterator(this), SplitBB);
992af732203SDimitry Andric   SplitBB->splice(SplitBB->begin(), this, SplitPoint, end());
993af732203SDimitry Andric 
994af732203SDimitry Andric   SplitBB->transferSuccessorsAndUpdatePHIs(this);
995af732203SDimitry Andric   addSuccessor(SplitBB);
996af732203SDimitry Andric 
997af732203SDimitry Andric   if (UpdateLiveIns)
998af732203SDimitry Andric     addLiveIns(*SplitBB, LiveRegs);
999af732203SDimitry Andric 
1000af732203SDimitry Andric   if (LIS)
1001af732203SDimitry Andric     LIS->insertMBBInMaps(SplitBB);
1002af732203SDimitry Andric 
1003af732203SDimitry Andric   return SplitBB;
1004af732203SDimitry Andric }
1005af732203SDimitry Andric 
SplitCriticalEdge(MachineBasicBlock * Succ,Pass & P,std::vector<SparseBitVector<>> * LiveInSets)10065ffd83dbSDimitry Andric MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge(
10075ffd83dbSDimitry Andric     MachineBasicBlock *Succ, Pass &P,
10085ffd83dbSDimitry Andric     std::vector<SparseBitVector<>> *LiveInSets) {
10090b57cec5SDimitry Andric   if (!canSplitCriticalEdge(Succ))
10100b57cec5SDimitry Andric     return nullptr;
10110b57cec5SDimitry Andric 
10120b57cec5SDimitry Andric   MachineFunction *MF = getParent();
10135ffd83dbSDimitry Andric   MachineBasicBlock *PrevFallthrough = getNextNode();
10140b57cec5SDimitry Andric   DebugLoc DL;  // FIXME: this is nowhere
10150b57cec5SDimitry Andric 
10160b57cec5SDimitry Andric   MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock();
10170b57cec5SDimitry Andric   MF->insert(std::next(MachineFunction::iterator(this)), NMBB);
10180b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Splitting critical edge: " << printMBBReference(*this)
10190b57cec5SDimitry Andric                     << " -- " << printMBBReference(*NMBB) << " -- "
10200b57cec5SDimitry Andric                     << printMBBReference(*Succ) << '\n');
10210b57cec5SDimitry Andric 
10220b57cec5SDimitry Andric   LiveIntervals *LIS = P.getAnalysisIfAvailable<LiveIntervals>();
10230b57cec5SDimitry Andric   SlotIndexes *Indexes = P.getAnalysisIfAvailable<SlotIndexes>();
10240b57cec5SDimitry Andric   if (LIS)
10250b57cec5SDimitry Andric     LIS->insertMBBInMaps(NMBB);
10260b57cec5SDimitry Andric   else if (Indexes)
10270b57cec5SDimitry Andric     Indexes->insertMBBInMaps(NMBB);
10280b57cec5SDimitry Andric 
10290b57cec5SDimitry Andric   // On some targets like Mips, branches may kill virtual registers. Make sure
10300b57cec5SDimitry Andric   // that LiveVariables is properly updated after updateTerminator replaces the
10310b57cec5SDimitry Andric   // terminators.
10320b57cec5SDimitry Andric   LiveVariables *LV = P.getAnalysisIfAvailable<LiveVariables>();
10330b57cec5SDimitry Andric 
10340b57cec5SDimitry Andric   // Collect a list of virtual registers killed by the terminators.
10355ffd83dbSDimitry Andric   SmallVector<Register, 4> KilledRegs;
10360b57cec5SDimitry Andric   if (LV)
10370b57cec5SDimitry Andric     for (instr_iterator I = getFirstInstrTerminator(), E = instr_end();
10380b57cec5SDimitry Andric          I != E; ++I) {
10390b57cec5SDimitry Andric       MachineInstr *MI = &*I;
10400b57cec5SDimitry Andric       for (MachineInstr::mop_iterator OI = MI->operands_begin(),
10410b57cec5SDimitry Andric            OE = MI->operands_end(); OI != OE; ++OI) {
10420b57cec5SDimitry Andric         if (!OI->isReg() || OI->getReg() == 0 ||
10430b57cec5SDimitry Andric             !OI->isUse() || !OI->isKill() || OI->isUndef())
10440b57cec5SDimitry Andric           continue;
10458bcb0991SDimitry Andric         Register Reg = OI->getReg();
10468bcb0991SDimitry Andric         if (Register::isPhysicalRegister(Reg) ||
10470b57cec5SDimitry Andric             LV->getVarInfo(Reg).removeKill(*MI)) {
10480b57cec5SDimitry Andric           KilledRegs.push_back(Reg);
10490b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << "Removing terminator kill: " << *MI);
10500b57cec5SDimitry Andric           OI->setIsKill(false);
10510b57cec5SDimitry Andric         }
10520b57cec5SDimitry Andric       }
10530b57cec5SDimitry Andric     }
10540b57cec5SDimitry Andric 
10555ffd83dbSDimitry Andric   SmallVector<Register, 4> UsedRegs;
10560b57cec5SDimitry Andric   if (LIS) {
10570b57cec5SDimitry Andric     for (instr_iterator I = getFirstInstrTerminator(), E = instr_end();
10580b57cec5SDimitry Andric          I != E; ++I) {
10590b57cec5SDimitry Andric       MachineInstr *MI = &*I;
10600b57cec5SDimitry Andric 
10610b57cec5SDimitry Andric       for (MachineInstr::mop_iterator OI = MI->operands_begin(),
10620b57cec5SDimitry Andric            OE = MI->operands_end(); OI != OE; ++OI) {
10630b57cec5SDimitry Andric         if (!OI->isReg() || OI->getReg() == 0)
10640b57cec5SDimitry Andric           continue;
10650b57cec5SDimitry Andric 
10668bcb0991SDimitry Andric         Register Reg = OI->getReg();
10670b57cec5SDimitry Andric         if (!is_contained(UsedRegs, Reg))
10680b57cec5SDimitry Andric           UsedRegs.push_back(Reg);
10690b57cec5SDimitry Andric       }
10700b57cec5SDimitry Andric     }
10710b57cec5SDimitry Andric   }
10720b57cec5SDimitry Andric 
10730b57cec5SDimitry Andric   ReplaceUsesOfBlockWith(Succ, NMBB);
10740b57cec5SDimitry Andric 
10750b57cec5SDimitry Andric   // If updateTerminator() removes instructions, we need to remove them from
10760b57cec5SDimitry Andric   // SlotIndexes.
10770b57cec5SDimitry Andric   SmallVector<MachineInstr*, 4> Terminators;
10780b57cec5SDimitry Andric   if (Indexes) {
10790b57cec5SDimitry Andric     for (instr_iterator I = getFirstInstrTerminator(), E = instr_end();
10800b57cec5SDimitry Andric          I != E; ++I)
10810b57cec5SDimitry Andric       Terminators.push_back(&*I);
10820b57cec5SDimitry Andric   }
10830b57cec5SDimitry 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);
10890b57cec5SDimitry Andric 
10900b57cec5SDimitry Andric   if (Indexes) {
10910b57cec5SDimitry Andric     SmallVector<MachineInstr*, 4> NewTerminators;
10920b57cec5SDimitry Andric     for (instr_iterator I = getFirstInstrTerminator(), E = instr_end();
10930b57cec5SDimitry Andric          I != E; ++I)
10940b57cec5SDimitry Andric       NewTerminators.push_back(&*I);
10950b57cec5SDimitry Andric 
1096*5f7ddb14SDimitry Andric     for (MachineInstr *Terminator : Terminators) {
1097*5f7ddb14SDimitry Andric       if (!is_contained(NewTerminators, Terminator))
1098*5f7ddb14SDimitry Andric         Indexes->removeMachineInstrFromMaps(*Terminator);
10990b57cec5SDimitry Andric     }
11000b57cec5SDimitry Andric   }
11010b57cec5SDimitry Andric 
11020b57cec5SDimitry Andric   // Insert unconditional "jump Succ" instruction in NMBB if necessary.
11030b57cec5SDimitry Andric   NMBB->addSuccessor(Succ);
11040b57cec5SDimitry Andric   if (!NMBB->isLayoutSuccessor(Succ)) {
11050b57cec5SDimitry Andric     SmallVector<MachineOperand, 4> Cond;
11060b57cec5SDimitry Andric     const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo();
11070b57cec5SDimitry Andric     TII->insertBranch(*NMBB, Succ, nullptr, Cond, DL);
11080b57cec5SDimitry Andric 
11090b57cec5SDimitry Andric     if (Indexes) {
11100b57cec5SDimitry Andric       for (MachineInstr &MI : NMBB->instrs()) {
11110b57cec5SDimitry Andric         // Some instructions may have been moved to NMBB by updateTerminator(),
11120b57cec5SDimitry Andric         // so we first remove any instruction that already has an index.
11130b57cec5SDimitry Andric         if (Indexes->hasIndex(MI))
11140b57cec5SDimitry Andric           Indexes->removeMachineInstrFromMaps(MI);
11150b57cec5SDimitry Andric         Indexes->insertMachineInstrInMaps(MI);
11160b57cec5SDimitry Andric       }
11170b57cec5SDimitry Andric     }
11180b57cec5SDimitry Andric   }
11190b57cec5SDimitry Andric 
11208bcb0991SDimitry Andric   // Fix PHI nodes in Succ so they refer to NMBB instead of this.
11218bcb0991SDimitry Andric   Succ->replacePhiUsesWith(this, NMBB);
11220b57cec5SDimitry Andric 
11230b57cec5SDimitry Andric   // Inherit live-ins from the successor
11240b57cec5SDimitry Andric   for (const auto &LI : Succ->liveins())
11250b57cec5SDimitry Andric     NMBB->addLiveIn(LI);
11260b57cec5SDimitry Andric 
11270b57cec5SDimitry Andric   // Update LiveVariables.
11280b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
11290b57cec5SDimitry Andric   if (LV) {
11300b57cec5SDimitry Andric     // Restore kills of virtual registers that were killed by the terminators.
11310b57cec5SDimitry Andric     while (!KilledRegs.empty()) {
11325ffd83dbSDimitry Andric       Register Reg = KilledRegs.pop_back_val();
11330b57cec5SDimitry Andric       for (instr_iterator I = instr_end(), E = instr_begin(); I != E;) {
11340b57cec5SDimitry Andric         if (!(--I)->addRegisterKilled(Reg, TRI, /* AddIfNotFound= */ false))
11350b57cec5SDimitry Andric           continue;
11368bcb0991SDimitry Andric         if (Register::isVirtualRegister(Reg))
11370b57cec5SDimitry Andric           LV->getVarInfo(Reg).Kills.push_back(&*I);
11380b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Restored terminator kill: " << *I);
11390b57cec5SDimitry Andric         break;
11400b57cec5SDimitry Andric       }
11410b57cec5SDimitry Andric     }
11420b57cec5SDimitry Andric     // Update relevant live-through information.
11435ffd83dbSDimitry Andric     if (LiveInSets != nullptr)
11445ffd83dbSDimitry Andric       LV->addNewBlock(NMBB, this, Succ, *LiveInSets);
11455ffd83dbSDimitry Andric     else
11460b57cec5SDimitry Andric       LV->addNewBlock(NMBB, this, Succ);
11470b57cec5SDimitry Andric   }
11480b57cec5SDimitry Andric 
11490b57cec5SDimitry Andric   if (LIS) {
11500b57cec5SDimitry Andric     // After splitting the edge and updating SlotIndexes, live intervals may be
11510b57cec5SDimitry Andric     // in one of two situations, depending on whether this block was the last in
11520b57cec5SDimitry Andric     // the function. If the original block was the last in the function, all
11530b57cec5SDimitry Andric     // live intervals will end prior to the beginning of the new split block. If
11540b57cec5SDimitry Andric     // the original block was not at the end of the function, all live intervals
11550b57cec5SDimitry Andric     // will extend to the end of the new split block.
11560b57cec5SDimitry Andric 
11570b57cec5SDimitry Andric     bool isLastMBB =
11580b57cec5SDimitry Andric       std::next(MachineFunction::iterator(NMBB)) == getParent()->end();
11590b57cec5SDimitry Andric 
11600b57cec5SDimitry Andric     SlotIndex StartIndex = Indexes->getMBBEndIdx(this);
11610b57cec5SDimitry Andric     SlotIndex PrevIndex = StartIndex.getPrevSlot();
11620b57cec5SDimitry Andric     SlotIndex EndIndex = Indexes->getMBBEndIdx(NMBB);
11630b57cec5SDimitry Andric 
11640b57cec5SDimitry Andric     // Find the registers used from NMBB in PHIs in Succ.
11655ffd83dbSDimitry Andric     SmallSet<Register, 8> PHISrcRegs;
11660b57cec5SDimitry Andric     for (MachineBasicBlock::instr_iterator
11670b57cec5SDimitry Andric          I = Succ->instr_begin(), E = Succ->instr_end();
11680b57cec5SDimitry Andric          I != E && I->isPHI(); ++I) {
11690b57cec5SDimitry Andric       for (unsigned ni = 1, ne = I->getNumOperands(); ni != ne; ni += 2) {
11700b57cec5SDimitry Andric         if (I->getOperand(ni+1).getMBB() == NMBB) {
11710b57cec5SDimitry Andric           MachineOperand &MO = I->getOperand(ni);
11728bcb0991SDimitry Andric           Register Reg = MO.getReg();
11730b57cec5SDimitry Andric           PHISrcRegs.insert(Reg);
11740b57cec5SDimitry Andric           if (MO.isUndef())
11750b57cec5SDimitry Andric             continue;
11760b57cec5SDimitry Andric 
11770b57cec5SDimitry Andric           LiveInterval &LI = LIS->getInterval(Reg);
11780b57cec5SDimitry Andric           VNInfo *VNI = LI.getVNInfoAt(PrevIndex);
11790b57cec5SDimitry Andric           assert(VNI &&
11800b57cec5SDimitry Andric                  "PHI sources should be live out of their predecessors.");
11810b57cec5SDimitry Andric           LI.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI));
11820b57cec5SDimitry Andric         }
11830b57cec5SDimitry Andric       }
11840b57cec5SDimitry Andric     }
11850b57cec5SDimitry Andric 
11860b57cec5SDimitry Andric     MachineRegisterInfo *MRI = &getParent()->getRegInfo();
11870b57cec5SDimitry Andric     for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
11885ffd83dbSDimitry Andric       Register Reg = Register::index2VirtReg(i);
11890b57cec5SDimitry Andric       if (PHISrcRegs.count(Reg) || !LIS->hasInterval(Reg))
11900b57cec5SDimitry Andric         continue;
11910b57cec5SDimitry Andric 
11920b57cec5SDimitry Andric       LiveInterval &LI = LIS->getInterval(Reg);
11930b57cec5SDimitry Andric       if (!LI.liveAt(PrevIndex))
11940b57cec5SDimitry Andric         continue;
11950b57cec5SDimitry Andric 
11960b57cec5SDimitry Andric       bool isLiveOut = LI.liveAt(LIS->getMBBStartIdx(Succ));
11970b57cec5SDimitry Andric       if (isLiveOut && isLastMBB) {
11980b57cec5SDimitry Andric         VNInfo *VNI = LI.getVNInfoAt(PrevIndex);
11990b57cec5SDimitry Andric         assert(VNI && "LiveInterval should have VNInfo where it is live.");
12000b57cec5SDimitry Andric         LI.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI));
12010b57cec5SDimitry Andric       } else if (!isLiveOut && !isLastMBB) {
12020b57cec5SDimitry Andric         LI.removeSegment(StartIndex, EndIndex);
12030b57cec5SDimitry Andric       }
12040b57cec5SDimitry Andric     }
12050b57cec5SDimitry Andric 
12060b57cec5SDimitry Andric     // Update all intervals for registers whose uses may have been modified by
12070b57cec5SDimitry Andric     // updateTerminator().
12080b57cec5SDimitry Andric     LIS->repairIntervalsInRange(this, getFirstTerminator(), end(), UsedRegs);
12090b57cec5SDimitry Andric   }
12100b57cec5SDimitry Andric 
12110b57cec5SDimitry Andric   if (MachineDominatorTree *MDT =
12120b57cec5SDimitry Andric           P.getAnalysisIfAvailable<MachineDominatorTree>())
12130b57cec5SDimitry Andric     MDT->recordSplitCriticalEdge(this, Succ, NMBB);
12140b57cec5SDimitry Andric 
12150b57cec5SDimitry Andric   if (MachineLoopInfo *MLI = P.getAnalysisIfAvailable<MachineLoopInfo>())
12160b57cec5SDimitry Andric     if (MachineLoop *TIL = MLI->getLoopFor(this)) {
12170b57cec5SDimitry Andric       // If one or the other blocks were not in a loop, the new block is not
12180b57cec5SDimitry Andric       // either, and thus LI doesn't need to be updated.
12190b57cec5SDimitry Andric       if (MachineLoop *DestLoop = MLI->getLoopFor(Succ)) {
12200b57cec5SDimitry Andric         if (TIL == DestLoop) {
12210b57cec5SDimitry Andric           // Both in the same loop, the NMBB joins loop.
12220b57cec5SDimitry Andric           DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase());
12230b57cec5SDimitry Andric         } else if (TIL->contains(DestLoop)) {
12240b57cec5SDimitry Andric           // Edge from an outer loop to an inner loop.  Add to the outer loop.
12250b57cec5SDimitry Andric           TIL->addBasicBlockToLoop(NMBB, MLI->getBase());
12260b57cec5SDimitry Andric         } else if (DestLoop->contains(TIL)) {
12270b57cec5SDimitry Andric           // Edge from an inner loop to an outer loop.  Add to the outer loop.
12280b57cec5SDimitry Andric           DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase());
12290b57cec5SDimitry Andric         } else {
12300b57cec5SDimitry Andric           // Edge from two loops with no containment relation.  Because these
12310b57cec5SDimitry Andric           // are natural loops, we know that the destination block must be the
12320b57cec5SDimitry Andric           // header of its loop (adding a branch into a loop elsewhere would
12330b57cec5SDimitry Andric           // create an irreducible loop).
12340b57cec5SDimitry Andric           assert(DestLoop->getHeader() == Succ &&
12350b57cec5SDimitry Andric                  "Should not create irreducible loops!");
12360b57cec5SDimitry Andric           if (MachineLoop *P = DestLoop->getParentLoop())
12370b57cec5SDimitry Andric             P->addBasicBlockToLoop(NMBB, MLI->getBase());
12380b57cec5SDimitry Andric         }
12390b57cec5SDimitry Andric       }
12400b57cec5SDimitry Andric     }
12410b57cec5SDimitry Andric 
12420b57cec5SDimitry Andric   return NMBB;
12430b57cec5SDimitry Andric }
12440b57cec5SDimitry Andric 
canSplitCriticalEdge(const MachineBasicBlock * Succ) const12450b57cec5SDimitry Andric bool MachineBasicBlock::canSplitCriticalEdge(
12460b57cec5SDimitry Andric     const MachineBasicBlock *Succ) const {
12470b57cec5SDimitry Andric   // Splitting the critical edge to a landing pad block is non-trivial. Don't do
12480b57cec5SDimitry Andric   // it in this generic function.
12490b57cec5SDimitry Andric   if (Succ->isEHPad())
12500b57cec5SDimitry Andric     return false;
12510b57cec5SDimitry 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;
12560b57cec5SDimitry Andric 
12575ffd83dbSDimitry Andric   const MachineFunction *MF = getParent();
12580b57cec5SDimitry Andric   // Performance might be harmed on HW that implements branching using exec mask
12590b57cec5SDimitry Andric   // where both sides of the branches are always executed.
12600b57cec5SDimitry Andric   if (MF->getTarget().requiresStructuredCFG())
12610b57cec5SDimitry Andric     return false;
12620b57cec5SDimitry Andric 
12630b57cec5SDimitry 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.
12650b57cec5SDimitry Andric   const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
12660b57cec5SDimitry Andric   MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
12670b57cec5SDimitry Andric   SmallVector<MachineOperand, 4> Cond;
12680b57cec5SDimitry Andric   // AnalyzeBanch should modify this, since we did not allow modification.
12690b57cec5SDimitry Andric   if (TII->analyzeBranch(*const_cast<MachineBasicBlock *>(this), TBB, FBB, Cond,
12700b57cec5SDimitry Andric                          /*AllowModify*/ false))
12710b57cec5SDimitry Andric     return false;
12720b57cec5SDimitry Andric 
12730b57cec5SDimitry Andric   // Avoid bugpoint weirdness: A block may end with a conditional branch but
12740b57cec5SDimitry Andric   // jumps to the same MBB is either case. We have duplicate CFG edges in that
12750b57cec5SDimitry Andric   // case that we can't handle. Since this never happens in properly optimized
12760b57cec5SDimitry Andric   // code, just skip those edges.
12770b57cec5SDimitry Andric   if (TBB && TBB == FBB) {
12780b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Won't split critical edge after degenerate "
12790b57cec5SDimitry Andric                       << printMBBReference(*this) << '\n');
12800b57cec5SDimitry Andric     return false;
12810b57cec5SDimitry Andric   }
12820b57cec5SDimitry Andric   return true;
12830b57cec5SDimitry Andric }
12840b57cec5SDimitry Andric 
12850b57cec5SDimitry Andric /// Prepare MI to be removed from its bundle. This fixes bundle flags on MI's
12860b57cec5SDimitry Andric /// neighboring instructions so the bundle won't be broken by removing MI.
unbundleSingleMI(MachineInstr * MI)12870b57cec5SDimitry Andric static void unbundleSingleMI(MachineInstr *MI) {
12880b57cec5SDimitry Andric   // Removing the first instruction in a bundle.
12890b57cec5SDimitry Andric   if (MI->isBundledWithSucc() && !MI->isBundledWithPred())
12900b57cec5SDimitry Andric     MI->unbundleFromSucc();
12910b57cec5SDimitry Andric   // Removing the last instruction in a bundle.
12920b57cec5SDimitry Andric   if (MI->isBundledWithPred() && !MI->isBundledWithSucc())
12930b57cec5SDimitry Andric     MI->unbundleFromPred();
12940b57cec5SDimitry Andric   // If MI is not bundled, or if it is internal to a bundle, the neighbor flags
12950b57cec5SDimitry Andric   // are already fine.
12960b57cec5SDimitry Andric }
12970b57cec5SDimitry Andric 
12980b57cec5SDimitry Andric MachineBasicBlock::instr_iterator
erase(MachineBasicBlock::instr_iterator I)12990b57cec5SDimitry Andric MachineBasicBlock::erase(MachineBasicBlock::instr_iterator I) {
13000b57cec5SDimitry Andric   unbundleSingleMI(&*I);
13010b57cec5SDimitry Andric   return Insts.erase(I);
13020b57cec5SDimitry Andric }
13030b57cec5SDimitry Andric 
remove_instr(MachineInstr * MI)13040b57cec5SDimitry Andric MachineInstr *MachineBasicBlock::remove_instr(MachineInstr *MI) {
13050b57cec5SDimitry Andric   unbundleSingleMI(MI);
13060b57cec5SDimitry Andric   MI->clearFlag(MachineInstr::BundledPred);
13070b57cec5SDimitry Andric   MI->clearFlag(MachineInstr::BundledSucc);
13080b57cec5SDimitry Andric   return Insts.remove(MI);
13090b57cec5SDimitry Andric }
13100b57cec5SDimitry Andric 
13110b57cec5SDimitry Andric MachineBasicBlock::instr_iterator
insert(instr_iterator I,MachineInstr * MI)13120b57cec5SDimitry Andric MachineBasicBlock::insert(instr_iterator I, MachineInstr *MI) {
13130b57cec5SDimitry Andric   assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
13140b57cec5SDimitry Andric          "Cannot insert instruction with bundle flags");
13150b57cec5SDimitry Andric   // Set the bundle flags when inserting inside a bundle.
13160b57cec5SDimitry Andric   if (I != instr_end() && I->isBundledWithPred()) {
13170b57cec5SDimitry Andric     MI->setFlag(MachineInstr::BundledPred);
13180b57cec5SDimitry Andric     MI->setFlag(MachineInstr::BundledSucc);
13190b57cec5SDimitry Andric   }
13200b57cec5SDimitry Andric   return Insts.insert(I, MI);
13210b57cec5SDimitry Andric }
13220b57cec5SDimitry Andric 
13230b57cec5SDimitry Andric /// This method unlinks 'this' from the containing function, and returns it, but
13240b57cec5SDimitry Andric /// does not delete it.
removeFromParent()13250b57cec5SDimitry Andric MachineBasicBlock *MachineBasicBlock::removeFromParent() {
13260b57cec5SDimitry Andric   assert(getParent() && "Not embedded in a function!");
13270b57cec5SDimitry Andric   getParent()->remove(this);
13280b57cec5SDimitry Andric   return this;
13290b57cec5SDimitry Andric }
13300b57cec5SDimitry Andric 
13310b57cec5SDimitry Andric /// This method unlinks 'this' from the containing function, and deletes it.
eraseFromParent()13320b57cec5SDimitry Andric void MachineBasicBlock::eraseFromParent() {
13330b57cec5SDimitry Andric   assert(getParent() && "Not embedded in a function!");
13340b57cec5SDimitry Andric   getParent()->erase(this);
13350b57cec5SDimitry Andric }
13360b57cec5SDimitry Andric 
13370b57cec5SDimitry Andric /// Given a machine basic block that branched to 'Old', change the code and CFG
13380b57cec5SDimitry Andric /// so that it branches to 'New' instead.
ReplaceUsesOfBlockWith(MachineBasicBlock * Old,MachineBasicBlock * New)13390b57cec5SDimitry Andric void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old,
13400b57cec5SDimitry Andric                                                MachineBasicBlock *New) {
13410b57cec5SDimitry Andric   assert(Old != New && "Cannot replace self with self!");
13420b57cec5SDimitry Andric 
13430b57cec5SDimitry Andric   MachineBasicBlock::instr_iterator I = instr_end();
13440b57cec5SDimitry Andric   while (I != instr_begin()) {
13450b57cec5SDimitry Andric     --I;
13460b57cec5SDimitry Andric     if (!I->isTerminator()) break;
13470b57cec5SDimitry Andric 
13480b57cec5SDimitry Andric     // Scan the operands of this machine instruction, replacing any uses of Old
13490b57cec5SDimitry Andric     // with New.
13500b57cec5SDimitry Andric     for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
13510b57cec5SDimitry Andric       if (I->getOperand(i).isMBB() &&
13520b57cec5SDimitry Andric           I->getOperand(i).getMBB() == Old)
13530b57cec5SDimitry Andric         I->getOperand(i).setMBB(New);
13540b57cec5SDimitry Andric   }
13550b57cec5SDimitry Andric 
13560b57cec5SDimitry Andric   // Update the successor information.
13570b57cec5SDimitry Andric   replaceSuccessor(Old, New);
13580b57cec5SDimitry Andric }
13590b57cec5SDimitry Andric 
replacePhiUsesWith(MachineBasicBlock * Old,MachineBasicBlock * New)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 
13700b57cec5SDimitry Andric /// Find the next valid DebugLoc starting at MBBI, skipping any DBG_VALUE
13710b57cec5SDimitry Andric /// instructions.  Return UnknownLoc if there is none.
13720b57cec5SDimitry Andric DebugLoc
findDebugLoc(instr_iterator MBBI)13730b57cec5SDimitry Andric MachineBasicBlock::findDebugLoc(instr_iterator MBBI) {
13740b57cec5SDimitry Andric   // Skip debug declarations, we don't want a DebugLoc from them.
13750b57cec5SDimitry Andric   MBBI = skipDebugInstructionsForward(MBBI, instr_end());
13760b57cec5SDimitry Andric   if (MBBI != instr_end())
13770b57cec5SDimitry Andric     return MBBI->getDebugLoc();
13780b57cec5SDimitry Andric   return {};
13790b57cec5SDimitry Andric }
13800b57cec5SDimitry Andric 
rfindDebugLoc(reverse_instr_iterator MBBI)1381*5f7ddb14SDimitry Andric DebugLoc MachineBasicBlock::rfindDebugLoc(reverse_instr_iterator MBBI) {
1382*5f7ddb14SDimitry Andric   // Skip debug declarations, we don't want a DebugLoc from them.
1383*5f7ddb14SDimitry Andric   MBBI = skipDebugInstructionsBackward(MBBI, instr_rbegin());
1384*5f7ddb14SDimitry Andric   if (!MBBI->isDebugInstr())
1385*5f7ddb14SDimitry Andric     return MBBI->getDebugLoc();
1386*5f7ddb14SDimitry Andric   return {};
1387*5f7ddb14SDimitry Andric }
1388*5f7ddb14SDimitry Andric 
13890b57cec5SDimitry Andric /// Find the previous valid DebugLoc preceding MBBI, skipping and DBG_VALUE
13900b57cec5SDimitry Andric /// instructions.  Return UnknownLoc if there is none.
findPrevDebugLoc(instr_iterator MBBI)13910b57cec5SDimitry Andric DebugLoc MachineBasicBlock::findPrevDebugLoc(instr_iterator MBBI) {
13920b57cec5SDimitry 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());
13950b57cec5SDimitry Andric   if (!MBBI->isDebugInstr()) return MBBI->getDebugLoc();
13960b57cec5SDimitry Andric   return {};
13970b57cec5SDimitry Andric }
13980b57cec5SDimitry Andric 
rfindPrevDebugLoc(reverse_instr_iterator MBBI)1399*5f7ddb14SDimitry Andric DebugLoc MachineBasicBlock::rfindPrevDebugLoc(reverse_instr_iterator MBBI) {
1400*5f7ddb14SDimitry Andric   if (MBBI == instr_rend())
1401*5f7ddb14SDimitry Andric     return {};
1402*5f7ddb14SDimitry Andric   // Skip debug declarations, we don't want a DebugLoc from them.
1403*5f7ddb14SDimitry Andric   MBBI = next_nodbg(MBBI, instr_rend());
1404*5f7ddb14SDimitry Andric   if (MBBI != instr_rend())
1405*5f7ddb14SDimitry Andric     return MBBI->getDebugLoc();
1406*5f7ddb14SDimitry Andric   return {};
1407*5f7ddb14SDimitry Andric }
1408*5f7ddb14SDimitry Andric 
14090b57cec5SDimitry Andric /// Find and return the merged DebugLoc of the branch instructions of the block.
14100b57cec5SDimitry Andric /// Return UnknownLoc if there is none.
14110b57cec5SDimitry Andric DebugLoc
findBranchDebugLoc()14120b57cec5SDimitry Andric MachineBasicBlock::findBranchDebugLoc() {
14130b57cec5SDimitry Andric   DebugLoc DL;
14140b57cec5SDimitry Andric   auto TI = getFirstTerminator();
14150b57cec5SDimitry Andric   while (TI != end() && !TI->isBranch())
14160b57cec5SDimitry Andric     ++TI;
14170b57cec5SDimitry Andric 
14180b57cec5SDimitry Andric   if (TI != end()) {
14190b57cec5SDimitry Andric     DL = TI->getDebugLoc();
14200b57cec5SDimitry Andric     for (++TI ; TI != end() ; ++TI)
14210b57cec5SDimitry Andric       if (TI->isBranch())
14220b57cec5SDimitry Andric         DL = DILocation::getMergedLocation(DL, TI->getDebugLoc());
14230b57cec5SDimitry Andric   }
14240b57cec5SDimitry Andric   return DL;
14250b57cec5SDimitry Andric }
14260b57cec5SDimitry Andric 
14270b57cec5SDimitry Andric /// Return probability of the edge from this block to MBB.
14280b57cec5SDimitry Andric BranchProbability
getSuccProbability(const_succ_iterator Succ) const14290b57cec5SDimitry Andric MachineBasicBlock::getSuccProbability(const_succ_iterator Succ) const {
14300b57cec5SDimitry Andric   if (Probs.empty())
14310b57cec5SDimitry Andric     return BranchProbability(1, succ_size());
14320b57cec5SDimitry Andric 
14330b57cec5SDimitry Andric   const auto &Prob = *getProbabilityIterator(Succ);
14340b57cec5SDimitry Andric   if (Prob.isUnknown()) {
14350b57cec5SDimitry Andric     // For unknown probabilities, collect the sum of all known ones, and evenly
14360b57cec5SDimitry Andric     // ditribute the complemental of the sum to each unknown probability.
14370b57cec5SDimitry Andric     unsigned KnownProbNum = 0;
14380b57cec5SDimitry Andric     auto Sum = BranchProbability::getZero();
14390b57cec5SDimitry Andric     for (auto &P : Probs) {
14400b57cec5SDimitry Andric       if (!P.isUnknown()) {
14410b57cec5SDimitry Andric         Sum += P;
14420b57cec5SDimitry Andric         KnownProbNum++;
14430b57cec5SDimitry Andric       }
14440b57cec5SDimitry Andric     }
14450b57cec5SDimitry Andric     return Sum.getCompl() / (Probs.size() - KnownProbNum);
14460b57cec5SDimitry Andric   } else
14470b57cec5SDimitry Andric     return Prob;
14480b57cec5SDimitry Andric }
14490b57cec5SDimitry Andric 
14500b57cec5SDimitry Andric /// Set successor probability of a given iterator.
setSuccProbability(succ_iterator I,BranchProbability Prob)14510b57cec5SDimitry Andric void MachineBasicBlock::setSuccProbability(succ_iterator I,
14520b57cec5SDimitry Andric                                            BranchProbability Prob) {
14530b57cec5SDimitry Andric   assert(!Prob.isUnknown());
14540b57cec5SDimitry Andric   if (Probs.empty())
14550b57cec5SDimitry Andric     return;
14560b57cec5SDimitry Andric   *getProbabilityIterator(I) = Prob;
14570b57cec5SDimitry Andric }
14580b57cec5SDimitry Andric 
14590b57cec5SDimitry Andric /// Return probability iterator corresonding to the I successor iterator
14600b57cec5SDimitry Andric MachineBasicBlock::const_probability_iterator
getProbabilityIterator(MachineBasicBlock::const_succ_iterator I) const14610b57cec5SDimitry Andric MachineBasicBlock::getProbabilityIterator(
14620b57cec5SDimitry Andric     MachineBasicBlock::const_succ_iterator I) const {
14630b57cec5SDimitry Andric   assert(Probs.size() == Successors.size() && "Async probability list!");
14640b57cec5SDimitry Andric   const size_t index = std::distance(Successors.begin(), I);
14650b57cec5SDimitry Andric   assert(index < Probs.size() && "Not a current successor!");
14660b57cec5SDimitry Andric   return Probs.begin() + index;
14670b57cec5SDimitry Andric }
14680b57cec5SDimitry Andric 
14690b57cec5SDimitry Andric /// Return probability iterator corresonding to the I successor iterator.
14700b57cec5SDimitry Andric MachineBasicBlock::probability_iterator
getProbabilityIterator(MachineBasicBlock::succ_iterator I)14710b57cec5SDimitry Andric MachineBasicBlock::getProbabilityIterator(MachineBasicBlock::succ_iterator I) {
14720b57cec5SDimitry Andric   assert(Probs.size() == Successors.size() && "Async probability list!");
14730b57cec5SDimitry Andric   const size_t index = std::distance(Successors.begin(), I);
14740b57cec5SDimitry Andric   assert(index < Probs.size() && "Not a current successor!");
14750b57cec5SDimitry Andric   return Probs.begin() + index;
14760b57cec5SDimitry Andric }
14770b57cec5SDimitry Andric 
14780b57cec5SDimitry Andric /// Return whether (physical) register "Reg" has been <def>ined and not <kill>ed
14790b57cec5SDimitry Andric /// as of just before "MI".
14800b57cec5SDimitry Andric ///
14810b57cec5SDimitry Andric /// Search is localised to a neighborhood of
14820b57cec5SDimitry Andric /// Neighborhood instructions before (searching for defs or kills) and N
14830b57cec5SDimitry Andric /// instructions after (searching just for defs) MI.
14840b57cec5SDimitry Andric MachineBasicBlock::LivenessQueryResult
computeRegisterLiveness(const TargetRegisterInfo * TRI,MCRegister Reg,const_iterator Before,unsigned Neighborhood) const14850b57cec5SDimitry Andric MachineBasicBlock::computeRegisterLiveness(const TargetRegisterInfo *TRI,
14865ffd83dbSDimitry Andric                                            MCRegister Reg, const_iterator Before,
14870b57cec5SDimitry Andric                                            unsigned Neighborhood) const {
14880b57cec5SDimitry Andric   unsigned N = Neighborhood;
14890b57cec5SDimitry Andric 
14900b57cec5SDimitry Andric   // Try searching forwards from Before, looking for reads or defs.
14910b57cec5SDimitry Andric   const_iterator I(Before);
14920b57cec5SDimitry Andric   for (; I != end() && N > 0; ++I) {
1493*5f7ddb14SDimitry Andric     if (I->isDebugOrPseudoInstr())
14940b57cec5SDimitry Andric       continue;
14950b57cec5SDimitry Andric 
14960b57cec5SDimitry Andric     --N;
14970b57cec5SDimitry Andric 
1498480093f4SDimitry Andric     PhysRegInfo Info = AnalyzePhysRegInBundle(*I, Reg, TRI);
14990b57cec5SDimitry Andric 
15000b57cec5SDimitry Andric     // Register is live when we read it here.
15010b57cec5SDimitry Andric     if (Info.Read)
15020b57cec5SDimitry Andric       return LQR_Live;
15030b57cec5SDimitry Andric     // Register is dead if we can fully overwrite or clobber it here.
15040b57cec5SDimitry Andric     if (Info.FullyDefined || Info.Clobbered)
15050b57cec5SDimitry Andric       return LQR_Dead;
15060b57cec5SDimitry Andric   }
15070b57cec5SDimitry Andric 
15080b57cec5SDimitry Andric   // If we reached the end, it is safe to clobber Reg at the end of a block of
15090b57cec5SDimitry Andric   // no successor has it live in.
15100b57cec5SDimitry Andric   if (I == end()) {
15110b57cec5SDimitry Andric     for (MachineBasicBlock *S : successors()) {
15120b57cec5SDimitry Andric       for (const MachineBasicBlock::RegisterMaskPair &LI : S->liveins()) {
15130b57cec5SDimitry Andric         if (TRI->regsOverlap(LI.PhysReg, Reg))
15140b57cec5SDimitry Andric           return LQR_Live;
15150b57cec5SDimitry Andric       }
15160b57cec5SDimitry Andric     }
15170b57cec5SDimitry Andric 
15180b57cec5SDimitry Andric     return LQR_Dead;
15190b57cec5SDimitry Andric   }
15200b57cec5SDimitry Andric 
15210b57cec5SDimitry Andric 
15220b57cec5SDimitry Andric   N = Neighborhood;
15230b57cec5SDimitry Andric 
15240b57cec5SDimitry Andric   // Start by searching backwards from Before, looking for kills, reads or defs.
15250b57cec5SDimitry Andric   I = const_iterator(Before);
15260b57cec5SDimitry Andric   // If this is the first insn in the block, don't search backwards.
15270b57cec5SDimitry Andric   if (I != begin()) {
15280b57cec5SDimitry Andric     do {
15290b57cec5SDimitry Andric       --I;
15300b57cec5SDimitry Andric 
1531*5f7ddb14SDimitry Andric       if (I->isDebugOrPseudoInstr())
15320b57cec5SDimitry Andric         continue;
15330b57cec5SDimitry Andric 
15340b57cec5SDimitry Andric       --N;
15350b57cec5SDimitry Andric 
1536480093f4SDimitry Andric       PhysRegInfo Info = AnalyzePhysRegInBundle(*I, Reg, TRI);
15370b57cec5SDimitry Andric 
15380b57cec5SDimitry Andric       // Defs happen after uses so they take precedence if both are present.
15390b57cec5SDimitry Andric 
15400b57cec5SDimitry Andric       // Register is dead after a dead def of the full register.
15410b57cec5SDimitry Andric       if (Info.DeadDef)
15420b57cec5SDimitry Andric         return LQR_Dead;
15430b57cec5SDimitry Andric       // Register is (at least partially) live after a def.
15440b57cec5SDimitry Andric       if (Info.Defined) {
15450b57cec5SDimitry Andric         if (!Info.PartialDeadDef)
15460b57cec5SDimitry Andric           return LQR_Live;
15470b57cec5SDimitry Andric         // As soon as we saw a partial definition (dead or not),
15480b57cec5SDimitry Andric         // we cannot tell if the value is partial live without
15490b57cec5SDimitry Andric         // tracking the lanemasks. We are not going to do this,
15500b57cec5SDimitry Andric         // so fall back on the remaining of the analysis.
15510b57cec5SDimitry Andric         break;
15520b57cec5SDimitry Andric       }
15530b57cec5SDimitry Andric       // Register is dead after a full kill or clobber and no def.
15540b57cec5SDimitry Andric       if (Info.Killed || Info.Clobbered)
15550b57cec5SDimitry Andric         return LQR_Dead;
15560b57cec5SDimitry Andric       // Register must be live if we read it.
15570b57cec5SDimitry Andric       if (Info.Read)
15580b57cec5SDimitry Andric         return LQR_Live;
15590b57cec5SDimitry Andric 
15600b57cec5SDimitry Andric     } while (I != begin() && N > 0);
15610b57cec5SDimitry Andric   }
15620b57cec5SDimitry Andric 
1563480093f4SDimitry Andric   // If all the instructions before this in the block are debug instructions,
1564480093f4SDimitry Andric   // skip over them.
1565*5f7ddb14SDimitry Andric   while (I != begin() && std::prev(I)->isDebugOrPseudoInstr())
1566480093f4SDimitry Andric     --I;
1567480093f4SDimitry Andric 
15680b57cec5SDimitry Andric   // Did we get to the start of the block?
15690b57cec5SDimitry Andric   if (I == begin()) {
15700b57cec5SDimitry Andric     // If so, the register's state is definitely defined by the live-in state.
15710b57cec5SDimitry Andric     for (const MachineBasicBlock::RegisterMaskPair &LI : liveins())
15720b57cec5SDimitry Andric       if (TRI->regsOverlap(LI.PhysReg, Reg))
15730b57cec5SDimitry Andric         return LQR_Live;
15740b57cec5SDimitry Andric 
15750b57cec5SDimitry Andric     return LQR_Dead;
15760b57cec5SDimitry Andric   }
15770b57cec5SDimitry Andric 
15780b57cec5SDimitry Andric   // At this point we have no idea of the liveness of the register.
15790b57cec5SDimitry Andric   return LQR_Unknown;
15800b57cec5SDimitry Andric }
15810b57cec5SDimitry Andric 
15820b57cec5SDimitry Andric const uint32_t *
getBeginClobberMask(const TargetRegisterInfo * TRI) const15830b57cec5SDimitry Andric MachineBasicBlock::getBeginClobberMask(const TargetRegisterInfo *TRI) const {
15840b57cec5SDimitry Andric   // EH funclet entry does not preserve any registers.
15850b57cec5SDimitry Andric   return isEHFuncletEntry() ? TRI->getNoPreservedMask() : nullptr;
15860b57cec5SDimitry Andric }
15870b57cec5SDimitry Andric 
15880b57cec5SDimitry Andric const uint32_t *
getEndClobberMask(const TargetRegisterInfo * TRI) const15890b57cec5SDimitry Andric MachineBasicBlock::getEndClobberMask(const TargetRegisterInfo *TRI) const {
15900b57cec5SDimitry Andric   // If we see a return block with successors, this must be a funclet return,
15910b57cec5SDimitry Andric   // which does not preserve any registers. If there are no successors, we don't
15920b57cec5SDimitry Andric   // care what kind of return it is, putting a mask after it is a no-op.
15930b57cec5SDimitry Andric   return isReturnBlock() && !succ_empty() ? TRI->getNoPreservedMask() : nullptr;
15940b57cec5SDimitry Andric }
15950b57cec5SDimitry Andric 
clearLiveIns()15960b57cec5SDimitry Andric void MachineBasicBlock::clearLiveIns() {
15970b57cec5SDimitry Andric   LiveIns.clear();
15980b57cec5SDimitry Andric }
15990b57cec5SDimitry Andric 
livein_begin() const16000b57cec5SDimitry Andric MachineBasicBlock::livein_iterator MachineBasicBlock::livein_begin() const {
16010b57cec5SDimitry Andric   assert(getParent()->getProperties().hasProperty(
16020b57cec5SDimitry Andric       MachineFunctionProperties::Property::TracksLiveness) &&
16030b57cec5SDimitry Andric       "Liveness information is accurate");
16040b57cec5SDimitry Andric   return LiveIns.begin();
16050b57cec5SDimitry Andric }
16065ffd83dbSDimitry Andric 
liveout_begin() const1607*5f7ddb14SDimitry Andric MachineBasicBlock::liveout_iterator MachineBasicBlock::liveout_begin() const {
1608*5f7ddb14SDimitry Andric   const MachineFunction &MF = *getParent();
1609*5f7ddb14SDimitry Andric   assert(MF.getProperties().hasProperty(
1610*5f7ddb14SDimitry Andric       MachineFunctionProperties::Property::TracksLiveness) &&
1611*5f7ddb14SDimitry Andric       "Liveness information is accurate");
1612*5f7ddb14SDimitry Andric 
1613*5f7ddb14SDimitry Andric   const TargetLowering &TLI = *MF.getSubtarget().getTargetLowering();
1614*5f7ddb14SDimitry Andric   MCPhysReg ExceptionPointer = 0, ExceptionSelector = 0;
1615*5f7ddb14SDimitry Andric   if (MF.getFunction().hasPersonalityFn()) {
1616*5f7ddb14SDimitry Andric     auto PersonalityFn = MF.getFunction().getPersonalityFn();
1617*5f7ddb14SDimitry Andric     ExceptionPointer = TLI.getExceptionPointerRegister(PersonalityFn);
1618*5f7ddb14SDimitry Andric     ExceptionSelector = TLI.getExceptionSelectorRegister(PersonalityFn);
1619*5f7ddb14SDimitry Andric   }
1620*5f7ddb14SDimitry Andric 
1621*5f7ddb14SDimitry Andric   return liveout_iterator(*this, ExceptionPointer, ExceptionSelector, false);
1622*5f7ddb14SDimitry Andric }
1623*5f7ddb14SDimitry Andric 
16245ffd83dbSDimitry Andric const MBBSectionID MBBSectionID::ColdSectionID(MBBSectionID::SectionType::Cold);
16255ffd83dbSDimitry Andric const MBBSectionID
16265ffd83dbSDimitry Andric     MBBSectionID::ExceptionSectionID(MBBSectionID::SectionType::Exception);
1627