1*0b57cec5SDimitry Andric //===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // Collect the sequence of machine instructions for a basic block. 10*0b57cec5SDimitry Andric // 11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12*0b57cec5SDimitry Andric 13*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 14*0b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 15*0b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h" 16*0b57cec5SDimitry Andric #include "llvm/CodeGen/LiveVariables.h" 17*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h" 18*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 19*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 20*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h" 21*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 22*0b57cec5SDimitry Andric #include "llvm/CodeGen/SlotIndexes.h" 23*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 24*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 25*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 26*0b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h" 27*0b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 28*0b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 29*0b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 30*0b57cec5SDimitry Andric #include "llvm/IR/ModuleSlotTracker.h" 31*0b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h" 32*0b57cec5SDimitry Andric #include "llvm/MC/MCContext.h" 33*0b57cec5SDimitry Andric #include "llvm/Support/DataTypes.h" 34*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 35*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 36*0b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 37*0b57cec5SDimitry Andric #include <algorithm> 38*0b57cec5SDimitry Andric using namespace llvm; 39*0b57cec5SDimitry Andric 40*0b57cec5SDimitry Andric #define DEBUG_TYPE "codegen" 41*0b57cec5SDimitry Andric 42*0b57cec5SDimitry Andric MachineBasicBlock::MachineBasicBlock(MachineFunction &MF, const BasicBlock *B) 43*0b57cec5SDimitry Andric : BB(B), Number(-1), xParent(&MF) { 44*0b57cec5SDimitry Andric Insts.Parent = this; 45*0b57cec5SDimitry Andric if (B) 46*0b57cec5SDimitry Andric IrrLoopHeaderWeight = B->getIrrLoopHeaderWeight(); 47*0b57cec5SDimitry Andric } 48*0b57cec5SDimitry Andric 49*0b57cec5SDimitry Andric MachineBasicBlock::~MachineBasicBlock() { 50*0b57cec5SDimitry Andric } 51*0b57cec5SDimitry Andric 52*0b57cec5SDimitry Andric /// Return the MCSymbol for this basic block. 53*0b57cec5SDimitry Andric MCSymbol *MachineBasicBlock::getSymbol() const { 54*0b57cec5SDimitry Andric if (!CachedMCSymbol) { 55*0b57cec5SDimitry Andric const MachineFunction *MF = getParent(); 56*0b57cec5SDimitry Andric MCContext &Ctx = MF->getContext(); 57*0b57cec5SDimitry Andric auto Prefix = Ctx.getAsmInfo()->getPrivateLabelPrefix(); 58*0b57cec5SDimitry Andric assert(getNumber() >= 0 && "cannot get label for unreachable MBB"); 59*0b57cec5SDimitry Andric CachedMCSymbol = Ctx.getOrCreateSymbol(Twine(Prefix) + "BB" + 60*0b57cec5SDimitry Andric Twine(MF->getFunctionNumber()) + 61*0b57cec5SDimitry Andric "_" + Twine(getNumber())); 62*0b57cec5SDimitry Andric } 63*0b57cec5SDimitry Andric 64*0b57cec5SDimitry Andric return CachedMCSymbol; 65*0b57cec5SDimitry Andric } 66*0b57cec5SDimitry Andric 67*0b57cec5SDimitry Andric 68*0b57cec5SDimitry Andric raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) { 69*0b57cec5SDimitry Andric MBB.print(OS); 70*0b57cec5SDimitry Andric return OS; 71*0b57cec5SDimitry Andric } 72*0b57cec5SDimitry Andric 73*0b57cec5SDimitry Andric Printable llvm::printMBBReference(const MachineBasicBlock &MBB) { 74*0b57cec5SDimitry Andric return Printable([&MBB](raw_ostream &OS) { return MBB.printAsOperand(OS); }); 75*0b57cec5SDimitry Andric } 76*0b57cec5SDimitry Andric 77*0b57cec5SDimitry Andric /// When an MBB is added to an MF, we need to update the parent pointer of the 78*0b57cec5SDimitry Andric /// MBB, the MBB numbering, and any instructions in the MBB to be on the right 79*0b57cec5SDimitry Andric /// operand list for registers. 80*0b57cec5SDimitry Andric /// 81*0b57cec5SDimitry Andric /// MBBs start out as #-1. When a MBB is added to a MachineFunction, it 82*0b57cec5SDimitry Andric /// gets the next available unique MBB number. If it is removed from a 83*0b57cec5SDimitry Andric /// MachineFunction, it goes back to being #-1. 84*0b57cec5SDimitry Andric void ilist_callback_traits<MachineBasicBlock>::addNodeToList( 85*0b57cec5SDimitry Andric MachineBasicBlock *N) { 86*0b57cec5SDimitry Andric MachineFunction &MF = *N->getParent(); 87*0b57cec5SDimitry Andric N->Number = MF.addToMBBNumbering(N); 88*0b57cec5SDimitry Andric 89*0b57cec5SDimitry Andric // Make sure the instructions have their operands in the reginfo lists. 90*0b57cec5SDimitry Andric MachineRegisterInfo &RegInfo = MF.getRegInfo(); 91*0b57cec5SDimitry Andric for (MachineBasicBlock::instr_iterator 92*0b57cec5SDimitry Andric I = N->instr_begin(), E = N->instr_end(); I != E; ++I) 93*0b57cec5SDimitry Andric I->AddRegOperandsToUseLists(RegInfo); 94*0b57cec5SDimitry Andric } 95*0b57cec5SDimitry Andric 96*0b57cec5SDimitry Andric void ilist_callback_traits<MachineBasicBlock>::removeNodeFromList( 97*0b57cec5SDimitry Andric MachineBasicBlock *N) { 98*0b57cec5SDimitry Andric N->getParent()->removeFromMBBNumbering(N->Number); 99*0b57cec5SDimitry Andric N->Number = -1; 100*0b57cec5SDimitry Andric } 101*0b57cec5SDimitry Andric 102*0b57cec5SDimitry Andric /// When we add an instruction to a basic block list, we update its parent 103*0b57cec5SDimitry Andric /// pointer and add its operands from reg use/def lists if appropriate. 104*0b57cec5SDimitry Andric void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) { 105*0b57cec5SDimitry Andric assert(!N->getParent() && "machine instruction already in a basic block"); 106*0b57cec5SDimitry Andric N->setParent(Parent); 107*0b57cec5SDimitry Andric 108*0b57cec5SDimitry Andric // Add the instruction's register operands to their corresponding 109*0b57cec5SDimitry Andric // use/def lists. 110*0b57cec5SDimitry Andric MachineFunction *MF = Parent->getParent(); 111*0b57cec5SDimitry Andric N->AddRegOperandsToUseLists(MF->getRegInfo()); 112*0b57cec5SDimitry Andric MF->handleInsertion(*N); 113*0b57cec5SDimitry Andric } 114*0b57cec5SDimitry Andric 115*0b57cec5SDimitry Andric /// When we remove an instruction from a basic block list, we update its parent 116*0b57cec5SDimitry Andric /// pointer and remove its operands from reg use/def lists if appropriate. 117*0b57cec5SDimitry Andric void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) { 118*0b57cec5SDimitry Andric assert(N->getParent() && "machine instruction not in a basic block"); 119*0b57cec5SDimitry Andric 120*0b57cec5SDimitry Andric // Remove from the use/def lists. 121*0b57cec5SDimitry Andric if (MachineFunction *MF = N->getMF()) { 122*0b57cec5SDimitry Andric MF->handleRemoval(*N); 123*0b57cec5SDimitry Andric N->RemoveRegOperandsFromUseLists(MF->getRegInfo()); 124*0b57cec5SDimitry Andric } 125*0b57cec5SDimitry Andric 126*0b57cec5SDimitry Andric N->setParent(nullptr); 127*0b57cec5SDimitry Andric } 128*0b57cec5SDimitry Andric 129*0b57cec5SDimitry Andric /// When moving a range of instructions from one MBB list to another, we need to 130*0b57cec5SDimitry Andric /// update the parent pointers and the use/def lists. 131*0b57cec5SDimitry Andric void ilist_traits<MachineInstr>::transferNodesFromList(ilist_traits &FromList, 132*0b57cec5SDimitry Andric instr_iterator First, 133*0b57cec5SDimitry Andric instr_iterator Last) { 134*0b57cec5SDimitry Andric assert(Parent->getParent() == FromList.Parent->getParent() && 135*0b57cec5SDimitry Andric "cannot transfer MachineInstrs between MachineFunctions"); 136*0b57cec5SDimitry Andric 137*0b57cec5SDimitry Andric // If it's within the same BB, there's nothing to do. 138*0b57cec5SDimitry Andric if (this == &FromList) 139*0b57cec5SDimitry Andric return; 140*0b57cec5SDimitry Andric 141*0b57cec5SDimitry Andric assert(Parent != FromList.Parent && "Two lists have the same parent?"); 142*0b57cec5SDimitry Andric 143*0b57cec5SDimitry Andric // If splicing between two blocks within the same function, just update the 144*0b57cec5SDimitry Andric // parent pointers. 145*0b57cec5SDimitry Andric for (; First != Last; ++First) 146*0b57cec5SDimitry Andric First->setParent(Parent); 147*0b57cec5SDimitry Andric } 148*0b57cec5SDimitry Andric 149*0b57cec5SDimitry Andric void ilist_traits<MachineInstr>::deleteNode(MachineInstr *MI) { 150*0b57cec5SDimitry Andric assert(!MI->getParent() && "MI is still in a block!"); 151*0b57cec5SDimitry Andric Parent->getParent()->DeleteMachineInstr(MI); 152*0b57cec5SDimitry Andric } 153*0b57cec5SDimitry Andric 154*0b57cec5SDimitry Andric MachineBasicBlock::iterator MachineBasicBlock::getFirstNonPHI() { 155*0b57cec5SDimitry Andric instr_iterator I = instr_begin(), E = instr_end(); 156*0b57cec5SDimitry Andric while (I != E && I->isPHI()) 157*0b57cec5SDimitry Andric ++I; 158*0b57cec5SDimitry Andric assert((I == E || !I->isInsideBundle()) && 159*0b57cec5SDimitry Andric "First non-phi MI cannot be inside a bundle!"); 160*0b57cec5SDimitry Andric return I; 161*0b57cec5SDimitry Andric } 162*0b57cec5SDimitry Andric 163*0b57cec5SDimitry Andric MachineBasicBlock::iterator 164*0b57cec5SDimitry Andric MachineBasicBlock::SkipPHIsAndLabels(MachineBasicBlock::iterator I) { 165*0b57cec5SDimitry Andric const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo(); 166*0b57cec5SDimitry Andric 167*0b57cec5SDimitry Andric iterator E = end(); 168*0b57cec5SDimitry Andric while (I != E && (I->isPHI() || I->isPosition() || 169*0b57cec5SDimitry Andric TII->isBasicBlockPrologue(*I))) 170*0b57cec5SDimitry Andric ++I; 171*0b57cec5SDimitry Andric // FIXME: This needs to change if we wish to bundle labels 172*0b57cec5SDimitry Andric // inside the bundle. 173*0b57cec5SDimitry Andric assert((I == E || !I->isInsideBundle()) && 174*0b57cec5SDimitry Andric "First non-phi / non-label instruction is inside a bundle!"); 175*0b57cec5SDimitry Andric return I; 176*0b57cec5SDimitry Andric } 177*0b57cec5SDimitry Andric 178*0b57cec5SDimitry Andric MachineBasicBlock::iterator 179*0b57cec5SDimitry Andric MachineBasicBlock::SkipPHIsLabelsAndDebug(MachineBasicBlock::iterator I) { 180*0b57cec5SDimitry Andric const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo(); 181*0b57cec5SDimitry Andric 182*0b57cec5SDimitry Andric iterator E = end(); 183*0b57cec5SDimitry Andric while (I != E && (I->isPHI() || I->isPosition() || I->isDebugInstr() || 184*0b57cec5SDimitry Andric TII->isBasicBlockPrologue(*I))) 185*0b57cec5SDimitry Andric ++I; 186*0b57cec5SDimitry Andric // FIXME: This needs to change if we wish to bundle labels / dbg_values 187*0b57cec5SDimitry Andric // inside the bundle. 188*0b57cec5SDimitry Andric assert((I == E || !I->isInsideBundle()) && 189*0b57cec5SDimitry Andric "First non-phi / non-label / non-debug " 190*0b57cec5SDimitry Andric "instruction is inside a bundle!"); 191*0b57cec5SDimitry Andric return I; 192*0b57cec5SDimitry Andric } 193*0b57cec5SDimitry Andric 194*0b57cec5SDimitry Andric MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { 195*0b57cec5SDimitry Andric iterator B = begin(), E = end(), I = E; 196*0b57cec5SDimitry Andric while (I != B && ((--I)->isTerminator() || I->isDebugInstr())) 197*0b57cec5SDimitry Andric ; /*noop */ 198*0b57cec5SDimitry Andric while (I != E && !I->isTerminator()) 199*0b57cec5SDimitry Andric ++I; 200*0b57cec5SDimitry Andric return I; 201*0b57cec5SDimitry Andric } 202*0b57cec5SDimitry Andric 203*0b57cec5SDimitry Andric MachineBasicBlock::instr_iterator MachineBasicBlock::getFirstInstrTerminator() { 204*0b57cec5SDimitry Andric instr_iterator B = instr_begin(), E = instr_end(), I = E; 205*0b57cec5SDimitry Andric while (I != B && ((--I)->isTerminator() || I->isDebugInstr())) 206*0b57cec5SDimitry Andric ; /*noop */ 207*0b57cec5SDimitry Andric while (I != E && !I->isTerminator()) 208*0b57cec5SDimitry Andric ++I; 209*0b57cec5SDimitry Andric return I; 210*0b57cec5SDimitry Andric } 211*0b57cec5SDimitry Andric 212*0b57cec5SDimitry Andric MachineBasicBlock::iterator MachineBasicBlock::getFirstNonDebugInstr() { 213*0b57cec5SDimitry Andric // Skip over begin-of-block dbg_value instructions. 214*0b57cec5SDimitry Andric return skipDebugInstructionsForward(begin(), end()); 215*0b57cec5SDimitry Andric } 216*0b57cec5SDimitry Andric 217*0b57cec5SDimitry Andric MachineBasicBlock::iterator MachineBasicBlock::getLastNonDebugInstr() { 218*0b57cec5SDimitry Andric // Skip over end-of-block dbg_value instructions. 219*0b57cec5SDimitry Andric instr_iterator B = instr_begin(), I = instr_end(); 220*0b57cec5SDimitry Andric while (I != B) { 221*0b57cec5SDimitry Andric --I; 222*0b57cec5SDimitry Andric // Return instruction that starts a bundle. 223*0b57cec5SDimitry Andric if (I->isDebugInstr() || I->isInsideBundle()) 224*0b57cec5SDimitry Andric continue; 225*0b57cec5SDimitry Andric return I; 226*0b57cec5SDimitry Andric } 227*0b57cec5SDimitry Andric // The block is all debug values. 228*0b57cec5SDimitry Andric return end(); 229*0b57cec5SDimitry Andric } 230*0b57cec5SDimitry Andric 231*0b57cec5SDimitry Andric bool MachineBasicBlock::hasEHPadSuccessor() const { 232*0b57cec5SDimitry Andric for (const_succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I) 233*0b57cec5SDimitry Andric if ((*I)->isEHPad()) 234*0b57cec5SDimitry Andric return true; 235*0b57cec5SDimitry Andric return false; 236*0b57cec5SDimitry Andric } 237*0b57cec5SDimitry Andric 238*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 239*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineBasicBlock::dump() const { 240*0b57cec5SDimitry Andric print(dbgs()); 241*0b57cec5SDimitry Andric } 242*0b57cec5SDimitry Andric #endif 243*0b57cec5SDimitry Andric 244*0b57cec5SDimitry Andric bool MachineBasicBlock::isLegalToHoistInto() const { 245*0b57cec5SDimitry Andric if (isReturnBlock() || hasEHPadSuccessor()) 246*0b57cec5SDimitry Andric return false; 247*0b57cec5SDimitry Andric return true; 248*0b57cec5SDimitry Andric } 249*0b57cec5SDimitry Andric 250*0b57cec5SDimitry Andric StringRef MachineBasicBlock::getName() const { 251*0b57cec5SDimitry Andric if (const BasicBlock *LBB = getBasicBlock()) 252*0b57cec5SDimitry Andric return LBB->getName(); 253*0b57cec5SDimitry Andric else 254*0b57cec5SDimitry Andric return StringRef("", 0); 255*0b57cec5SDimitry Andric } 256*0b57cec5SDimitry Andric 257*0b57cec5SDimitry Andric /// Return a hopefully unique identifier for this block. 258*0b57cec5SDimitry Andric std::string MachineBasicBlock::getFullName() const { 259*0b57cec5SDimitry Andric std::string Name; 260*0b57cec5SDimitry Andric if (getParent()) 261*0b57cec5SDimitry Andric Name = (getParent()->getName() + ":").str(); 262*0b57cec5SDimitry Andric if (getBasicBlock()) 263*0b57cec5SDimitry Andric Name += getBasicBlock()->getName(); 264*0b57cec5SDimitry Andric else 265*0b57cec5SDimitry Andric Name += ("BB" + Twine(getNumber())).str(); 266*0b57cec5SDimitry Andric return Name; 267*0b57cec5SDimitry Andric } 268*0b57cec5SDimitry Andric 269*0b57cec5SDimitry Andric void MachineBasicBlock::print(raw_ostream &OS, const SlotIndexes *Indexes, 270*0b57cec5SDimitry Andric bool IsStandalone) const { 271*0b57cec5SDimitry Andric const MachineFunction *MF = getParent(); 272*0b57cec5SDimitry Andric if (!MF) { 273*0b57cec5SDimitry Andric OS << "Can't print out MachineBasicBlock because parent MachineFunction" 274*0b57cec5SDimitry Andric << " is null\n"; 275*0b57cec5SDimitry Andric return; 276*0b57cec5SDimitry Andric } 277*0b57cec5SDimitry Andric const Function &F = MF->getFunction(); 278*0b57cec5SDimitry Andric const Module *M = F.getParent(); 279*0b57cec5SDimitry Andric ModuleSlotTracker MST(M); 280*0b57cec5SDimitry Andric MST.incorporateFunction(F); 281*0b57cec5SDimitry Andric print(OS, MST, Indexes, IsStandalone); 282*0b57cec5SDimitry Andric } 283*0b57cec5SDimitry Andric 284*0b57cec5SDimitry Andric void MachineBasicBlock::print(raw_ostream &OS, ModuleSlotTracker &MST, 285*0b57cec5SDimitry Andric const SlotIndexes *Indexes, 286*0b57cec5SDimitry Andric bool IsStandalone) const { 287*0b57cec5SDimitry Andric const MachineFunction *MF = getParent(); 288*0b57cec5SDimitry Andric if (!MF) { 289*0b57cec5SDimitry Andric OS << "Can't print out MachineBasicBlock because parent MachineFunction" 290*0b57cec5SDimitry Andric << " is null\n"; 291*0b57cec5SDimitry Andric return; 292*0b57cec5SDimitry Andric } 293*0b57cec5SDimitry Andric 294*0b57cec5SDimitry Andric if (Indexes) 295*0b57cec5SDimitry Andric OS << Indexes->getMBBStartIdx(this) << '\t'; 296*0b57cec5SDimitry Andric 297*0b57cec5SDimitry Andric OS << "bb." << getNumber(); 298*0b57cec5SDimitry Andric bool HasAttributes = false; 299*0b57cec5SDimitry Andric if (const auto *BB = getBasicBlock()) { 300*0b57cec5SDimitry Andric if (BB->hasName()) { 301*0b57cec5SDimitry Andric OS << "." << BB->getName(); 302*0b57cec5SDimitry Andric } else { 303*0b57cec5SDimitry Andric HasAttributes = true; 304*0b57cec5SDimitry Andric OS << " ("; 305*0b57cec5SDimitry Andric int Slot = MST.getLocalSlot(BB); 306*0b57cec5SDimitry Andric if (Slot == -1) 307*0b57cec5SDimitry Andric OS << "<ir-block badref>"; 308*0b57cec5SDimitry Andric else 309*0b57cec5SDimitry Andric OS << (Twine("%ir-block.") + Twine(Slot)).str(); 310*0b57cec5SDimitry Andric } 311*0b57cec5SDimitry Andric } 312*0b57cec5SDimitry Andric 313*0b57cec5SDimitry Andric if (hasAddressTaken()) { 314*0b57cec5SDimitry Andric OS << (HasAttributes ? ", " : " ("); 315*0b57cec5SDimitry Andric OS << "address-taken"; 316*0b57cec5SDimitry Andric HasAttributes = true; 317*0b57cec5SDimitry Andric } 318*0b57cec5SDimitry Andric if (isEHPad()) { 319*0b57cec5SDimitry Andric OS << (HasAttributes ? ", " : " ("); 320*0b57cec5SDimitry Andric OS << "landing-pad"; 321*0b57cec5SDimitry Andric HasAttributes = true; 322*0b57cec5SDimitry Andric } 323*0b57cec5SDimitry Andric if (getAlignment()) { 324*0b57cec5SDimitry Andric OS << (HasAttributes ? ", " : " ("); 325*0b57cec5SDimitry Andric OS << "align " << getAlignment(); 326*0b57cec5SDimitry Andric HasAttributes = true; 327*0b57cec5SDimitry Andric } 328*0b57cec5SDimitry Andric if (HasAttributes) 329*0b57cec5SDimitry Andric OS << ")"; 330*0b57cec5SDimitry Andric OS << ":\n"; 331*0b57cec5SDimitry Andric 332*0b57cec5SDimitry Andric const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); 333*0b57cec5SDimitry Andric const MachineRegisterInfo &MRI = MF->getRegInfo(); 334*0b57cec5SDimitry Andric const TargetInstrInfo &TII = *getParent()->getSubtarget().getInstrInfo(); 335*0b57cec5SDimitry Andric bool HasLineAttributes = false; 336*0b57cec5SDimitry Andric 337*0b57cec5SDimitry Andric // Print the preds of this block according to the CFG. 338*0b57cec5SDimitry Andric if (!pred_empty() && IsStandalone) { 339*0b57cec5SDimitry Andric if (Indexes) OS << '\t'; 340*0b57cec5SDimitry Andric // Don't indent(2), align with previous line attributes. 341*0b57cec5SDimitry Andric OS << "; predecessors: "; 342*0b57cec5SDimitry Andric for (auto I = pred_begin(), E = pred_end(); I != E; ++I) { 343*0b57cec5SDimitry Andric if (I != pred_begin()) 344*0b57cec5SDimitry Andric OS << ", "; 345*0b57cec5SDimitry Andric OS << printMBBReference(**I); 346*0b57cec5SDimitry Andric } 347*0b57cec5SDimitry Andric OS << '\n'; 348*0b57cec5SDimitry Andric HasLineAttributes = true; 349*0b57cec5SDimitry Andric } 350*0b57cec5SDimitry Andric 351*0b57cec5SDimitry Andric if (!succ_empty()) { 352*0b57cec5SDimitry Andric if (Indexes) OS << '\t'; 353*0b57cec5SDimitry Andric // Print the successors 354*0b57cec5SDimitry Andric OS.indent(2) << "successors: "; 355*0b57cec5SDimitry Andric for (auto I = succ_begin(), E = succ_end(); I != E; ++I) { 356*0b57cec5SDimitry Andric if (I != succ_begin()) 357*0b57cec5SDimitry Andric OS << ", "; 358*0b57cec5SDimitry Andric OS << printMBBReference(**I); 359*0b57cec5SDimitry Andric if (!Probs.empty()) 360*0b57cec5SDimitry Andric OS << '(' 361*0b57cec5SDimitry Andric << format("0x%08" PRIx32, getSuccProbability(I).getNumerator()) 362*0b57cec5SDimitry Andric << ')'; 363*0b57cec5SDimitry Andric } 364*0b57cec5SDimitry Andric if (!Probs.empty() && IsStandalone) { 365*0b57cec5SDimitry Andric // Print human readable probabilities as comments. 366*0b57cec5SDimitry Andric OS << "; "; 367*0b57cec5SDimitry Andric for (auto I = succ_begin(), E = succ_end(); I != E; ++I) { 368*0b57cec5SDimitry Andric const BranchProbability &BP = getSuccProbability(I); 369*0b57cec5SDimitry Andric if (I != succ_begin()) 370*0b57cec5SDimitry Andric OS << ", "; 371*0b57cec5SDimitry Andric OS << printMBBReference(**I) << '(' 372*0b57cec5SDimitry Andric << format("%.2f%%", 373*0b57cec5SDimitry Andric rint(((double)BP.getNumerator() / BP.getDenominator()) * 374*0b57cec5SDimitry Andric 100.0 * 100.0) / 375*0b57cec5SDimitry Andric 100.0) 376*0b57cec5SDimitry Andric << ')'; 377*0b57cec5SDimitry Andric } 378*0b57cec5SDimitry Andric } 379*0b57cec5SDimitry Andric 380*0b57cec5SDimitry Andric OS << '\n'; 381*0b57cec5SDimitry Andric HasLineAttributes = true; 382*0b57cec5SDimitry Andric } 383*0b57cec5SDimitry Andric 384*0b57cec5SDimitry Andric if (!livein_empty() && MRI.tracksLiveness()) { 385*0b57cec5SDimitry Andric if (Indexes) OS << '\t'; 386*0b57cec5SDimitry Andric OS.indent(2) << "liveins: "; 387*0b57cec5SDimitry Andric 388*0b57cec5SDimitry Andric bool First = true; 389*0b57cec5SDimitry Andric for (const auto &LI : liveins()) { 390*0b57cec5SDimitry Andric if (!First) 391*0b57cec5SDimitry Andric OS << ", "; 392*0b57cec5SDimitry Andric First = false; 393*0b57cec5SDimitry Andric OS << printReg(LI.PhysReg, TRI); 394*0b57cec5SDimitry Andric if (!LI.LaneMask.all()) 395*0b57cec5SDimitry Andric OS << ":0x" << PrintLaneMask(LI.LaneMask); 396*0b57cec5SDimitry Andric } 397*0b57cec5SDimitry Andric HasLineAttributes = true; 398*0b57cec5SDimitry Andric } 399*0b57cec5SDimitry Andric 400*0b57cec5SDimitry Andric if (HasLineAttributes) 401*0b57cec5SDimitry Andric OS << '\n'; 402*0b57cec5SDimitry Andric 403*0b57cec5SDimitry Andric bool IsInBundle = false; 404*0b57cec5SDimitry Andric for (const MachineInstr &MI : instrs()) { 405*0b57cec5SDimitry Andric if (Indexes) { 406*0b57cec5SDimitry Andric if (Indexes->hasIndex(MI)) 407*0b57cec5SDimitry Andric OS << Indexes->getInstructionIndex(MI); 408*0b57cec5SDimitry Andric OS << '\t'; 409*0b57cec5SDimitry Andric } 410*0b57cec5SDimitry Andric 411*0b57cec5SDimitry Andric if (IsInBundle && !MI.isInsideBundle()) { 412*0b57cec5SDimitry Andric OS.indent(2) << "}\n"; 413*0b57cec5SDimitry Andric IsInBundle = false; 414*0b57cec5SDimitry Andric } 415*0b57cec5SDimitry Andric 416*0b57cec5SDimitry Andric OS.indent(IsInBundle ? 4 : 2); 417*0b57cec5SDimitry Andric MI.print(OS, MST, IsStandalone, /*SkipOpers=*/false, /*SkipDebugLoc=*/false, 418*0b57cec5SDimitry Andric /*AddNewLine=*/false, &TII); 419*0b57cec5SDimitry Andric 420*0b57cec5SDimitry Andric if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) { 421*0b57cec5SDimitry Andric OS << " {"; 422*0b57cec5SDimitry Andric IsInBundle = true; 423*0b57cec5SDimitry Andric } 424*0b57cec5SDimitry Andric OS << '\n'; 425*0b57cec5SDimitry Andric } 426*0b57cec5SDimitry Andric 427*0b57cec5SDimitry Andric if (IsInBundle) 428*0b57cec5SDimitry Andric OS.indent(2) << "}\n"; 429*0b57cec5SDimitry Andric 430*0b57cec5SDimitry Andric if (IrrLoopHeaderWeight && IsStandalone) { 431*0b57cec5SDimitry Andric if (Indexes) OS << '\t'; 432*0b57cec5SDimitry Andric OS.indent(2) << "; Irreducible loop header weight: " 433*0b57cec5SDimitry Andric << IrrLoopHeaderWeight.getValue() << '\n'; 434*0b57cec5SDimitry Andric } 435*0b57cec5SDimitry Andric } 436*0b57cec5SDimitry Andric 437*0b57cec5SDimitry Andric void MachineBasicBlock::printAsOperand(raw_ostream &OS, 438*0b57cec5SDimitry Andric bool /*PrintType*/) const { 439*0b57cec5SDimitry Andric OS << "%bb." << getNumber(); 440*0b57cec5SDimitry Andric } 441*0b57cec5SDimitry Andric 442*0b57cec5SDimitry Andric void MachineBasicBlock::removeLiveIn(MCPhysReg Reg, LaneBitmask LaneMask) { 443*0b57cec5SDimitry Andric LiveInVector::iterator I = find_if( 444*0b57cec5SDimitry Andric LiveIns, [Reg](const RegisterMaskPair &LI) { return LI.PhysReg == Reg; }); 445*0b57cec5SDimitry Andric if (I == LiveIns.end()) 446*0b57cec5SDimitry Andric return; 447*0b57cec5SDimitry Andric 448*0b57cec5SDimitry Andric I->LaneMask &= ~LaneMask; 449*0b57cec5SDimitry Andric if (I->LaneMask.none()) 450*0b57cec5SDimitry Andric LiveIns.erase(I); 451*0b57cec5SDimitry Andric } 452*0b57cec5SDimitry Andric 453*0b57cec5SDimitry Andric MachineBasicBlock::livein_iterator 454*0b57cec5SDimitry Andric MachineBasicBlock::removeLiveIn(MachineBasicBlock::livein_iterator I) { 455*0b57cec5SDimitry Andric // Get non-const version of iterator. 456*0b57cec5SDimitry Andric LiveInVector::iterator LI = LiveIns.begin() + (I - LiveIns.begin()); 457*0b57cec5SDimitry Andric return LiveIns.erase(LI); 458*0b57cec5SDimitry Andric } 459*0b57cec5SDimitry Andric 460*0b57cec5SDimitry Andric bool MachineBasicBlock::isLiveIn(MCPhysReg Reg, LaneBitmask LaneMask) const { 461*0b57cec5SDimitry Andric livein_iterator I = find_if( 462*0b57cec5SDimitry Andric LiveIns, [Reg](const RegisterMaskPair &LI) { return LI.PhysReg == Reg; }); 463*0b57cec5SDimitry Andric return I != livein_end() && (I->LaneMask & LaneMask).any(); 464*0b57cec5SDimitry Andric } 465*0b57cec5SDimitry Andric 466*0b57cec5SDimitry Andric void MachineBasicBlock::sortUniqueLiveIns() { 467*0b57cec5SDimitry Andric llvm::sort(LiveIns, 468*0b57cec5SDimitry Andric [](const RegisterMaskPair &LI0, const RegisterMaskPair &LI1) { 469*0b57cec5SDimitry Andric return LI0.PhysReg < LI1.PhysReg; 470*0b57cec5SDimitry Andric }); 471*0b57cec5SDimitry Andric // Liveins are sorted by physreg now we can merge their lanemasks. 472*0b57cec5SDimitry Andric LiveInVector::const_iterator I = LiveIns.begin(); 473*0b57cec5SDimitry Andric LiveInVector::const_iterator J; 474*0b57cec5SDimitry Andric LiveInVector::iterator Out = LiveIns.begin(); 475*0b57cec5SDimitry Andric for (; I != LiveIns.end(); ++Out, I = J) { 476*0b57cec5SDimitry Andric unsigned PhysReg = I->PhysReg; 477*0b57cec5SDimitry Andric LaneBitmask LaneMask = I->LaneMask; 478*0b57cec5SDimitry Andric for (J = std::next(I); J != LiveIns.end() && J->PhysReg == PhysReg; ++J) 479*0b57cec5SDimitry Andric LaneMask |= J->LaneMask; 480*0b57cec5SDimitry Andric Out->PhysReg = PhysReg; 481*0b57cec5SDimitry Andric Out->LaneMask = LaneMask; 482*0b57cec5SDimitry Andric } 483*0b57cec5SDimitry Andric LiveIns.erase(Out, LiveIns.end()); 484*0b57cec5SDimitry Andric } 485*0b57cec5SDimitry Andric 486*0b57cec5SDimitry Andric unsigned 487*0b57cec5SDimitry Andric MachineBasicBlock::addLiveIn(MCPhysReg PhysReg, const TargetRegisterClass *RC) { 488*0b57cec5SDimitry Andric assert(getParent() && "MBB must be inserted in function"); 489*0b57cec5SDimitry Andric assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) && "Expected physreg"); 490*0b57cec5SDimitry Andric assert(RC && "Register class is required"); 491*0b57cec5SDimitry Andric assert((isEHPad() || this == &getParent()->front()) && 492*0b57cec5SDimitry Andric "Only the entry block and landing pads can have physreg live ins"); 493*0b57cec5SDimitry Andric 494*0b57cec5SDimitry Andric bool LiveIn = isLiveIn(PhysReg); 495*0b57cec5SDimitry Andric iterator I = SkipPHIsAndLabels(begin()), E = end(); 496*0b57cec5SDimitry Andric MachineRegisterInfo &MRI = getParent()->getRegInfo(); 497*0b57cec5SDimitry Andric const TargetInstrInfo &TII = *getParent()->getSubtarget().getInstrInfo(); 498*0b57cec5SDimitry Andric 499*0b57cec5SDimitry Andric // Look for an existing copy. 500*0b57cec5SDimitry Andric if (LiveIn) 501*0b57cec5SDimitry Andric for (;I != E && I->isCopy(); ++I) 502*0b57cec5SDimitry Andric if (I->getOperand(1).getReg() == PhysReg) { 503*0b57cec5SDimitry Andric unsigned VirtReg = I->getOperand(0).getReg(); 504*0b57cec5SDimitry Andric if (!MRI.constrainRegClass(VirtReg, RC)) 505*0b57cec5SDimitry Andric llvm_unreachable("Incompatible live-in register class."); 506*0b57cec5SDimitry Andric return VirtReg; 507*0b57cec5SDimitry Andric } 508*0b57cec5SDimitry Andric 509*0b57cec5SDimitry Andric // No luck, create a virtual register. 510*0b57cec5SDimitry Andric unsigned VirtReg = MRI.createVirtualRegister(RC); 511*0b57cec5SDimitry Andric BuildMI(*this, I, DebugLoc(), TII.get(TargetOpcode::COPY), VirtReg) 512*0b57cec5SDimitry Andric .addReg(PhysReg, RegState::Kill); 513*0b57cec5SDimitry Andric if (!LiveIn) 514*0b57cec5SDimitry Andric addLiveIn(PhysReg); 515*0b57cec5SDimitry Andric return VirtReg; 516*0b57cec5SDimitry Andric } 517*0b57cec5SDimitry Andric 518*0b57cec5SDimitry Andric void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) { 519*0b57cec5SDimitry Andric getParent()->splice(NewAfter->getIterator(), getIterator()); 520*0b57cec5SDimitry Andric } 521*0b57cec5SDimitry Andric 522*0b57cec5SDimitry Andric void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) { 523*0b57cec5SDimitry Andric getParent()->splice(++NewBefore->getIterator(), getIterator()); 524*0b57cec5SDimitry Andric } 525*0b57cec5SDimitry Andric 526*0b57cec5SDimitry Andric void MachineBasicBlock::updateTerminator() { 527*0b57cec5SDimitry Andric const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo(); 528*0b57cec5SDimitry Andric // A block with no successors has no concerns with fall-through edges. 529*0b57cec5SDimitry Andric if (this->succ_empty()) 530*0b57cec5SDimitry Andric return; 531*0b57cec5SDimitry Andric 532*0b57cec5SDimitry Andric MachineBasicBlock *TBB = nullptr, *FBB = nullptr; 533*0b57cec5SDimitry Andric SmallVector<MachineOperand, 4> Cond; 534*0b57cec5SDimitry Andric DebugLoc DL = findBranchDebugLoc(); 535*0b57cec5SDimitry Andric bool B = TII->analyzeBranch(*this, TBB, FBB, Cond); 536*0b57cec5SDimitry Andric (void) B; 537*0b57cec5SDimitry Andric assert(!B && "UpdateTerminators requires analyzable predecessors!"); 538*0b57cec5SDimitry Andric if (Cond.empty()) { 539*0b57cec5SDimitry Andric if (TBB) { 540*0b57cec5SDimitry Andric // The block has an unconditional branch. If its successor is now its 541*0b57cec5SDimitry Andric // layout successor, delete the branch. 542*0b57cec5SDimitry Andric if (isLayoutSuccessor(TBB)) 543*0b57cec5SDimitry Andric TII->removeBranch(*this); 544*0b57cec5SDimitry Andric } else { 545*0b57cec5SDimitry Andric // The block has an unconditional fallthrough. If its successor is not its 546*0b57cec5SDimitry Andric // layout successor, insert a branch. First we have to locate the only 547*0b57cec5SDimitry Andric // non-landing-pad successor, as that is the fallthrough block. 548*0b57cec5SDimitry Andric for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) { 549*0b57cec5SDimitry Andric if ((*SI)->isEHPad()) 550*0b57cec5SDimitry Andric continue; 551*0b57cec5SDimitry Andric assert(!TBB && "Found more than one non-landing-pad successor!"); 552*0b57cec5SDimitry Andric TBB = *SI; 553*0b57cec5SDimitry Andric } 554*0b57cec5SDimitry Andric 555*0b57cec5SDimitry Andric // If there is no non-landing-pad successor, the block has no fall-through 556*0b57cec5SDimitry Andric // edges to be concerned with. 557*0b57cec5SDimitry Andric if (!TBB) 558*0b57cec5SDimitry Andric return; 559*0b57cec5SDimitry Andric 560*0b57cec5SDimitry Andric // Finally update the unconditional successor to be reached via a branch 561*0b57cec5SDimitry Andric // if it would not be reached by fallthrough. 562*0b57cec5SDimitry Andric if (!isLayoutSuccessor(TBB)) 563*0b57cec5SDimitry Andric TII->insertBranch(*this, TBB, nullptr, Cond, DL); 564*0b57cec5SDimitry Andric } 565*0b57cec5SDimitry Andric return; 566*0b57cec5SDimitry Andric } 567*0b57cec5SDimitry Andric 568*0b57cec5SDimitry Andric if (FBB) { 569*0b57cec5SDimitry Andric // The block has a non-fallthrough conditional branch. If one of its 570*0b57cec5SDimitry Andric // successors is its layout successor, rewrite it to a fallthrough 571*0b57cec5SDimitry Andric // conditional branch. 572*0b57cec5SDimitry Andric if (isLayoutSuccessor(TBB)) { 573*0b57cec5SDimitry Andric if (TII->reverseBranchCondition(Cond)) 574*0b57cec5SDimitry Andric return; 575*0b57cec5SDimitry Andric TII->removeBranch(*this); 576*0b57cec5SDimitry Andric TII->insertBranch(*this, FBB, nullptr, Cond, DL); 577*0b57cec5SDimitry Andric } else if (isLayoutSuccessor(FBB)) { 578*0b57cec5SDimitry Andric TII->removeBranch(*this); 579*0b57cec5SDimitry Andric TII->insertBranch(*this, TBB, nullptr, Cond, DL); 580*0b57cec5SDimitry Andric } 581*0b57cec5SDimitry Andric return; 582*0b57cec5SDimitry Andric } 583*0b57cec5SDimitry Andric 584*0b57cec5SDimitry Andric // Walk through the successors and find the successor which is not a landing 585*0b57cec5SDimitry Andric // pad and is not the conditional branch destination (in TBB) as the 586*0b57cec5SDimitry Andric // fallthrough successor. 587*0b57cec5SDimitry Andric MachineBasicBlock *FallthroughBB = nullptr; 588*0b57cec5SDimitry Andric for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) { 589*0b57cec5SDimitry Andric if ((*SI)->isEHPad() || *SI == TBB) 590*0b57cec5SDimitry Andric continue; 591*0b57cec5SDimitry Andric assert(!FallthroughBB && "Found more than one fallthrough successor."); 592*0b57cec5SDimitry Andric FallthroughBB = *SI; 593*0b57cec5SDimitry Andric } 594*0b57cec5SDimitry Andric 595*0b57cec5SDimitry Andric if (!FallthroughBB) { 596*0b57cec5SDimitry Andric if (canFallThrough()) { 597*0b57cec5SDimitry Andric // We fallthrough to the same basic block as the conditional jump targets. 598*0b57cec5SDimitry Andric // Remove the conditional jump, leaving unconditional fallthrough. 599*0b57cec5SDimitry Andric // FIXME: This does not seem like a reasonable pattern to support, but it 600*0b57cec5SDimitry Andric // has been seen in the wild coming out of degenerate ARM test cases. 601*0b57cec5SDimitry Andric TII->removeBranch(*this); 602*0b57cec5SDimitry Andric 603*0b57cec5SDimitry Andric // Finally update the unconditional successor to be reached via a branch if 604*0b57cec5SDimitry Andric // it would not be reached by fallthrough. 605*0b57cec5SDimitry Andric if (!isLayoutSuccessor(TBB)) 606*0b57cec5SDimitry Andric TII->insertBranch(*this, TBB, nullptr, Cond, DL); 607*0b57cec5SDimitry Andric return; 608*0b57cec5SDimitry Andric } 609*0b57cec5SDimitry Andric 610*0b57cec5SDimitry Andric // We enter here iff exactly one successor is TBB which cannot fallthrough 611*0b57cec5SDimitry Andric // and the rest successors if any are EHPads. In this case, we need to 612*0b57cec5SDimitry Andric // change the conditional branch into unconditional branch. 613*0b57cec5SDimitry Andric TII->removeBranch(*this); 614*0b57cec5SDimitry Andric Cond.clear(); 615*0b57cec5SDimitry Andric TII->insertBranch(*this, TBB, nullptr, Cond, DL); 616*0b57cec5SDimitry Andric return; 617*0b57cec5SDimitry Andric } 618*0b57cec5SDimitry Andric 619*0b57cec5SDimitry Andric // The block has a fallthrough conditional branch. 620*0b57cec5SDimitry Andric if (isLayoutSuccessor(TBB)) { 621*0b57cec5SDimitry Andric if (TII->reverseBranchCondition(Cond)) { 622*0b57cec5SDimitry Andric // We can't reverse the condition, add an unconditional branch. 623*0b57cec5SDimitry Andric Cond.clear(); 624*0b57cec5SDimitry Andric TII->insertBranch(*this, FallthroughBB, nullptr, Cond, DL); 625*0b57cec5SDimitry Andric return; 626*0b57cec5SDimitry Andric } 627*0b57cec5SDimitry Andric TII->removeBranch(*this); 628*0b57cec5SDimitry Andric TII->insertBranch(*this, FallthroughBB, nullptr, Cond, DL); 629*0b57cec5SDimitry Andric } else if (!isLayoutSuccessor(FallthroughBB)) { 630*0b57cec5SDimitry Andric TII->removeBranch(*this); 631*0b57cec5SDimitry Andric TII->insertBranch(*this, TBB, FallthroughBB, Cond, DL); 632*0b57cec5SDimitry Andric } 633*0b57cec5SDimitry Andric } 634*0b57cec5SDimitry Andric 635*0b57cec5SDimitry Andric void MachineBasicBlock::validateSuccProbs() const { 636*0b57cec5SDimitry Andric #ifndef NDEBUG 637*0b57cec5SDimitry Andric int64_t Sum = 0; 638*0b57cec5SDimitry Andric for (auto Prob : Probs) 639*0b57cec5SDimitry Andric Sum += Prob.getNumerator(); 640*0b57cec5SDimitry Andric // Due to precision issue, we assume that the sum of probabilities is one if 641*0b57cec5SDimitry Andric // the difference between the sum of their numerators and the denominator is 642*0b57cec5SDimitry Andric // no greater than the number of successors. 643*0b57cec5SDimitry Andric assert((uint64_t)std::abs(Sum - BranchProbability::getDenominator()) <= 644*0b57cec5SDimitry Andric Probs.size() && 645*0b57cec5SDimitry Andric "The sum of successors's probabilities exceeds one."); 646*0b57cec5SDimitry Andric #endif // NDEBUG 647*0b57cec5SDimitry Andric } 648*0b57cec5SDimitry Andric 649*0b57cec5SDimitry Andric void MachineBasicBlock::addSuccessor(MachineBasicBlock *Succ, 650*0b57cec5SDimitry Andric BranchProbability Prob) { 651*0b57cec5SDimitry Andric // Probability list is either empty (if successor list isn't empty, this means 652*0b57cec5SDimitry Andric // disabled optimization) or has the same size as successor list. 653*0b57cec5SDimitry Andric if (!(Probs.empty() && !Successors.empty())) 654*0b57cec5SDimitry Andric Probs.push_back(Prob); 655*0b57cec5SDimitry Andric Successors.push_back(Succ); 656*0b57cec5SDimitry Andric Succ->addPredecessor(this); 657*0b57cec5SDimitry Andric } 658*0b57cec5SDimitry Andric 659*0b57cec5SDimitry Andric void MachineBasicBlock::addSuccessorWithoutProb(MachineBasicBlock *Succ) { 660*0b57cec5SDimitry Andric // We need to make sure probability list is either empty or has the same size 661*0b57cec5SDimitry Andric // of successor list. When this function is called, we can safely delete all 662*0b57cec5SDimitry Andric // probability in the list. 663*0b57cec5SDimitry Andric Probs.clear(); 664*0b57cec5SDimitry Andric Successors.push_back(Succ); 665*0b57cec5SDimitry Andric Succ->addPredecessor(this); 666*0b57cec5SDimitry Andric } 667*0b57cec5SDimitry Andric 668*0b57cec5SDimitry Andric void MachineBasicBlock::splitSuccessor(MachineBasicBlock *Old, 669*0b57cec5SDimitry Andric MachineBasicBlock *New, 670*0b57cec5SDimitry Andric bool NormalizeSuccProbs) { 671*0b57cec5SDimitry Andric succ_iterator OldI = llvm::find(successors(), Old); 672*0b57cec5SDimitry Andric assert(OldI != succ_end() && "Old is not a successor of this block!"); 673*0b57cec5SDimitry Andric assert(llvm::find(successors(), New) == succ_end() && 674*0b57cec5SDimitry Andric "New is already a successor of this block!"); 675*0b57cec5SDimitry Andric 676*0b57cec5SDimitry Andric // Add a new successor with equal probability as the original one. Note 677*0b57cec5SDimitry Andric // that we directly copy the probability using the iterator rather than 678*0b57cec5SDimitry Andric // getting a potentially synthetic probability computed when unknown. This 679*0b57cec5SDimitry Andric // preserves the probabilities as-is and then we can renormalize them and 680*0b57cec5SDimitry Andric // query them effectively afterward. 681*0b57cec5SDimitry Andric addSuccessor(New, Probs.empty() ? BranchProbability::getUnknown() 682*0b57cec5SDimitry Andric : *getProbabilityIterator(OldI)); 683*0b57cec5SDimitry Andric if (NormalizeSuccProbs) 684*0b57cec5SDimitry Andric normalizeSuccProbs(); 685*0b57cec5SDimitry Andric } 686*0b57cec5SDimitry Andric 687*0b57cec5SDimitry Andric void MachineBasicBlock::removeSuccessor(MachineBasicBlock *Succ, 688*0b57cec5SDimitry Andric bool NormalizeSuccProbs) { 689*0b57cec5SDimitry Andric succ_iterator I = find(Successors, Succ); 690*0b57cec5SDimitry Andric removeSuccessor(I, NormalizeSuccProbs); 691*0b57cec5SDimitry Andric } 692*0b57cec5SDimitry Andric 693*0b57cec5SDimitry Andric MachineBasicBlock::succ_iterator 694*0b57cec5SDimitry Andric MachineBasicBlock::removeSuccessor(succ_iterator I, bool NormalizeSuccProbs) { 695*0b57cec5SDimitry Andric assert(I != Successors.end() && "Not a current successor!"); 696*0b57cec5SDimitry Andric 697*0b57cec5SDimitry Andric // If probability list is empty it means we don't use it (disabled 698*0b57cec5SDimitry Andric // optimization). 699*0b57cec5SDimitry Andric if (!Probs.empty()) { 700*0b57cec5SDimitry Andric probability_iterator WI = getProbabilityIterator(I); 701*0b57cec5SDimitry Andric Probs.erase(WI); 702*0b57cec5SDimitry Andric if (NormalizeSuccProbs) 703*0b57cec5SDimitry Andric normalizeSuccProbs(); 704*0b57cec5SDimitry Andric } 705*0b57cec5SDimitry Andric 706*0b57cec5SDimitry Andric (*I)->removePredecessor(this); 707*0b57cec5SDimitry Andric return Successors.erase(I); 708*0b57cec5SDimitry Andric } 709*0b57cec5SDimitry Andric 710*0b57cec5SDimitry Andric void MachineBasicBlock::replaceSuccessor(MachineBasicBlock *Old, 711*0b57cec5SDimitry Andric MachineBasicBlock *New) { 712*0b57cec5SDimitry Andric if (Old == New) 713*0b57cec5SDimitry Andric return; 714*0b57cec5SDimitry Andric 715*0b57cec5SDimitry Andric succ_iterator E = succ_end(); 716*0b57cec5SDimitry Andric succ_iterator NewI = E; 717*0b57cec5SDimitry Andric succ_iterator OldI = E; 718*0b57cec5SDimitry Andric for (succ_iterator I = succ_begin(); I != E; ++I) { 719*0b57cec5SDimitry Andric if (*I == Old) { 720*0b57cec5SDimitry Andric OldI = I; 721*0b57cec5SDimitry Andric if (NewI != E) 722*0b57cec5SDimitry Andric break; 723*0b57cec5SDimitry Andric } 724*0b57cec5SDimitry Andric if (*I == New) { 725*0b57cec5SDimitry Andric NewI = I; 726*0b57cec5SDimitry Andric if (OldI != E) 727*0b57cec5SDimitry Andric break; 728*0b57cec5SDimitry Andric } 729*0b57cec5SDimitry Andric } 730*0b57cec5SDimitry Andric assert(OldI != E && "Old is not a successor of this block"); 731*0b57cec5SDimitry Andric 732*0b57cec5SDimitry Andric // If New isn't already a successor, let it take Old's place. 733*0b57cec5SDimitry Andric if (NewI == E) { 734*0b57cec5SDimitry Andric Old->removePredecessor(this); 735*0b57cec5SDimitry Andric New->addPredecessor(this); 736*0b57cec5SDimitry Andric *OldI = New; 737*0b57cec5SDimitry Andric return; 738*0b57cec5SDimitry Andric } 739*0b57cec5SDimitry Andric 740*0b57cec5SDimitry Andric // New is already a successor. 741*0b57cec5SDimitry Andric // Update its probability instead of adding a duplicate edge. 742*0b57cec5SDimitry Andric if (!Probs.empty()) { 743*0b57cec5SDimitry Andric auto ProbIter = getProbabilityIterator(NewI); 744*0b57cec5SDimitry Andric if (!ProbIter->isUnknown()) 745*0b57cec5SDimitry Andric *ProbIter += *getProbabilityIterator(OldI); 746*0b57cec5SDimitry Andric } 747*0b57cec5SDimitry Andric removeSuccessor(OldI); 748*0b57cec5SDimitry Andric } 749*0b57cec5SDimitry Andric 750*0b57cec5SDimitry Andric void MachineBasicBlock::copySuccessor(MachineBasicBlock *Orig, 751*0b57cec5SDimitry Andric succ_iterator I) { 752*0b57cec5SDimitry Andric if (Orig->Probs.empty()) 753*0b57cec5SDimitry Andric addSuccessor(*I, Orig->getSuccProbability(I)); 754*0b57cec5SDimitry Andric else 755*0b57cec5SDimitry Andric addSuccessorWithoutProb(*I); 756*0b57cec5SDimitry Andric } 757*0b57cec5SDimitry Andric 758*0b57cec5SDimitry Andric void MachineBasicBlock::addPredecessor(MachineBasicBlock *Pred) { 759*0b57cec5SDimitry Andric Predecessors.push_back(Pred); 760*0b57cec5SDimitry Andric } 761*0b57cec5SDimitry Andric 762*0b57cec5SDimitry Andric void MachineBasicBlock::removePredecessor(MachineBasicBlock *Pred) { 763*0b57cec5SDimitry Andric pred_iterator I = find(Predecessors, Pred); 764*0b57cec5SDimitry Andric assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); 765*0b57cec5SDimitry Andric Predecessors.erase(I); 766*0b57cec5SDimitry Andric } 767*0b57cec5SDimitry Andric 768*0b57cec5SDimitry Andric void MachineBasicBlock::transferSuccessors(MachineBasicBlock *FromMBB) { 769*0b57cec5SDimitry Andric if (this == FromMBB) 770*0b57cec5SDimitry Andric return; 771*0b57cec5SDimitry Andric 772*0b57cec5SDimitry Andric while (!FromMBB->succ_empty()) { 773*0b57cec5SDimitry Andric MachineBasicBlock *Succ = *FromMBB->succ_begin(); 774*0b57cec5SDimitry Andric 775*0b57cec5SDimitry Andric // If probability list is empty it means we don't use it (disabled optimization). 776*0b57cec5SDimitry Andric if (!FromMBB->Probs.empty()) { 777*0b57cec5SDimitry Andric auto Prob = *FromMBB->Probs.begin(); 778*0b57cec5SDimitry Andric addSuccessor(Succ, Prob); 779*0b57cec5SDimitry Andric } else 780*0b57cec5SDimitry Andric addSuccessorWithoutProb(Succ); 781*0b57cec5SDimitry Andric 782*0b57cec5SDimitry Andric FromMBB->removeSuccessor(Succ); 783*0b57cec5SDimitry Andric } 784*0b57cec5SDimitry Andric } 785*0b57cec5SDimitry Andric 786*0b57cec5SDimitry Andric void 787*0b57cec5SDimitry Andric MachineBasicBlock::transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB) { 788*0b57cec5SDimitry Andric if (this == FromMBB) 789*0b57cec5SDimitry Andric return; 790*0b57cec5SDimitry Andric 791*0b57cec5SDimitry Andric while (!FromMBB->succ_empty()) { 792*0b57cec5SDimitry Andric MachineBasicBlock *Succ = *FromMBB->succ_begin(); 793*0b57cec5SDimitry Andric if (!FromMBB->Probs.empty()) { 794*0b57cec5SDimitry Andric auto Prob = *FromMBB->Probs.begin(); 795*0b57cec5SDimitry Andric addSuccessor(Succ, Prob); 796*0b57cec5SDimitry Andric } else 797*0b57cec5SDimitry Andric addSuccessorWithoutProb(Succ); 798*0b57cec5SDimitry Andric FromMBB->removeSuccessor(Succ); 799*0b57cec5SDimitry Andric 800*0b57cec5SDimitry Andric // Fix up any PHI nodes in the successor. 801*0b57cec5SDimitry Andric for (MachineBasicBlock::instr_iterator MI = Succ->instr_begin(), 802*0b57cec5SDimitry Andric ME = Succ->instr_end(); MI != ME && MI->isPHI(); ++MI) 803*0b57cec5SDimitry Andric for (unsigned i = 2, e = MI->getNumOperands()+1; i != e; i += 2) { 804*0b57cec5SDimitry Andric MachineOperand &MO = MI->getOperand(i); 805*0b57cec5SDimitry Andric if (MO.getMBB() == FromMBB) 806*0b57cec5SDimitry Andric MO.setMBB(this); 807*0b57cec5SDimitry Andric } 808*0b57cec5SDimitry Andric } 809*0b57cec5SDimitry Andric normalizeSuccProbs(); 810*0b57cec5SDimitry Andric } 811*0b57cec5SDimitry Andric 812*0b57cec5SDimitry Andric bool MachineBasicBlock::isPredecessor(const MachineBasicBlock *MBB) const { 813*0b57cec5SDimitry Andric return is_contained(predecessors(), MBB); 814*0b57cec5SDimitry Andric } 815*0b57cec5SDimitry Andric 816*0b57cec5SDimitry Andric bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const { 817*0b57cec5SDimitry Andric return is_contained(successors(), MBB); 818*0b57cec5SDimitry Andric } 819*0b57cec5SDimitry Andric 820*0b57cec5SDimitry Andric bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const { 821*0b57cec5SDimitry Andric MachineFunction::const_iterator I(this); 822*0b57cec5SDimitry Andric return std::next(I) == MachineFunction::const_iterator(MBB); 823*0b57cec5SDimitry Andric } 824*0b57cec5SDimitry Andric 825*0b57cec5SDimitry Andric MachineBasicBlock *MachineBasicBlock::getFallThrough() { 826*0b57cec5SDimitry Andric MachineFunction::iterator Fallthrough = getIterator(); 827*0b57cec5SDimitry Andric ++Fallthrough; 828*0b57cec5SDimitry Andric // If FallthroughBlock is off the end of the function, it can't fall through. 829*0b57cec5SDimitry Andric if (Fallthrough == getParent()->end()) 830*0b57cec5SDimitry Andric return nullptr; 831*0b57cec5SDimitry Andric 832*0b57cec5SDimitry Andric // If FallthroughBlock isn't a successor, no fallthrough is possible. 833*0b57cec5SDimitry Andric if (!isSuccessor(&*Fallthrough)) 834*0b57cec5SDimitry Andric return nullptr; 835*0b57cec5SDimitry Andric 836*0b57cec5SDimitry Andric // Analyze the branches, if any, at the end of the block. 837*0b57cec5SDimitry Andric MachineBasicBlock *TBB = nullptr, *FBB = nullptr; 838*0b57cec5SDimitry Andric SmallVector<MachineOperand, 4> Cond; 839*0b57cec5SDimitry Andric const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo(); 840*0b57cec5SDimitry Andric if (TII->analyzeBranch(*this, TBB, FBB, Cond)) { 841*0b57cec5SDimitry Andric // If we couldn't analyze the branch, examine the last instruction. 842*0b57cec5SDimitry Andric // If the block doesn't end in a known control barrier, assume fallthrough 843*0b57cec5SDimitry Andric // is possible. The isPredicated check is needed because this code can be 844*0b57cec5SDimitry Andric // called during IfConversion, where an instruction which is normally a 845*0b57cec5SDimitry Andric // Barrier is predicated and thus no longer an actual control barrier. 846*0b57cec5SDimitry Andric return (empty() || !back().isBarrier() || TII->isPredicated(back())) 847*0b57cec5SDimitry Andric ? &*Fallthrough 848*0b57cec5SDimitry Andric : nullptr; 849*0b57cec5SDimitry Andric } 850*0b57cec5SDimitry Andric 851*0b57cec5SDimitry Andric // If there is no branch, control always falls through. 852*0b57cec5SDimitry Andric if (!TBB) return &*Fallthrough; 853*0b57cec5SDimitry Andric 854*0b57cec5SDimitry Andric // If there is some explicit branch to the fallthrough block, it can obviously 855*0b57cec5SDimitry Andric // reach, even though the branch should get folded to fall through implicitly. 856*0b57cec5SDimitry Andric if (MachineFunction::iterator(TBB) == Fallthrough || 857*0b57cec5SDimitry Andric MachineFunction::iterator(FBB) == Fallthrough) 858*0b57cec5SDimitry Andric return &*Fallthrough; 859*0b57cec5SDimitry Andric 860*0b57cec5SDimitry Andric // If it's an unconditional branch to some block not the fall through, it 861*0b57cec5SDimitry Andric // doesn't fall through. 862*0b57cec5SDimitry Andric if (Cond.empty()) return nullptr; 863*0b57cec5SDimitry Andric 864*0b57cec5SDimitry Andric // Otherwise, if it is conditional and has no explicit false block, it falls 865*0b57cec5SDimitry Andric // through. 866*0b57cec5SDimitry Andric return (FBB == nullptr) ? &*Fallthrough : nullptr; 867*0b57cec5SDimitry Andric } 868*0b57cec5SDimitry Andric 869*0b57cec5SDimitry Andric bool MachineBasicBlock::canFallThrough() { 870*0b57cec5SDimitry Andric return getFallThrough() != nullptr; 871*0b57cec5SDimitry Andric } 872*0b57cec5SDimitry Andric 873*0b57cec5SDimitry Andric MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, 874*0b57cec5SDimitry Andric Pass &P) { 875*0b57cec5SDimitry Andric if (!canSplitCriticalEdge(Succ)) 876*0b57cec5SDimitry Andric return nullptr; 877*0b57cec5SDimitry Andric 878*0b57cec5SDimitry Andric MachineFunction *MF = getParent(); 879*0b57cec5SDimitry Andric DebugLoc DL; // FIXME: this is nowhere 880*0b57cec5SDimitry Andric 881*0b57cec5SDimitry Andric MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock(); 882*0b57cec5SDimitry Andric MF->insert(std::next(MachineFunction::iterator(this)), NMBB); 883*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Splitting critical edge: " << printMBBReference(*this) 884*0b57cec5SDimitry Andric << " -- " << printMBBReference(*NMBB) << " -- " 885*0b57cec5SDimitry Andric << printMBBReference(*Succ) << '\n'); 886*0b57cec5SDimitry Andric 887*0b57cec5SDimitry Andric LiveIntervals *LIS = P.getAnalysisIfAvailable<LiveIntervals>(); 888*0b57cec5SDimitry Andric SlotIndexes *Indexes = P.getAnalysisIfAvailable<SlotIndexes>(); 889*0b57cec5SDimitry Andric if (LIS) 890*0b57cec5SDimitry Andric LIS->insertMBBInMaps(NMBB); 891*0b57cec5SDimitry Andric else if (Indexes) 892*0b57cec5SDimitry Andric Indexes->insertMBBInMaps(NMBB); 893*0b57cec5SDimitry Andric 894*0b57cec5SDimitry Andric // On some targets like Mips, branches may kill virtual registers. Make sure 895*0b57cec5SDimitry Andric // that LiveVariables is properly updated after updateTerminator replaces the 896*0b57cec5SDimitry Andric // terminators. 897*0b57cec5SDimitry Andric LiveVariables *LV = P.getAnalysisIfAvailable<LiveVariables>(); 898*0b57cec5SDimitry Andric 899*0b57cec5SDimitry Andric // Collect a list of virtual registers killed by the terminators. 900*0b57cec5SDimitry Andric SmallVector<unsigned, 4> KilledRegs; 901*0b57cec5SDimitry Andric if (LV) 902*0b57cec5SDimitry Andric for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); 903*0b57cec5SDimitry Andric I != E; ++I) { 904*0b57cec5SDimitry Andric MachineInstr *MI = &*I; 905*0b57cec5SDimitry Andric for (MachineInstr::mop_iterator OI = MI->operands_begin(), 906*0b57cec5SDimitry Andric OE = MI->operands_end(); OI != OE; ++OI) { 907*0b57cec5SDimitry Andric if (!OI->isReg() || OI->getReg() == 0 || 908*0b57cec5SDimitry Andric !OI->isUse() || !OI->isKill() || OI->isUndef()) 909*0b57cec5SDimitry Andric continue; 910*0b57cec5SDimitry Andric unsigned Reg = OI->getReg(); 911*0b57cec5SDimitry Andric if (TargetRegisterInfo::isPhysicalRegister(Reg) || 912*0b57cec5SDimitry Andric LV->getVarInfo(Reg).removeKill(*MI)) { 913*0b57cec5SDimitry Andric KilledRegs.push_back(Reg); 914*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Removing terminator kill: " << *MI); 915*0b57cec5SDimitry Andric OI->setIsKill(false); 916*0b57cec5SDimitry Andric } 917*0b57cec5SDimitry Andric } 918*0b57cec5SDimitry Andric } 919*0b57cec5SDimitry Andric 920*0b57cec5SDimitry Andric SmallVector<unsigned, 4> UsedRegs; 921*0b57cec5SDimitry Andric if (LIS) { 922*0b57cec5SDimitry Andric for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); 923*0b57cec5SDimitry Andric I != E; ++I) { 924*0b57cec5SDimitry Andric MachineInstr *MI = &*I; 925*0b57cec5SDimitry Andric 926*0b57cec5SDimitry Andric for (MachineInstr::mop_iterator OI = MI->operands_begin(), 927*0b57cec5SDimitry Andric OE = MI->operands_end(); OI != OE; ++OI) { 928*0b57cec5SDimitry Andric if (!OI->isReg() || OI->getReg() == 0) 929*0b57cec5SDimitry Andric continue; 930*0b57cec5SDimitry Andric 931*0b57cec5SDimitry Andric unsigned Reg = OI->getReg(); 932*0b57cec5SDimitry Andric if (!is_contained(UsedRegs, Reg)) 933*0b57cec5SDimitry Andric UsedRegs.push_back(Reg); 934*0b57cec5SDimitry Andric } 935*0b57cec5SDimitry Andric } 936*0b57cec5SDimitry Andric } 937*0b57cec5SDimitry Andric 938*0b57cec5SDimitry Andric ReplaceUsesOfBlockWith(Succ, NMBB); 939*0b57cec5SDimitry Andric 940*0b57cec5SDimitry Andric // If updateTerminator() removes instructions, we need to remove them from 941*0b57cec5SDimitry Andric // SlotIndexes. 942*0b57cec5SDimitry Andric SmallVector<MachineInstr*, 4> Terminators; 943*0b57cec5SDimitry Andric if (Indexes) { 944*0b57cec5SDimitry Andric for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); 945*0b57cec5SDimitry Andric I != E; ++I) 946*0b57cec5SDimitry Andric Terminators.push_back(&*I); 947*0b57cec5SDimitry Andric } 948*0b57cec5SDimitry Andric 949*0b57cec5SDimitry Andric updateTerminator(); 950*0b57cec5SDimitry Andric 951*0b57cec5SDimitry Andric if (Indexes) { 952*0b57cec5SDimitry Andric SmallVector<MachineInstr*, 4> NewTerminators; 953*0b57cec5SDimitry Andric for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); 954*0b57cec5SDimitry Andric I != E; ++I) 955*0b57cec5SDimitry Andric NewTerminators.push_back(&*I); 956*0b57cec5SDimitry Andric 957*0b57cec5SDimitry Andric for (SmallVectorImpl<MachineInstr*>::iterator I = Terminators.begin(), 958*0b57cec5SDimitry Andric E = Terminators.end(); I != E; ++I) { 959*0b57cec5SDimitry Andric if (!is_contained(NewTerminators, *I)) 960*0b57cec5SDimitry Andric Indexes->removeMachineInstrFromMaps(**I); 961*0b57cec5SDimitry Andric } 962*0b57cec5SDimitry Andric } 963*0b57cec5SDimitry Andric 964*0b57cec5SDimitry Andric // Insert unconditional "jump Succ" instruction in NMBB if necessary. 965*0b57cec5SDimitry Andric NMBB->addSuccessor(Succ); 966*0b57cec5SDimitry Andric if (!NMBB->isLayoutSuccessor(Succ)) { 967*0b57cec5SDimitry Andric SmallVector<MachineOperand, 4> Cond; 968*0b57cec5SDimitry Andric const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo(); 969*0b57cec5SDimitry Andric TII->insertBranch(*NMBB, Succ, nullptr, Cond, DL); 970*0b57cec5SDimitry Andric 971*0b57cec5SDimitry Andric if (Indexes) { 972*0b57cec5SDimitry Andric for (MachineInstr &MI : NMBB->instrs()) { 973*0b57cec5SDimitry Andric // Some instructions may have been moved to NMBB by updateTerminator(), 974*0b57cec5SDimitry Andric // so we first remove any instruction that already has an index. 975*0b57cec5SDimitry Andric if (Indexes->hasIndex(MI)) 976*0b57cec5SDimitry Andric Indexes->removeMachineInstrFromMaps(MI); 977*0b57cec5SDimitry Andric Indexes->insertMachineInstrInMaps(MI); 978*0b57cec5SDimitry Andric } 979*0b57cec5SDimitry Andric } 980*0b57cec5SDimitry Andric } 981*0b57cec5SDimitry Andric 982*0b57cec5SDimitry Andric // Fix PHI nodes in Succ so they refer to NMBB instead of this 983*0b57cec5SDimitry Andric for (MachineBasicBlock::instr_iterator 984*0b57cec5SDimitry Andric i = Succ->instr_begin(),e = Succ->instr_end(); 985*0b57cec5SDimitry Andric i != e && i->isPHI(); ++i) 986*0b57cec5SDimitry Andric for (unsigned ni = 1, ne = i->getNumOperands(); ni != ne; ni += 2) 987*0b57cec5SDimitry Andric if (i->getOperand(ni+1).getMBB() == this) 988*0b57cec5SDimitry Andric i->getOperand(ni+1).setMBB(NMBB); 989*0b57cec5SDimitry Andric 990*0b57cec5SDimitry Andric // Inherit live-ins from the successor 991*0b57cec5SDimitry Andric for (const auto &LI : Succ->liveins()) 992*0b57cec5SDimitry Andric NMBB->addLiveIn(LI); 993*0b57cec5SDimitry Andric 994*0b57cec5SDimitry Andric // Update LiveVariables. 995*0b57cec5SDimitry Andric const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); 996*0b57cec5SDimitry Andric if (LV) { 997*0b57cec5SDimitry Andric // Restore kills of virtual registers that were killed by the terminators. 998*0b57cec5SDimitry Andric while (!KilledRegs.empty()) { 999*0b57cec5SDimitry Andric unsigned Reg = KilledRegs.pop_back_val(); 1000*0b57cec5SDimitry Andric for (instr_iterator I = instr_end(), E = instr_begin(); I != E;) { 1001*0b57cec5SDimitry Andric if (!(--I)->addRegisterKilled(Reg, TRI, /* AddIfNotFound= */ false)) 1002*0b57cec5SDimitry Andric continue; 1003*0b57cec5SDimitry Andric if (TargetRegisterInfo::isVirtualRegister(Reg)) 1004*0b57cec5SDimitry Andric LV->getVarInfo(Reg).Kills.push_back(&*I); 1005*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Restored terminator kill: " << *I); 1006*0b57cec5SDimitry Andric break; 1007*0b57cec5SDimitry Andric } 1008*0b57cec5SDimitry Andric } 1009*0b57cec5SDimitry Andric // Update relevant live-through information. 1010*0b57cec5SDimitry Andric LV->addNewBlock(NMBB, this, Succ); 1011*0b57cec5SDimitry Andric } 1012*0b57cec5SDimitry Andric 1013*0b57cec5SDimitry Andric if (LIS) { 1014*0b57cec5SDimitry Andric // After splitting the edge and updating SlotIndexes, live intervals may be 1015*0b57cec5SDimitry Andric // in one of two situations, depending on whether this block was the last in 1016*0b57cec5SDimitry Andric // the function. If the original block was the last in the function, all 1017*0b57cec5SDimitry Andric // live intervals will end prior to the beginning of the new split block. If 1018*0b57cec5SDimitry Andric // the original block was not at the end of the function, all live intervals 1019*0b57cec5SDimitry Andric // will extend to the end of the new split block. 1020*0b57cec5SDimitry Andric 1021*0b57cec5SDimitry Andric bool isLastMBB = 1022*0b57cec5SDimitry Andric std::next(MachineFunction::iterator(NMBB)) == getParent()->end(); 1023*0b57cec5SDimitry Andric 1024*0b57cec5SDimitry Andric SlotIndex StartIndex = Indexes->getMBBEndIdx(this); 1025*0b57cec5SDimitry Andric SlotIndex PrevIndex = StartIndex.getPrevSlot(); 1026*0b57cec5SDimitry Andric SlotIndex EndIndex = Indexes->getMBBEndIdx(NMBB); 1027*0b57cec5SDimitry Andric 1028*0b57cec5SDimitry Andric // Find the registers used from NMBB in PHIs in Succ. 1029*0b57cec5SDimitry Andric SmallSet<unsigned, 8> PHISrcRegs; 1030*0b57cec5SDimitry Andric for (MachineBasicBlock::instr_iterator 1031*0b57cec5SDimitry Andric I = Succ->instr_begin(), E = Succ->instr_end(); 1032*0b57cec5SDimitry Andric I != E && I->isPHI(); ++I) { 1033*0b57cec5SDimitry Andric for (unsigned ni = 1, ne = I->getNumOperands(); ni != ne; ni += 2) { 1034*0b57cec5SDimitry Andric if (I->getOperand(ni+1).getMBB() == NMBB) { 1035*0b57cec5SDimitry Andric MachineOperand &MO = I->getOperand(ni); 1036*0b57cec5SDimitry Andric unsigned Reg = MO.getReg(); 1037*0b57cec5SDimitry Andric PHISrcRegs.insert(Reg); 1038*0b57cec5SDimitry Andric if (MO.isUndef()) 1039*0b57cec5SDimitry Andric continue; 1040*0b57cec5SDimitry Andric 1041*0b57cec5SDimitry Andric LiveInterval &LI = LIS->getInterval(Reg); 1042*0b57cec5SDimitry Andric VNInfo *VNI = LI.getVNInfoAt(PrevIndex); 1043*0b57cec5SDimitry Andric assert(VNI && 1044*0b57cec5SDimitry Andric "PHI sources should be live out of their predecessors."); 1045*0b57cec5SDimitry Andric LI.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI)); 1046*0b57cec5SDimitry Andric } 1047*0b57cec5SDimitry Andric } 1048*0b57cec5SDimitry Andric } 1049*0b57cec5SDimitry Andric 1050*0b57cec5SDimitry Andric MachineRegisterInfo *MRI = &getParent()->getRegInfo(); 1051*0b57cec5SDimitry Andric for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { 1052*0b57cec5SDimitry Andric unsigned Reg = TargetRegisterInfo::index2VirtReg(i); 1053*0b57cec5SDimitry Andric if (PHISrcRegs.count(Reg) || !LIS->hasInterval(Reg)) 1054*0b57cec5SDimitry Andric continue; 1055*0b57cec5SDimitry Andric 1056*0b57cec5SDimitry Andric LiveInterval &LI = LIS->getInterval(Reg); 1057*0b57cec5SDimitry Andric if (!LI.liveAt(PrevIndex)) 1058*0b57cec5SDimitry Andric continue; 1059*0b57cec5SDimitry Andric 1060*0b57cec5SDimitry Andric bool isLiveOut = LI.liveAt(LIS->getMBBStartIdx(Succ)); 1061*0b57cec5SDimitry Andric if (isLiveOut && isLastMBB) { 1062*0b57cec5SDimitry Andric VNInfo *VNI = LI.getVNInfoAt(PrevIndex); 1063*0b57cec5SDimitry Andric assert(VNI && "LiveInterval should have VNInfo where it is live."); 1064*0b57cec5SDimitry Andric LI.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI)); 1065*0b57cec5SDimitry Andric } else if (!isLiveOut && !isLastMBB) { 1066*0b57cec5SDimitry Andric LI.removeSegment(StartIndex, EndIndex); 1067*0b57cec5SDimitry Andric } 1068*0b57cec5SDimitry Andric } 1069*0b57cec5SDimitry Andric 1070*0b57cec5SDimitry Andric // Update all intervals for registers whose uses may have been modified by 1071*0b57cec5SDimitry Andric // updateTerminator(). 1072*0b57cec5SDimitry Andric LIS->repairIntervalsInRange(this, getFirstTerminator(), end(), UsedRegs); 1073*0b57cec5SDimitry Andric } 1074*0b57cec5SDimitry Andric 1075*0b57cec5SDimitry Andric if (MachineDominatorTree *MDT = 1076*0b57cec5SDimitry Andric P.getAnalysisIfAvailable<MachineDominatorTree>()) 1077*0b57cec5SDimitry Andric MDT->recordSplitCriticalEdge(this, Succ, NMBB); 1078*0b57cec5SDimitry Andric 1079*0b57cec5SDimitry Andric if (MachineLoopInfo *MLI = P.getAnalysisIfAvailable<MachineLoopInfo>()) 1080*0b57cec5SDimitry Andric if (MachineLoop *TIL = MLI->getLoopFor(this)) { 1081*0b57cec5SDimitry Andric // If one or the other blocks were not in a loop, the new block is not 1082*0b57cec5SDimitry Andric // either, and thus LI doesn't need to be updated. 1083*0b57cec5SDimitry Andric if (MachineLoop *DestLoop = MLI->getLoopFor(Succ)) { 1084*0b57cec5SDimitry Andric if (TIL == DestLoop) { 1085*0b57cec5SDimitry Andric // Both in the same loop, the NMBB joins loop. 1086*0b57cec5SDimitry Andric DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase()); 1087*0b57cec5SDimitry Andric } else if (TIL->contains(DestLoop)) { 1088*0b57cec5SDimitry Andric // Edge from an outer loop to an inner loop. Add to the outer loop. 1089*0b57cec5SDimitry Andric TIL->addBasicBlockToLoop(NMBB, MLI->getBase()); 1090*0b57cec5SDimitry Andric } else if (DestLoop->contains(TIL)) { 1091*0b57cec5SDimitry Andric // Edge from an inner loop to an outer loop. Add to the outer loop. 1092*0b57cec5SDimitry Andric DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase()); 1093*0b57cec5SDimitry Andric } else { 1094*0b57cec5SDimitry Andric // Edge from two loops with no containment relation. Because these 1095*0b57cec5SDimitry Andric // are natural loops, we know that the destination block must be the 1096*0b57cec5SDimitry Andric // header of its loop (adding a branch into a loop elsewhere would 1097*0b57cec5SDimitry Andric // create an irreducible loop). 1098*0b57cec5SDimitry Andric assert(DestLoop->getHeader() == Succ && 1099*0b57cec5SDimitry Andric "Should not create irreducible loops!"); 1100*0b57cec5SDimitry Andric if (MachineLoop *P = DestLoop->getParentLoop()) 1101*0b57cec5SDimitry Andric P->addBasicBlockToLoop(NMBB, MLI->getBase()); 1102*0b57cec5SDimitry Andric } 1103*0b57cec5SDimitry Andric } 1104*0b57cec5SDimitry Andric } 1105*0b57cec5SDimitry Andric 1106*0b57cec5SDimitry Andric return NMBB; 1107*0b57cec5SDimitry Andric } 1108*0b57cec5SDimitry Andric 1109*0b57cec5SDimitry Andric bool MachineBasicBlock::canSplitCriticalEdge( 1110*0b57cec5SDimitry Andric const MachineBasicBlock *Succ) const { 1111*0b57cec5SDimitry Andric // Splitting the critical edge to a landing pad block is non-trivial. Don't do 1112*0b57cec5SDimitry Andric // it in this generic function. 1113*0b57cec5SDimitry Andric if (Succ->isEHPad()) 1114*0b57cec5SDimitry Andric return false; 1115*0b57cec5SDimitry Andric 1116*0b57cec5SDimitry Andric const MachineFunction *MF = getParent(); 1117*0b57cec5SDimitry Andric 1118*0b57cec5SDimitry Andric // Performance might be harmed on HW that implements branching using exec mask 1119*0b57cec5SDimitry Andric // where both sides of the branches are always executed. 1120*0b57cec5SDimitry Andric if (MF->getTarget().requiresStructuredCFG()) 1121*0b57cec5SDimitry Andric return false; 1122*0b57cec5SDimitry Andric 1123*0b57cec5SDimitry Andric // We may need to update this's terminator, but we can't do that if 1124*0b57cec5SDimitry Andric // AnalyzeBranch fails. If this uses a jump table, we won't touch it. 1125*0b57cec5SDimitry Andric const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); 1126*0b57cec5SDimitry Andric MachineBasicBlock *TBB = nullptr, *FBB = nullptr; 1127*0b57cec5SDimitry Andric SmallVector<MachineOperand, 4> Cond; 1128*0b57cec5SDimitry Andric // AnalyzeBanch should modify this, since we did not allow modification. 1129*0b57cec5SDimitry Andric if (TII->analyzeBranch(*const_cast<MachineBasicBlock *>(this), TBB, FBB, Cond, 1130*0b57cec5SDimitry Andric /*AllowModify*/ false)) 1131*0b57cec5SDimitry Andric return false; 1132*0b57cec5SDimitry Andric 1133*0b57cec5SDimitry Andric // Avoid bugpoint weirdness: A block may end with a conditional branch but 1134*0b57cec5SDimitry Andric // jumps to the same MBB is either case. We have duplicate CFG edges in that 1135*0b57cec5SDimitry Andric // case that we can't handle. Since this never happens in properly optimized 1136*0b57cec5SDimitry Andric // code, just skip those edges. 1137*0b57cec5SDimitry Andric if (TBB && TBB == FBB) { 1138*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Won't split critical edge after degenerate " 1139*0b57cec5SDimitry Andric << printMBBReference(*this) << '\n'); 1140*0b57cec5SDimitry Andric return false; 1141*0b57cec5SDimitry Andric } 1142*0b57cec5SDimitry Andric return true; 1143*0b57cec5SDimitry Andric } 1144*0b57cec5SDimitry Andric 1145*0b57cec5SDimitry Andric /// Prepare MI to be removed from its bundle. This fixes bundle flags on MI's 1146*0b57cec5SDimitry Andric /// neighboring instructions so the bundle won't be broken by removing MI. 1147*0b57cec5SDimitry Andric static void unbundleSingleMI(MachineInstr *MI) { 1148*0b57cec5SDimitry Andric // Removing the first instruction in a bundle. 1149*0b57cec5SDimitry Andric if (MI->isBundledWithSucc() && !MI->isBundledWithPred()) 1150*0b57cec5SDimitry Andric MI->unbundleFromSucc(); 1151*0b57cec5SDimitry Andric // Removing the last instruction in a bundle. 1152*0b57cec5SDimitry Andric if (MI->isBundledWithPred() && !MI->isBundledWithSucc()) 1153*0b57cec5SDimitry Andric MI->unbundleFromPred(); 1154*0b57cec5SDimitry Andric // If MI is not bundled, or if it is internal to a bundle, the neighbor flags 1155*0b57cec5SDimitry Andric // are already fine. 1156*0b57cec5SDimitry Andric } 1157*0b57cec5SDimitry Andric 1158*0b57cec5SDimitry Andric MachineBasicBlock::instr_iterator 1159*0b57cec5SDimitry Andric MachineBasicBlock::erase(MachineBasicBlock::instr_iterator I) { 1160*0b57cec5SDimitry Andric unbundleSingleMI(&*I); 1161*0b57cec5SDimitry Andric return Insts.erase(I); 1162*0b57cec5SDimitry Andric } 1163*0b57cec5SDimitry Andric 1164*0b57cec5SDimitry Andric MachineInstr *MachineBasicBlock::remove_instr(MachineInstr *MI) { 1165*0b57cec5SDimitry Andric unbundleSingleMI(MI); 1166*0b57cec5SDimitry Andric MI->clearFlag(MachineInstr::BundledPred); 1167*0b57cec5SDimitry Andric MI->clearFlag(MachineInstr::BundledSucc); 1168*0b57cec5SDimitry Andric return Insts.remove(MI); 1169*0b57cec5SDimitry Andric } 1170*0b57cec5SDimitry Andric 1171*0b57cec5SDimitry Andric MachineBasicBlock::instr_iterator 1172*0b57cec5SDimitry Andric MachineBasicBlock::insert(instr_iterator I, MachineInstr *MI) { 1173*0b57cec5SDimitry Andric assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() && 1174*0b57cec5SDimitry Andric "Cannot insert instruction with bundle flags"); 1175*0b57cec5SDimitry Andric // Set the bundle flags when inserting inside a bundle. 1176*0b57cec5SDimitry Andric if (I != instr_end() && I->isBundledWithPred()) { 1177*0b57cec5SDimitry Andric MI->setFlag(MachineInstr::BundledPred); 1178*0b57cec5SDimitry Andric MI->setFlag(MachineInstr::BundledSucc); 1179*0b57cec5SDimitry Andric } 1180*0b57cec5SDimitry Andric return Insts.insert(I, MI); 1181*0b57cec5SDimitry Andric } 1182*0b57cec5SDimitry Andric 1183*0b57cec5SDimitry Andric /// This method unlinks 'this' from the containing function, and returns it, but 1184*0b57cec5SDimitry Andric /// does not delete it. 1185*0b57cec5SDimitry Andric MachineBasicBlock *MachineBasicBlock::removeFromParent() { 1186*0b57cec5SDimitry Andric assert(getParent() && "Not embedded in a function!"); 1187*0b57cec5SDimitry Andric getParent()->remove(this); 1188*0b57cec5SDimitry Andric return this; 1189*0b57cec5SDimitry Andric } 1190*0b57cec5SDimitry Andric 1191*0b57cec5SDimitry Andric /// This method unlinks 'this' from the containing function, and deletes it. 1192*0b57cec5SDimitry Andric void MachineBasicBlock::eraseFromParent() { 1193*0b57cec5SDimitry Andric assert(getParent() && "Not embedded in a function!"); 1194*0b57cec5SDimitry Andric getParent()->erase(this); 1195*0b57cec5SDimitry Andric } 1196*0b57cec5SDimitry Andric 1197*0b57cec5SDimitry Andric /// Given a machine basic block that branched to 'Old', change the code and CFG 1198*0b57cec5SDimitry Andric /// so that it branches to 'New' instead. 1199*0b57cec5SDimitry Andric void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old, 1200*0b57cec5SDimitry Andric MachineBasicBlock *New) { 1201*0b57cec5SDimitry Andric assert(Old != New && "Cannot replace self with self!"); 1202*0b57cec5SDimitry Andric 1203*0b57cec5SDimitry Andric MachineBasicBlock::instr_iterator I = instr_end(); 1204*0b57cec5SDimitry Andric while (I != instr_begin()) { 1205*0b57cec5SDimitry Andric --I; 1206*0b57cec5SDimitry Andric if (!I->isTerminator()) break; 1207*0b57cec5SDimitry Andric 1208*0b57cec5SDimitry Andric // Scan the operands of this machine instruction, replacing any uses of Old 1209*0b57cec5SDimitry Andric // with New. 1210*0b57cec5SDimitry Andric for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 1211*0b57cec5SDimitry Andric if (I->getOperand(i).isMBB() && 1212*0b57cec5SDimitry Andric I->getOperand(i).getMBB() == Old) 1213*0b57cec5SDimitry Andric I->getOperand(i).setMBB(New); 1214*0b57cec5SDimitry Andric } 1215*0b57cec5SDimitry Andric 1216*0b57cec5SDimitry Andric // Update the successor information. 1217*0b57cec5SDimitry Andric replaceSuccessor(Old, New); 1218*0b57cec5SDimitry Andric } 1219*0b57cec5SDimitry Andric 1220*0b57cec5SDimitry Andric /// Various pieces of code can cause excess edges in the CFG to be inserted. If 1221*0b57cec5SDimitry Andric /// we have proven that MBB can only branch to DestA and DestB, remove any other 1222*0b57cec5SDimitry Andric /// MBB successors from the CFG. DestA and DestB can be null. 1223*0b57cec5SDimitry Andric /// 1224*0b57cec5SDimitry Andric /// Besides DestA and DestB, retain other edges leading to LandingPads 1225*0b57cec5SDimitry Andric /// (currently there can be only one; we don't check or require that here). 1226*0b57cec5SDimitry Andric /// Note it is possible that DestA and/or DestB are LandingPads. 1227*0b57cec5SDimitry Andric bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA, 1228*0b57cec5SDimitry Andric MachineBasicBlock *DestB, 1229*0b57cec5SDimitry Andric bool IsCond) { 1230*0b57cec5SDimitry Andric // The values of DestA and DestB frequently come from a call to the 1231*0b57cec5SDimitry Andric // 'TargetInstrInfo::AnalyzeBranch' method. We take our meaning of the initial 1232*0b57cec5SDimitry Andric // values from there. 1233*0b57cec5SDimitry Andric // 1234*0b57cec5SDimitry Andric // 1. If both DestA and DestB are null, then the block ends with no branches 1235*0b57cec5SDimitry Andric // (it falls through to its successor). 1236*0b57cec5SDimitry Andric // 2. If DestA is set, DestB is null, and IsCond is false, then the block ends 1237*0b57cec5SDimitry Andric // with only an unconditional branch. 1238*0b57cec5SDimitry Andric // 3. If DestA is set, DestB is null, and IsCond is true, then the block ends 1239*0b57cec5SDimitry Andric // with a conditional branch that falls through to a successor (DestB). 1240*0b57cec5SDimitry Andric // 4. If DestA and DestB is set and IsCond is true, then the block ends with a 1241*0b57cec5SDimitry Andric // conditional branch followed by an unconditional branch. DestA is the 1242*0b57cec5SDimitry Andric // 'true' destination and DestB is the 'false' destination. 1243*0b57cec5SDimitry Andric 1244*0b57cec5SDimitry Andric bool Changed = false; 1245*0b57cec5SDimitry Andric 1246*0b57cec5SDimitry Andric MachineBasicBlock *FallThru = getNextNode(); 1247*0b57cec5SDimitry Andric 1248*0b57cec5SDimitry Andric if (!DestA && !DestB) { 1249*0b57cec5SDimitry Andric // Block falls through to successor. 1250*0b57cec5SDimitry Andric DestA = FallThru; 1251*0b57cec5SDimitry Andric DestB = FallThru; 1252*0b57cec5SDimitry Andric } else if (DestA && !DestB) { 1253*0b57cec5SDimitry Andric if (IsCond) 1254*0b57cec5SDimitry Andric // Block ends in conditional jump that falls through to successor. 1255*0b57cec5SDimitry Andric DestB = FallThru; 1256*0b57cec5SDimitry Andric } else { 1257*0b57cec5SDimitry Andric assert(DestA && DestB && IsCond && 1258*0b57cec5SDimitry Andric "CFG in a bad state. Cannot correct CFG edges"); 1259*0b57cec5SDimitry Andric } 1260*0b57cec5SDimitry Andric 1261*0b57cec5SDimitry Andric // Remove superfluous edges. I.e., those which aren't destinations of this 1262*0b57cec5SDimitry Andric // basic block, duplicate edges, or landing pads. 1263*0b57cec5SDimitry Andric SmallPtrSet<const MachineBasicBlock*, 8> SeenMBBs; 1264*0b57cec5SDimitry Andric MachineBasicBlock::succ_iterator SI = succ_begin(); 1265*0b57cec5SDimitry Andric while (SI != succ_end()) { 1266*0b57cec5SDimitry Andric const MachineBasicBlock *MBB = *SI; 1267*0b57cec5SDimitry Andric if (!SeenMBBs.insert(MBB).second || 1268*0b57cec5SDimitry Andric (MBB != DestA && MBB != DestB && !MBB->isEHPad())) { 1269*0b57cec5SDimitry Andric // This is a superfluous edge, remove it. 1270*0b57cec5SDimitry Andric SI = removeSuccessor(SI); 1271*0b57cec5SDimitry Andric Changed = true; 1272*0b57cec5SDimitry Andric } else { 1273*0b57cec5SDimitry Andric ++SI; 1274*0b57cec5SDimitry Andric } 1275*0b57cec5SDimitry Andric } 1276*0b57cec5SDimitry Andric 1277*0b57cec5SDimitry Andric if (Changed) 1278*0b57cec5SDimitry Andric normalizeSuccProbs(); 1279*0b57cec5SDimitry Andric return Changed; 1280*0b57cec5SDimitry Andric } 1281*0b57cec5SDimitry Andric 1282*0b57cec5SDimitry Andric /// Find the next valid DebugLoc starting at MBBI, skipping any DBG_VALUE 1283*0b57cec5SDimitry Andric /// instructions. Return UnknownLoc if there is none. 1284*0b57cec5SDimitry Andric DebugLoc 1285*0b57cec5SDimitry Andric MachineBasicBlock::findDebugLoc(instr_iterator MBBI) { 1286*0b57cec5SDimitry Andric // Skip debug declarations, we don't want a DebugLoc from them. 1287*0b57cec5SDimitry Andric MBBI = skipDebugInstructionsForward(MBBI, instr_end()); 1288*0b57cec5SDimitry Andric if (MBBI != instr_end()) 1289*0b57cec5SDimitry Andric return MBBI->getDebugLoc(); 1290*0b57cec5SDimitry Andric return {}; 1291*0b57cec5SDimitry Andric } 1292*0b57cec5SDimitry Andric 1293*0b57cec5SDimitry Andric /// Find the previous valid DebugLoc preceding MBBI, skipping and DBG_VALUE 1294*0b57cec5SDimitry Andric /// instructions. Return UnknownLoc if there is none. 1295*0b57cec5SDimitry Andric DebugLoc MachineBasicBlock::findPrevDebugLoc(instr_iterator MBBI) { 1296*0b57cec5SDimitry Andric if (MBBI == instr_begin()) return {}; 1297*0b57cec5SDimitry Andric // Skip debug declarations, we don't want a DebugLoc from them. 1298*0b57cec5SDimitry Andric MBBI = skipDebugInstructionsBackward(std::prev(MBBI), instr_begin()); 1299*0b57cec5SDimitry Andric if (!MBBI->isDebugInstr()) return MBBI->getDebugLoc(); 1300*0b57cec5SDimitry Andric return {}; 1301*0b57cec5SDimitry Andric } 1302*0b57cec5SDimitry Andric 1303*0b57cec5SDimitry Andric /// Find and return the merged DebugLoc of the branch instructions of the block. 1304*0b57cec5SDimitry Andric /// Return UnknownLoc if there is none. 1305*0b57cec5SDimitry Andric DebugLoc 1306*0b57cec5SDimitry Andric MachineBasicBlock::findBranchDebugLoc() { 1307*0b57cec5SDimitry Andric DebugLoc DL; 1308*0b57cec5SDimitry Andric auto TI = getFirstTerminator(); 1309*0b57cec5SDimitry Andric while (TI != end() && !TI->isBranch()) 1310*0b57cec5SDimitry Andric ++TI; 1311*0b57cec5SDimitry Andric 1312*0b57cec5SDimitry Andric if (TI != end()) { 1313*0b57cec5SDimitry Andric DL = TI->getDebugLoc(); 1314*0b57cec5SDimitry Andric for (++TI ; TI != end() ; ++TI) 1315*0b57cec5SDimitry Andric if (TI->isBranch()) 1316*0b57cec5SDimitry Andric DL = DILocation::getMergedLocation(DL, TI->getDebugLoc()); 1317*0b57cec5SDimitry Andric } 1318*0b57cec5SDimitry Andric return DL; 1319*0b57cec5SDimitry Andric } 1320*0b57cec5SDimitry Andric 1321*0b57cec5SDimitry Andric /// Return probability of the edge from this block to MBB. 1322*0b57cec5SDimitry Andric BranchProbability 1323*0b57cec5SDimitry Andric MachineBasicBlock::getSuccProbability(const_succ_iterator Succ) const { 1324*0b57cec5SDimitry Andric if (Probs.empty()) 1325*0b57cec5SDimitry Andric return BranchProbability(1, succ_size()); 1326*0b57cec5SDimitry Andric 1327*0b57cec5SDimitry Andric const auto &Prob = *getProbabilityIterator(Succ); 1328*0b57cec5SDimitry Andric if (Prob.isUnknown()) { 1329*0b57cec5SDimitry Andric // For unknown probabilities, collect the sum of all known ones, and evenly 1330*0b57cec5SDimitry Andric // ditribute the complemental of the sum to each unknown probability. 1331*0b57cec5SDimitry Andric unsigned KnownProbNum = 0; 1332*0b57cec5SDimitry Andric auto Sum = BranchProbability::getZero(); 1333*0b57cec5SDimitry Andric for (auto &P : Probs) { 1334*0b57cec5SDimitry Andric if (!P.isUnknown()) { 1335*0b57cec5SDimitry Andric Sum += P; 1336*0b57cec5SDimitry Andric KnownProbNum++; 1337*0b57cec5SDimitry Andric } 1338*0b57cec5SDimitry Andric } 1339*0b57cec5SDimitry Andric return Sum.getCompl() / (Probs.size() - KnownProbNum); 1340*0b57cec5SDimitry Andric } else 1341*0b57cec5SDimitry Andric return Prob; 1342*0b57cec5SDimitry Andric } 1343*0b57cec5SDimitry Andric 1344*0b57cec5SDimitry Andric /// Set successor probability of a given iterator. 1345*0b57cec5SDimitry Andric void MachineBasicBlock::setSuccProbability(succ_iterator I, 1346*0b57cec5SDimitry Andric BranchProbability Prob) { 1347*0b57cec5SDimitry Andric assert(!Prob.isUnknown()); 1348*0b57cec5SDimitry Andric if (Probs.empty()) 1349*0b57cec5SDimitry Andric return; 1350*0b57cec5SDimitry Andric *getProbabilityIterator(I) = Prob; 1351*0b57cec5SDimitry Andric } 1352*0b57cec5SDimitry Andric 1353*0b57cec5SDimitry Andric /// Return probability iterator corresonding to the I successor iterator 1354*0b57cec5SDimitry Andric MachineBasicBlock::const_probability_iterator 1355*0b57cec5SDimitry Andric MachineBasicBlock::getProbabilityIterator( 1356*0b57cec5SDimitry Andric MachineBasicBlock::const_succ_iterator I) const { 1357*0b57cec5SDimitry Andric assert(Probs.size() == Successors.size() && "Async probability list!"); 1358*0b57cec5SDimitry Andric const size_t index = std::distance(Successors.begin(), I); 1359*0b57cec5SDimitry Andric assert(index < Probs.size() && "Not a current successor!"); 1360*0b57cec5SDimitry Andric return Probs.begin() + index; 1361*0b57cec5SDimitry Andric } 1362*0b57cec5SDimitry Andric 1363*0b57cec5SDimitry Andric /// Return probability iterator corresonding to the I successor iterator. 1364*0b57cec5SDimitry Andric MachineBasicBlock::probability_iterator 1365*0b57cec5SDimitry Andric MachineBasicBlock::getProbabilityIterator(MachineBasicBlock::succ_iterator I) { 1366*0b57cec5SDimitry Andric assert(Probs.size() == Successors.size() && "Async probability list!"); 1367*0b57cec5SDimitry Andric const size_t index = std::distance(Successors.begin(), I); 1368*0b57cec5SDimitry Andric assert(index < Probs.size() && "Not a current successor!"); 1369*0b57cec5SDimitry Andric return Probs.begin() + index; 1370*0b57cec5SDimitry Andric } 1371*0b57cec5SDimitry Andric 1372*0b57cec5SDimitry Andric /// Return whether (physical) register "Reg" has been <def>ined and not <kill>ed 1373*0b57cec5SDimitry Andric /// as of just before "MI". 1374*0b57cec5SDimitry Andric /// 1375*0b57cec5SDimitry Andric /// Search is localised to a neighborhood of 1376*0b57cec5SDimitry Andric /// Neighborhood instructions before (searching for defs or kills) and N 1377*0b57cec5SDimitry Andric /// instructions after (searching just for defs) MI. 1378*0b57cec5SDimitry Andric MachineBasicBlock::LivenessQueryResult 1379*0b57cec5SDimitry Andric MachineBasicBlock::computeRegisterLiveness(const TargetRegisterInfo *TRI, 1380*0b57cec5SDimitry Andric unsigned Reg, const_iterator Before, 1381*0b57cec5SDimitry Andric unsigned Neighborhood) const { 1382*0b57cec5SDimitry Andric unsigned N = Neighborhood; 1383*0b57cec5SDimitry Andric 1384*0b57cec5SDimitry Andric // Try searching forwards from Before, looking for reads or defs. 1385*0b57cec5SDimitry Andric const_iterator I(Before); 1386*0b57cec5SDimitry Andric for (; I != end() && N > 0; ++I) { 1387*0b57cec5SDimitry Andric if (I->isDebugInstr()) 1388*0b57cec5SDimitry Andric continue; 1389*0b57cec5SDimitry Andric 1390*0b57cec5SDimitry Andric --N; 1391*0b57cec5SDimitry Andric 1392*0b57cec5SDimitry Andric MachineOperandIteratorBase::PhysRegInfo Info = 1393*0b57cec5SDimitry Andric ConstMIOperands(*I).analyzePhysReg(Reg, TRI); 1394*0b57cec5SDimitry Andric 1395*0b57cec5SDimitry Andric // Register is live when we read it here. 1396*0b57cec5SDimitry Andric if (Info.Read) 1397*0b57cec5SDimitry Andric return LQR_Live; 1398*0b57cec5SDimitry Andric // Register is dead if we can fully overwrite or clobber it here. 1399*0b57cec5SDimitry Andric if (Info.FullyDefined || Info.Clobbered) 1400*0b57cec5SDimitry Andric return LQR_Dead; 1401*0b57cec5SDimitry Andric } 1402*0b57cec5SDimitry Andric 1403*0b57cec5SDimitry Andric // If we reached the end, it is safe to clobber Reg at the end of a block of 1404*0b57cec5SDimitry Andric // no successor has it live in. 1405*0b57cec5SDimitry Andric if (I == end()) { 1406*0b57cec5SDimitry Andric for (MachineBasicBlock *S : successors()) { 1407*0b57cec5SDimitry Andric for (const MachineBasicBlock::RegisterMaskPair &LI : S->liveins()) { 1408*0b57cec5SDimitry Andric if (TRI->regsOverlap(LI.PhysReg, Reg)) 1409*0b57cec5SDimitry Andric return LQR_Live; 1410*0b57cec5SDimitry Andric } 1411*0b57cec5SDimitry Andric } 1412*0b57cec5SDimitry Andric 1413*0b57cec5SDimitry Andric return LQR_Dead; 1414*0b57cec5SDimitry Andric } 1415*0b57cec5SDimitry Andric 1416*0b57cec5SDimitry Andric 1417*0b57cec5SDimitry Andric N = Neighborhood; 1418*0b57cec5SDimitry Andric 1419*0b57cec5SDimitry Andric // Start by searching backwards from Before, looking for kills, reads or defs. 1420*0b57cec5SDimitry Andric I = const_iterator(Before); 1421*0b57cec5SDimitry Andric // If this is the first insn in the block, don't search backwards. 1422*0b57cec5SDimitry Andric if (I != begin()) { 1423*0b57cec5SDimitry Andric do { 1424*0b57cec5SDimitry Andric --I; 1425*0b57cec5SDimitry Andric 1426*0b57cec5SDimitry Andric if (I->isDebugInstr()) 1427*0b57cec5SDimitry Andric continue; 1428*0b57cec5SDimitry Andric 1429*0b57cec5SDimitry Andric --N; 1430*0b57cec5SDimitry Andric 1431*0b57cec5SDimitry Andric MachineOperandIteratorBase::PhysRegInfo Info = 1432*0b57cec5SDimitry Andric ConstMIOperands(*I).analyzePhysReg(Reg, TRI); 1433*0b57cec5SDimitry Andric 1434*0b57cec5SDimitry Andric // Defs happen after uses so they take precedence if both are present. 1435*0b57cec5SDimitry Andric 1436*0b57cec5SDimitry Andric // Register is dead after a dead def of the full register. 1437*0b57cec5SDimitry Andric if (Info.DeadDef) 1438*0b57cec5SDimitry Andric return LQR_Dead; 1439*0b57cec5SDimitry Andric // Register is (at least partially) live after a def. 1440*0b57cec5SDimitry Andric if (Info.Defined) { 1441*0b57cec5SDimitry Andric if (!Info.PartialDeadDef) 1442*0b57cec5SDimitry Andric return LQR_Live; 1443*0b57cec5SDimitry Andric // As soon as we saw a partial definition (dead or not), 1444*0b57cec5SDimitry Andric // we cannot tell if the value is partial live without 1445*0b57cec5SDimitry Andric // tracking the lanemasks. We are not going to do this, 1446*0b57cec5SDimitry Andric // so fall back on the remaining of the analysis. 1447*0b57cec5SDimitry Andric break; 1448*0b57cec5SDimitry Andric } 1449*0b57cec5SDimitry Andric // Register is dead after a full kill or clobber and no def. 1450*0b57cec5SDimitry Andric if (Info.Killed || Info.Clobbered) 1451*0b57cec5SDimitry Andric return LQR_Dead; 1452*0b57cec5SDimitry Andric // Register must be live if we read it. 1453*0b57cec5SDimitry Andric if (Info.Read) 1454*0b57cec5SDimitry Andric return LQR_Live; 1455*0b57cec5SDimitry Andric 1456*0b57cec5SDimitry Andric } while (I != begin() && N > 0); 1457*0b57cec5SDimitry Andric } 1458*0b57cec5SDimitry Andric 1459*0b57cec5SDimitry Andric // Did we get to the start of the block? 1460*0b57cec5SDimitry Andric if (I == begin()) { 1461*0b57cec5SDimitry Andric // If so, the register's state is definitely defined by the live-in state. 1462*0b57cec5SDimitry Andric for (const MachineBasicBlock::RegisterMaskPair &LI : liveins()) 1463*0b57cec5SDimitry Andric if (TRI->regsOverlap(LI.PhysReg, Reg)) 1464*0b57cec5SDimitry Andric return LQR_Live; 1465*0b57cec5SDimitry Andric 1466*0b57cec5SDimitry Andric return LQR_Dead; 1467*0b57cec5SDimitry Andric } 1468*0b57cec5SDimitry Andric 1469*0b57cec5SDimitry Andric // At this point we have no idea of the liveness of the register. 1470*0b57cec5SDimitry Andric return LQR_Unknown; 1471*0b57cec5SDimitry Andric } 1472*0b57cec5SDimitry Andric 1473*0b57cec5SDimitry Andric const uint32_t * 1474*0b57cec5SDimitry Andric MachineBasicBlock::getBeginClobberMask(const TargetRegisterInfo *TRI) const { 1475*0b57cec5SDimitry Andric // EH funclet entry does not preserve any registers. 1476*0b57cec5SDimitry Andric return isEHFuncletEntry() ? TRI->getNoPreservedMask() : nullptr; 1477*0b57cec5SDimitry Andric } 1478*0b57cec5SDimitry Andric 1479*0b57cec5SDimitry Andric const uint32_t * 1480*0b57cec5SDimitry Andric MachineBasicBlock::getEndClobberMask(const TargetRegisterInfo *TRI) const { 1481*0b57cec5SDimitry Andric // If we see a return block with successors, this must be a funclet return, 1482*0b57cec5SDimitry Andric // which does not preserve any registers. If there are no successors, we don't 1483*0b57cec5SDimitry Andric // care what kind of return it is, putting a mask after it is a no-op. 1484*0b57cec5SDimitry Andric return isReturnBlock() && !succ_empty() ? TRI->getNoPreservedMask() : nullptr; 1485*0b57cec5SDimitry Andric } 1486*0b57cec5SDimitry Andric 1487*0b57cec5SDimitry Andric void MachineBasicBlock::clearLiveIns() { 1488*0b57cec5SDimitry Andric LiveIns.clear(); 1489*0b57cec5SDimitry Andric } 1490*0b57cec5SDimitry Andric 1491*0b57cec5SDimitry Andric MachineBasicBlock::livein_iterator MachineBasicBlock::livein_begin() const { 1492*0b57cec5SDimitry Andric assert(getParent()->getProperties().hasProperty( 1493*0b57cec5SDimitry Andric MachineFunctionProperties::Property::TracksLiveness) && 1494*0b57cec5SDimitry Andric "Liveness information is accurate"); 1495*0b57cec5SDimitry Andric return LiveIns.begin(); 1496*0b57cec5SDimitry Andric } 1497