1 //===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Collect the sequence of machine instructions for a basic block.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/CodeGen/MachineBasicBlock.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/CodeGen/LiveIntervals.h"
17 #include "llvm/CodeGen/LivePhysRegs.h"
18 #include "llvm/CodeGen/LiveVariables.h"
19 #include "llvm/CodeGen/MachineDominators.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineJumpTableInfo.h"
23 #include "llvm/CodeGen/MachineLoopInfo.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/SlotIndexes.h"
26 #include "llvm/CodeGen/TargetInstrInfo.h"
27 #include "llvm/CodeGen/TargetLowering.h"
28 #include "llvm/CodeGen/TargetRegisterInfo.h"
29 #include "llvm/CodeGen/TargetSubtargetInfo.h"
30 #include "llvm/Config/llvm-config.h"
31 #include "llvm/IR/BasicBlock.h"
32 #include "llvm/IR/DebugInfoMetadata.h"
33 #include "llvm/IR/ModuleSlotTracker.h"
34 #include "llvm/MC/MCAsmInfo.h"
35 #include "llvm/MC/MCContext.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include "llvm/Target/TargetMachine.h"
43 #define DEBUG_TYPE "codegen"
45 static cl::opt
<bool> PrintSlotIndexes(
47 cl::desc("When printing machine IR, annotate instructions and blocks with "
48 "SlotIndexes when available"),
49 cl::init(true), cl::Hidden
);
51 MachineBasicBlock::MachineBasicBlock(MachineFunction
&MF
, const BasicBlock
*B
)
52 : BB(B
), Number(-1), xParent(&MF
) {
55 IrrLoopHeaderWeight
= B
->getIrrLoopHeaderWeight();
58 MachineBasicBlock::~MachineBasicBlock() = default;
60 /// Return the MCSymbol for this basic block.
61 MCSymbol
*MachineBasicBlock::getSymbol() const {
62 if (!CachedMCSymbol
) {
63 const MachineFunction
*MF
= getParent();
64 MCContext
&Ctx
= MF
->getContext();
66 // We emit a non-temporary symbol -- with a descriptive name -- if it begins
67 // a section (with basic block sections). Otherwise we fall back to use temp
69 if (MF
->hasBBSections() && isBeginSection()) {
70 SmallString
<5> Suffix
;
71 if (SectionID
== MBBSectionID::ColdSectionID
) {
73 } else if (SectionID
== MBBSectionID::ExceptionSectionID
) {
76 // For symbols that represent basic block sections, we add ".__part." to
77 // allow tools like symbolizers to know that this represents a part of
78 // the original function.
79 Suffix
= (Suffix
+ Twine(".__part.") + Twine(SectionID
.Number
)).str();
81 CachedMCSymbol
= Ctx
.getOrCreateSymbol(MF
->getName() + Suffix
);
83 // If the block occurs as label in inline assembly, parsing the assembly
84 // needs an actual label name => set AlwaysEmit in these cases.
85 CachedMCSymbol
= Ctx
.createBlockSymbol(
86 "BB" + Twine(MF
->getFunctionNumber()) + "_" + Twine(getNumber()),
87 /*AlwaysEmit=*/hasLabelMustBeEmitted());
90 return CachedMCSymbol
;
93 MCSymbol
*MachineBasicBlock::getEHCatchretSymbol() const {
94 if (!CachedEHCatchretMCSymbol
) {
95 const MachineFunction
*MF
= getParent();
96 SmallString
<128> SymbolName
;
97 raw_svector_ostream(SymbolName
)
98 << "$ehgcr_" << MF
->getFunctionNumber() << '_' << getNumber();
99 CachedEHCatchretMCSymbol
= MF
->getContext().getOrCreateSymbol(SymbolName
);
101 return CachedEHCatchretMCSymbol
;
104 MCSymbol
*MachineBasicBlock::getEndSymbol() const {
105 if (!CachedEndMCSymbol
) {
106 const MachineFunction
*MF
= getParent();
107 MCContext
&Ctx
= MF
->getContext();
108 CachedEndMCSymbol
= Ctx
.createBlockSymbol(
109 "BB_END" + Twine(MF
->getFunctionNumber()) + "_" + Twine(getNumber()),
110 /*AlwaysEmit=*/false);
112 return CachedEndMCSymbol
;
115 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const MachineBasicBlock
&MBB
) {
120 Printable
llvm::printMBBReference(const MachineBasicBlock
&MBB
) {
121 return Printable([&MBB
](raw_ostream
&OS
) { return MBB
.printAsOperand(OS
); });
124 /// When an MBB is added to an MF, we need to update the parent pointer of the
125 /// MBB, the MBB numbering, and any instructions in the MBB to be on the right
126 /// operand list for registers.
128 /// MBBs start out as #-1. When a MBB is added to a MachineFunction, it
129 /// gets the next available unique MBB number. If it is removed from a
130 /// MachineFunction, it goes back to being #-1.
131 void ilist_callback_traits
<MachineBasicBlock
>::addNodeToList(
132 MachineBasicBlock
*N
) {
133 MachineFunction
&MF
= *N
->getParent();
134 N
->Number
= MF
.addToMBBNumbering(N
);
136 // Make sure the instructions have their operands in the reginfo lists.
137 MachineRegisterInfo
&RegInfo
= MF
.getRegInfo();
138 for (MachineInstr
&MI
: N
->instrs())
139 MI
.addRegOperandsToUseLists(RegInfo
);
142 void ilist_callback_traits
<MachineBasicBlock
>::removeNodeFromList(
143 MachineBasicBlock
*N
) {
144 N
->getParent()->removeFromMBBNumbering(N
->Number
);
148 /// When we add an instruction to a basic block list, we update its parent
149 /// pointer and add its operands from reg use/def lists if appropriate.
150 void ilist_traits
<MachineInstr
>::addNodeToList(MachineInstr
*N
) {
151 assert(!N
->getParent() && "machine instruction already in a basic block");
152 N
->setParent(Parent
);
154 // Add the instruction's register operands to their corresponding
156 MachineFunction
*MF
= Parent
->getParent();
157 N
->addRegOperandsToUseLists(MF
->getRegInfo());
158 MF
->handleInsertion(*N
);
161 /// When we remove an instruction from a basic block list, we update its parent
162 /// pointer and remove its operands from reg use/def lists if appropriate.
163 void ilist_traits
<MachineInstr
>::removeNodeFromList(MachineInstr
*N
) {
164 assert(N
->getParent() && "machine instruction not in a basic block");
166 // Remove from the use/def lists.
167 if (MachineFunction
*MF
= N
->getMF()) {
168 MF
->handleRemoval(*N
);
169 N
->removeRegOperandsFromUseLists(MF
->getRegInfo());
172 N
->setParent(nullptr);
175 /// When moving a range of instructions from one MBB list to another, we need to
176 /// update the parent pointers and the use/def lists.
177 void ilist_traits
<MachineInstr
>::transferNodesFromList(ilist_traits
&FromList
,
178 instr_iterator First
,
179 instr_iterator Last
) {
180 assert(Parent
->getParent() == FromList
.Parent
->getParent() &&
181 "cannot transfer MachineInstrs between MachineFunctions");
183 // If it's within the same BB, there's nothing to do.
184 if (this == &FromList
)
187 assert(Parent
!= FromList
.Parent
&& "Two lists have the same parent?");
189 // If splicing between two blocks within the same function, just update the
191 for (; First
!= Last
; ++First
)
192 First
->setParent(Parent
);
195 void ilist_traits
<MachineInstr
>::deleteNode(MachineInstr
*MI
) {
196 assert(!MI
->getParent() && "MI is still in a block!");
197 Parent
->getParent()->deleteMachineInstr(MI
);
200 MachineBasicBlock::iterator
MachineBasicBlock::getFirstNonPHI() {
201 instr_iterator I
= instr_begin(), E
= instr_end();
202 while (I
!= E
&& I
->isPHI())
204 assert((I
== E
|| !I
->isInsideBundle()) &&
205 "First non-phi MI cannot be inside a bundle!");
209 MachineBasicBlock::iterator
210 MachineBasicBlock::SkipPHIsAndLabels(MachineBasicBlock::iterator I
) {
211 const TargetInstrInfo
*TII
= getParent()->getSubtarget().getInstrInfo();
214 while (I
!= E
&& (I
->isPHI() || I
->isPosition() ||
215 TII
->isBasicBlockPrologue(*I
)))
217 // FIXME: This needs to change if we wish to bundle labels
218 // inside the bundle.
219 assert((I
== E
|| !I
->isInsideBundle()) &&
220 "First non-phi / non-label instruction is inside a bundle!");
224 MachineBasicBlock::iterator
225 MachineBasicBlock::SkipPHIsLabelsAndDebug(MachineBasicBlock::iterator I
,
226 Register Reg
, bool SkipPseudoOp
) {
227 const TargetInstrInfo
*TII
= getParent()->getSubtarget().getInstrInfo();
230 while (I
!= E
&& (I
->isPHI() || I
->isPosition() || I
->isDebugInstr() ||
231 (SkipPseudoOp
&& I
->isPseudoProbe()) ||
232 TII
->isBasicBlockPrologue(*I
, Reg
)))
234 // FIXME: This needs to change if we wish to bundle labels / dbg_values
235 // inside the bundle.
236 assert((I
== E
|| !I
->isInsideBundle()) &&
237 "First non-phi / non-label / non-debug "
238 "instruction is inside a bundle!");
242 MachineBasicBlock::iterator
MachineBasicBlock::getFirstTerminator() {
243 iterator B
= begin(), E
= end(), I
= E
;
244 while (I
!= B
&& ((--I
)->isTerminator() || I
->isDebugInstr()))
246 while (I
!= E
&& !I
->isTerminator())
251 MachineBasicBlock::instr_iterator
MachineBasicBlock::getFirstInstrTerminator() {
252 instr_iterator B
= instr_begin(), E
= instr_end(), I
= E
;
253 while (I
!= B
&& ((--I
)->isTerminator() || I
->isDebugInstr()))
255 while (I
!= E
&& !I
->isTerminator())
260 MachineBasicBlock::iterator
MachineBasicBlock::getFirstTerminatorForward() {
261 return find_if(instrs(), [](auto &II
) { return II
.isTerminator(); });
264 MachineBasicBlock::iterator
265 MachineBasicBlock::getFirstNonDebugInstr(bool SkipPseudoOp
) {
266 // Skip over begin-of-block dbg_value instructions.
267 return skipDebugInstructionsForward(begin(), end(), SkipPseudoOp
);
270 MachineBasicBlock::iterator
271 MachineBasicBlock::getLastNonDebugInstr(bool SkipPseudoOp
) {
272 // Skip over end-of-block dbg_value instructions.
273 instr_iterator B
= instr_begin(), I
= instr_end();
276 // Return instruction that starts a bundle.
277 if (I
->isDebugInstr() || I
->isInsideBundle())
279 if (SkipPseudoOp
&& I
->isPseudoProbe())
283 // The block is all debug values.
287 bool MachineBasicBlock::hasEHPadSuccessor() const {
288 for (const MachineBasicBlock
*Succ
: successors())
294 bool MachineBasicBlock::isEntryBlock() const {
295 return getParent()->begin() == getIterator();
298 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
299 LLVM_DUMP_METHOD
void MachineBasicBlock::dump() const {
304 bool MachineBasicBlock::mayHaveInlineAsmBr() const {
305 for (const MachineBasicBlock
*Succ
: successors()) {
306 if (Succ
->isInlineAsmBrIndirectTarget())
312 bool MachineBasicBlock::isLegalToHoistInto() const {
313 if (isReturnBlock() || hasEHPadSuccessor() || mayHaveInlineAsmBr())
318 bool MachineBasicBlock::hasName() const {
319 if (const BasicBlock
*LBB
= getBasicBlock())
320 return LBB
->hasName();
324 StringRef
MachineBasicBlock::getName() const {
325 if (const BasicBlock
*LBB
= getBasicBlock())
326 return LBB
->getName();
328 return StringRef("", 0);
331 /// Return a hopefully unique identifier for this block.
332 std::string
MachineBasicBlock::getFullName() const {
335 Name
= (getParent()->getName() + ":").str();
337 Name
+= getBasicBlock()->getName();
339 Name
+= ("BB" + Twine(getNumber())).str();
343 void MachineBasicBlock::print(raw_ostream
&OS
, const SlotIndexes
*Indexes
,
344 bool IsStandalone
) const {
345 const MachineFunction
*MF
= getParent();
347 OS
<< "Can't print out MachineBasicBlock because parent MachineFunction"
351 const Function
&F
= MF
->getFunction();
352 const Module
*M
= F
.getParent();
353 ModuleSlotTracker
MST(M
);
354 MST
.incorporateFunction(F
);
355 print(OS
, MST
, Indexes
, IsStandalone
);
358 void MachineBasicBlock::print(raw_ostream
&OS
, ModuleSlotTracker
&MST
,
359 const SlotIndexes
*Indexes
,
360 bool IsStandalone
) const {
361 const MachineFunction
*MF
= getParent();
363 OS
<< "Can't print out MachineBasicBlock because parent MachineFunction"
368 if (Indexes
&& PrintSlotIndexes
)
369 OS
<< Indexes
->getMBBStartIdx(this) << '\t';
371 printName(OS
, PrintNameIr
| PrintNameAttributes
, &MST
);
374 const TargetRegisterInfo
*TRI
= MF
->getSubtarget().getRegisterInfo();
375 const MachineRegisterInfo
&MRI
= MF
->getRegInfo();
376 const TargetInstrInfo
&TII
= *getParent()->getSubtarget().getInstrInfo();
377 bool HasLineAttributes
= false;
379 // Print the preds of this block according to the CFG.
380 if (!pred_empty() && IsStandalone
) {
381 if (Indexes
) OS
<< '\t';
382 // Don't indent(2), align with previous line attributes.
383 OS
<< "; predecessors: ";
385 for (auto *Pred
: predecessors())
386 OS
<< LS
<< printMBBReference(*Pred
);
388 HasLineAttributes
= true;
392 if (Indexes
) OS
<< '\t';
393 // Print the successors
394 OS
.indent(2) << "successors: ";
396 for (auto I
= succ_begin(), E
= succ_end(); I
!= E
; ++I
) {
397 OS
<< LS
<< printMBBReference(**I
);
400 << format("0x%08" PRIx32
, getSuccProbability(I
).getNumerator())
403 if (!Probs
.empty() && IsStandalone
) {
404 // Print human readable probabilities as comments.
407 for (auto I
= succ_begin(), E
= succ_end(); I
!= E
; ++I
) {
408 const BranchProbability
&BP
= getSuccProbability(I
);
409 OS
<< LS
<< printMBBReference(**I
) << '('
411 rint(((double)BP
.getNumerator() / BP
.getDenominator()) *
419 HasLineAttributes
= true;
422 if (!livein_empty() && MRI
.tracksLiveness()) {
423 if (Indexes
) OS
<< '\t';
424 OS
.indent(2) << "liveins: ";
427 for (const auto &LI
: liveins()) {
428 OS
<< LS
<< printReg(LI
.PhysReg
, TRI
);
429 if (!LI
.LaneMask
.all())
430 OS
<< ":0x" << PrintLaneMask(LI
.LaneMask
);
432 HasLineAttributes
= true;
435 if (HasLineAttributes
)
438 bool IsInBundle
= false;
439 for (const MachineInstr
&MI
: instrs()) {
440 if (Indexes
&& PrintSlotIndexes
) {
441 if (Indexes
->hasIndex(MI
))
442 OS
<< Indexes
->getInstructionIndex(MI
);
446 if (IsInBundle
&& !MI
.isInsideBundle()) {
447 OS
.indent(2) << "}\n";
451 OS
.indent(IsInBundle
? 4 : 2);
452 MI
.print(OS
, MST
, IsStandalone
, /*SkipOpers=*/false, /*SkipDebugLoc=*/false,
453 /*AddNewLine=*/false, &TII
);
455 if (!IsInBundle
&& MI
.getFlag(MachineInstr::BundledSucc
)) {
463 OS
.indent(2) << "}\n";
465 if (IrrLoopHeaderWeight
&& IsStandalone
) {
466 if (Indexes
) OS
<< '\t';
467 OS
.indent(2) << "; Irreducible loop header weight: " << *IrrLoopHeaderWeight
472 /// Print the basic block's name as:
474 /// bb.{number}[.{ir-name}] [(attributes...)]
476 /// The {ir-name} is only printed when the \ref PrintNameIr flag is passed
477 /// (which is the default). If the IR block has no name, it is identified
478 /// numerically using the attribute syntax as "(%ir-block.{ir-slot})".
480 /// When the \ref PrintNameAttributes flag is passed, additional attributes
481 /// of the block are printed when set.
483 /// \param printNameFlags Combination of \ref PrintNameFlag flags indicating
484 /// the parts to print.
485 /// \param moduleSlotTracker Optional ModuleSlotTracker. This method will
486 /// incorporate its own tracker when necessary to
487 /// determine the block's IR name.
488 void MachineBasicBlock::printName(raw_ostream
&os
, unsigned printNameFlags
,
489 ModuleSlotTracker
*moduleSlotTracker
) const {
490 os
<< "bb." << getNumber();
491 bool hasAttributes
= false;
493 auto PrintBBRef
= [&](const BasicBlock
*bb
) {
500 if (moduleSlotTracker
) {
501 slot
= moduleSlotTracker
->getLocalSlot(bb
);
502 } else if (bb
->getParent()) {
503 ModuleSlotTracker
tmpTracker(bb
->getModule(), false);
504 tmpTracker
.incorporateFunction(*bb
->getParent());
505 slot
= tmpTracker
.getLocalSlot(bb
);
509 os
<< "<ir-block badref>";
515 if (printNameFlags
& PrintNameIr
) {
516 if (const auto *bb
= getBasicBlock()) {
518 os
<< '.' << bb
->getName();
520 hasAttributes
= true;
527 if (printNameFlags
& PrintNameAttributes
) {
528 if (isMachineBlockAddressTaken()) {
529 os
<< (hasAttributes
? ", " : " (");
530 os
<< "machine-block-address-taken";
531 hasAttributes
= true;
533 if (isIRBlockAddressTaken()) {
534 os
<< (hasAttributes
? ", " : " (");
535 os
<< "ir-block-address-taken ";
536 PrintBBRef(getAddressTakenIRBlock());
537 hasAttributes
= true;
540 os
<< (hasAttributes
? ", " : " (");
542 hasAttributes
= true;
544 if (isInlineAsmBrIndirectTarget()) {
545 os
<< (hasAttributes
? ", " : " (");
546 os
<< "inlineasm-br-indirect-target";
547 hasAttributes
= true;
549 if (isEHFuncletEntry()) {
550 os
<< (hasAttributes
? ", " : " (");
551 os
<< "ehfunclet-entry";
552 hasAttributes
= true;
554 if (getAlignment() != Align(1)) {
555 os
<< (hasAttributes
? ", " : " (");
556 os
<< "align " << getAlignment().value();
557 hasAttributes
= true;
559 if (getSectionID() != MBBSectionID(0)) {
560 os
<< (hasAttributes
? ", " : " (");
562 switch (getSectionID().Type
) {
563 case MBBSectionID::SectionType::Exception
:
566 case MBBSectionID::SectionType::Cold
:
570 os
<< getSectionID().Number
;
572 hasAttributes
= true;
574 if (getBBID().has_value()) {
575 os
<< (hasAttributes
? ", " : " (");
576 os
<< "bb_id " << getBBID()->BaseID
;
577 if (getBBID()->CloneID
!= 0)
578 os
<< " " << getBBID()->CloneID
;
579 hasAttributes
= true;
581 if (CallFrameSize
!= 0) {
582 os
<< (hasAttributes
? ", " : " (");
583 os
<< "call-frame-size " << CallFrameSize
;
584 hasAttributes
= true;
592 void MachineBasicBlock::printAsOperand(raw_ostream
&OS
,
593 bool /*PrintType*/) const {
598 void MachineBasicBlock::removeLiveIn(MCPhysReg Reg
, LaneBitmask LaneMask
) {
599 LiveInVector::iterator I
= find_if(
600 LiveIns
, [Reg
](const RegisterMaskPair
&LI
) { return LI
.PhysReg
== Reg
; });
601 if (I
== LiveIns
.end())
604 I
->LaneMask
&= ~LaneMask
;
605 if (I
->LaneMask
.none())
609 MachineBasicBlock::livein_iterator
610 MachineBasicBlock::removeLiveIn(MachineBasicBlock::livein_iterator I
) {
611 // Get non-const version of iterator.
612 LiveInVector::iterator LI
= LiveIns
.begin() + (I
- LiveIns
.begin());
613 return LiveIns
.erase(LI
);
616 bool MachineBasicBlock::isLiveIn(MCPhysReg Reg
, LaneBitmask LaneMask
) const {
617 livein_iterator I
= find_if(
618 LiveIns
, [Reg
](const RegisterMaskPair
&LI
) { return LI
.PhysReg
== Reg
; });
619 return I
!= livein_end() && (I
->LaneMask
& LaneMask
).any();
622 void MachineBasicBlock::sortUniqueLiveIns() {
624 [](const RegisterMaskPair
&LI0
, const RegisterMaskPair
&LI1
) {
625 return LI0
.PhysReg
< LI1
.PhysReg
;
627 // Liveins are sorted by physreg now we can merge their lanemasks.
628 LiveInVector::const_iterator I
= LiveIns
.begin();
629 LiveInVector::const_iterator J
;
630 LiveInVector::iterator Out
= LiveIns
.begin();
631 for (; I
!= LiveIns
.end(); ++Out
, I
= J
) {
632 MCRegister PhysReg
= I
->PhysReg
;
633 LaneBitmask LaneMask
= I
->LaneMask
;
634 for (J
= std::next(I
); J
!= LiveIns
.end() && J
->PhysReg
== PhysReg
; ++J
)
635 LaneMask
|= J
->LaneMask
;
636 Out
->PhysReg
= PhysReg
;
637 Out
->LaneMask
= LaneMask
;
639 LiveIns
.erase(Out
, LiveIns
.end());
643 MachineBasicBlock::addLiveIn(MCRegister PhysReg
, const TargetRegisterClass
*RC
) {
644 assert(getParent() && "MBB must be inserted in function");
645 assert(Register::isPhysicalRegister(PhysReg
) && "Expected physreg");
646 assert(RC
&& "Register class is required");
647 assert((isEHPad() || this == &getParent()->front()) &&
648 "Only the entry block and landing pads can have physreg live ins");
650 bool LiveIn
= isLiveIn(PhysReg
);
651 iterator I
= SkipPHIsAndLabels(begin()), E
= end();
652 MachineRegisterInfo
&MRI
= getParent()->getRegInfo();
653 const TargetInstrInfo
&TII
= *getParent()->getSubtarget().getInstrInfo();
655 // Look for an existing copy.
657 for (;I
!= E
&& I
->isCopy(); ++I
)
658 if (I
->getOperand(1).getReg() == PhysReg
) {
659 Register VirtReg
= I
->getOperand(0).getReg();
660 if (!MRI
.constrainRegClass(VirtReg
, RC
))
661 llvm_unreachable("Incompatible live-in register class.");
665 // No luck, create a virtual register.
666 Register VirtReg
= MRI
.createVirtualRegister(RC
);
667 BuildMI(*this, I
, DebugLoc(), TII
.get(TargetOpcode::COPY
), VirtReg
)
668 .addReg(PhysReg
, RegState::Kill
);
674 void MachineBasicBlock::moveBefore(MachineBasicBlock
*NewAfter
) {
675 getParent()->splice(NewAfter
->getIterator(), getIterator());
678 void MachineBasicBlock::moveAfter(MachineBasicBlock
*NewBefore
) {
679 getParent()->splice(++NewBefore
->getIterator(), getIterator());
682 static int findJumpTableIndex(const MachineBasicBlock
&MBB
) {
683 MachineBasicBlock::const_iterator TerminatorI
= MBB
.getFirstTerminator();
684 if (TerminatorI
== MBB
.end())
686 const MachineInstr
&Terminator
= *TerminatorI
;
687 const TargetInstrInfo
*TII
= MBB
.getParent()->getSubtarget().getInstrInfo();
688 return TII
->getJumpTableIndex(Terminator
);
691 void MachineBasicBlock::updateTerminator(
692 MachineBasicBlock
*PreviousLayoutSuccessor
) {
693 LLVM_DEBUG(dbgs() << "Updating terminators on " << printMBBReference(*this)
696 const TargetInstrInfo
*TII
= getParent()->getSubtarget().getInstrInfo();
697 // A block with no successors has no concerns with fall-through edges.
698 if (this->succ_empty())
701 MachineBasicBlock
*TBB
= nullptr, *FBB
= nullptr;
702 SmallVector
<MachineOperand
, 4> Cond
;
703 DebugLoc DL
= findBranchDebugLoc();
704 bool B
= TII
->analyzeBranch(*this, TBB
, FBB
, Cond
);
706 assert(!B
&& "UpdateTerminators requires analyzable predecessors!");
709 // The block has an unconditional branch. If its successor is now its
710 // layout successor, delete the branch.
711 if (isLayoutSuccessor(TBB
))
712 TII
->removeBranch(*this);
714 // The block has an unconditional fallthrough, or the end of the block is
717 // Unfortunately, whether the end of the block is unreachable is not
718 // immediately obvious; we must fall back to checking the successor list,
719 // and assuming that if the passed in block is in the succesor list and
720 // not an EHPad, it must be the intended target.
721 if (!PreviousLayoutSuccessor
|| !isSuccessor(PreviousLayoutSuccessor
) ||
722 PreviousLayoutSuccessor
->isEHPad())
725 // If the unconditional successor block is not the current layout
726 // successor, insert a branch to jump to it.
727 if (!isLayoutSuccessor(PreviousLayoutSuccessor
))
728 TII
->insertBranch(*this, PreviousLayoutSuccessor
, nullptr, Cond
, DL
);
734 // The block has a non-fallthrough conditional branch. If one of its
735 // successors is its layout successor, rewrite it to a fallthrough
736 // conditional branch.
737 if (isLayoutSuccessor(TBB
)) {
738 if (TII
->reverseBranchCondition(Cond
))
740 TII
->removeBranch(*this);
741 TII
->insertBranch(*this, FBB
, nullptr, Cond
, DL
);
742 } else if (isLayoutSuccessor(FBB
)) {
743 TII
->removeBranch(*this);
744 TII
->insertBranch(*this, TBB
, nullptr, Cond
, DL
);
749 // We now know we're going to fallthrough to PreviousLayoutSuccessor.
750 assert(PreviousLayoutSuccessor
);
751 assert(!PreviousLayoutSuccessor
->isEHPad());
752 assert(isSuccessor(PreviousLayoutSuccessor
));
754 if (PreviousLayoutSuccessor
== TBB
) {
755 // We had a fallthrough to the same basic block as the conditional jump
756 // targets. Remove the conditional jump, leaving an unconditional
757 // fallthrough or an unconditional jump.
758 TII
->removeBranch(*this);
759 if (!isLayoutSuccessor(TBB
)) {
761 TII
->insertBranch(*this, TBB
, nullptr, Cond
, DL
);
766 // The block has a fallthrough conditional branch.
767 if (isLayoutSuccessor(TBB
)) {
768 if (TII
->reverseBranchCondition(Cond
)) {
769 // We can't reverse the condition, add an unconditional branch.
771 TII
->insertBranch(*this, PreviousLayoutSuccessor
, nullptr, Cond
, DL
);
774 TII
->removeBranch(*this);
775 TII
->insertBranch(*this, PreviousLayoutSuccessor
, nullptr, Cond
, DL
);
776 } else if (!isLayoutSuccessor(PreviousLayoutSuccessor
)) {
777 TII
->removeBranch(*this);
778 TII
->insertBranch(*this, TBB
, PreviousLayoutSuccessor
, Cond
, DL
);
782 void MachineBasicBlock::validateSuccProbs() const {
785 for (auto Prob
: Probs
)
786 Sum
+= Prob
.getNumerator();
787 // Due to precision issue, we assume that the sum of probabilities is one if
788 // the difference between the sum of their numerators and the denominator is
789 // no greater than the number of successors.
790 assert((uint64_t)std::abs(Sum
- BranchProbability::getDenominator()) <=
792 "The sum of successors's probabilities exceeds one.");
796 void MachineBasicBlock::addSuccessor(MachineBasicBlock
*Succ
,
797 BranchProbability Prob
) {
798 // Probability list is either empty (if successor list isn't empty, this means
799 // disabled optimization) or has the same size as successor list.
800 if (!(Probs
.empty() && !Successors
.empty()))
801 Probs
.push_back(Prob
);
802 Successors
.push_back(Succ
);
803 Succ
->addPredecessor(this);
806 void MachineBasicBlock::addSuccessorWithoutProb(MachineBasicBlock
*Succ
) {
807 // We need to make sure probability list is either empty or has the same size
808 // of successor list. When this function is called, we can safely delete all
809 // probability in the list.
811 Successors
.push_back(Succ
);
812 Succ
->addPredecessor(this);
815 void MachineBasicBlock::splitSuccessor(MachineBasicBlock
*Old
,
816 MachineBasicBlock
*New
,
817 bool NormalizeSuccProbs
) {
818 succ_iterator OldI
= llvm::find(successors(), Old
);
819 assert(OldI
!= succ_end() && "Old is not a successor of this block!");
820 assert(!llvm::is_contained(successors(), New
) &&
821 "New is already a successor of this block!");
823 // Add a new successor with equal probability as the original one. Note
824 // that we directly copy the probability using the iterator rather than
825 // getting a potentially synthetic probability computed when unknown. This
826 // preserves the probabilities as-is and then we can renormalize them and
827 // query them effectively afterward.
828 addSuccessor(New
, Probs
.empty() ? BranchProbability::getUnknown()
829 : *getProbabilityIterator(OldI
));
830 if (NormalizeSuccProbs
)
831 normalizeSuccProbs();
834 void MachineBasicBlock::removeSuccessor(MachineBasicBlock
*Succ
,
835 bool NormalizeSuccProbs
) {
836 succ_iterator I
= find(Successors
, Succ
);
837 removeSuccessor(I
, NormalizeSuccProbs
);
840 MachineBasicBlock::succ_iterator
841 MachineBasicBlock::removeSuccessor(succ_iterator I
, bool NormalizeSuccProbs
) {
842 assert(I
!= Successors
.end() && "Not a current successor!");
844 // If probability list is empty it means we don't use it (disabled
846 if (!Probs
.empty()) {
847 probability_iterator WI
= getProbabilityIterator(I
);
849 if (NormalizeSuccProbs
)
850 normalizeSuccProbs();
853 (*I
)->removePredecessor(this);
854 return Successors
.erase(I
);
857 void MachineBasicBlock::replaceSuccessor(MachineBasicBlock
*Old
,
858 MachineBasicBlock
*New
) {
862 succ_iterator E
= succ_end();
863 succ_iterator NewI
= E
;
864 succ_iterator OldI
= E
;
865 for (succ_iterator I
= succ_begin(); I
!= E
; ++I
) {
877 assert(OldI
!= E
&& "Old is not a successor of this block");
879 // If New isn't already a successor, let it take Old's place.
881 Old
->removePredecessor(this);
882 New
->addPredecessor(this);
887 // New is already a successor.
888 // Update its probability instead of adding a duplicate edge.
889 if (!Probs
.empty()) {
890 auto ProbIter
= getProbabilityIterator(NewI
);
891 if (!ProbIter
->isUnknown())
892 *ProbIter
+= *getProbabilityIterator(OldI
);
894 removeSuccessor(OldI
);
897 void MachineBasicBlock::copySuccessor(const MachineBasicBlock
*Orig
,
899 if (!Orig
->Probs
.empty())
900 addSuccessor(*I
, Orig
->getSuccProbability(I
));
902 addSuccessorWithoutProb(*I
);
905 void MachineBasicBlock::addPredecessor(MachineBasicBlock
*Pred
) {
906 Predecessors
.push_back(Pred
);
909 void MachineBasicBlock::removePredecessor(MachineBasicBlock
*Pred
) {
910 pred_iterator I
= find(Predecessors
, Pred
);
911 assert(I
!= Predecessors
.end() && "Pred is not a predecessor of this block!");
912 Predecessors
.erase(I
);
915 void MachineBasicBlock::transferSuccessors(MachineBasicBlock
*FromMBB
) {
919 while (!FromMBB
->succ_empty()) {
920 MachineBasicBlock
*Succ
= *FromMBB
->succ_begin();
922 // If probability list is empty it means we don't use it (disabled
924 if (!FromMBB
->Probs
.empty()) {
925 auto Prob
= *FromMBB
->Probs
.begin();
926 addSuccessor(Succ
, Prob
);
928 addSuccessorWithoutProb(Succ
);
930 FromMBB
->removeSuccessor(Succ
);
935 MachineBasicBlock::transferSuccessorsAndUpdatePHIs(MachineBasicBlock
*FromMBB
) {
939 while (!FromMBB
->succ_empty()) {
940 MachineBasicBlock
*Succ
= *FromMBB
->succ_begin();
941 if (!FromMBB
->Probs
.empty()) {
942 auto Prob
= *FromMBB
->Probs
.begin();
943 addSuccessor(Succ
, Prob
);
945 addSuccessorWithoutProb(Succ
);
946 FromMBB
->removeSuccessor(Succ
);
948 // Fix up any PHI nodes in the successor.
949 Succ
->replacePhiUsesWith(FromMBB
, this);
951 normalizeSuccProbs();
954 bool MachineBasicBlock::isPredecessor(const MachineBasicBlock
*MBB
) const {
955 return is_contained(predecessors(), MBB
);
958 bool MachineBasicBlock::isSuccessor(const MachineBasicBlock
*MBB
) const {
959 return is_contained(successors(), MBB
);
962 bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock
*MBB
) const {
963 MachineFunction::const_iterator
I(this);
964 return std::next(I
) == MachineFunction::const_iterator(MBB
);
967 const MachineBasicBlock
*MachineBasicBlock::getSingleSuccessor() const {
968 return Successors
.size() == 1 ? Successors
[0] : nullptr;
971 const MachineBasicBlock
*MachineBasicBlock::getSinglePredecessor() const {
972 return Predecessors
.size() == 1 ? Predecessors
[0] : nullptr;
975 MachineBasicBlock
*MachineBasicBlock::getFallThrough(bool JumpToFallThrough
) {
976 MachineFunction::iterator Fallthrough
= getIterator();
978 // If FallthroughBlock is off the end of the function, it can't fall through.
979 if (Fallthrough
== getParent()->end())
982 // If FallthroughBlock isn't a successor, no fallthrough is possible.
983 if (!isSuccessor(&*Fallthrough
))
986 // Analyze the branches, if any, at the end of the block.
987 MachineBasicBlock
*TBB
= nullptr, *FBB
= nullptr;
988 SmallVector
<MachineOperand
, 4> Cond
;
989 const TargetInstrInfo
*TII
= getParent()->getSubtarget().getInstrInfo();
990 if (TII
->analyzeBranch(*this, TBB
, FBB
, Cond
)) {
991 // If we couldn't analyze the branch, examine the last instruction.
992 // If the block doesn't end in a known control barrier, assume fallthrough
993 // is possible. The isPredicated check is needed because this code can be
994 // called during IfConversion, where an instruction which is normally a
995 // Barrier is predicated and thus no longer an actual control barrier.
996 return (empty() || !back().isBarrier() || TII
->isPredicated(back()))
1001 // If there is no branch, control always falls through.
1002 if (!TBB
) return &*Fallthrough
;
1004 // If there is some explicit branch to the fallthrough block, it can obviously
1005 // reach, even though the branch should get folded to fall through implicitly.
1006 if (JumpToFallThrough
&& (MachineFunction::iterator(TBB
) == Fallthrough
||
1007 MachineFunction::iterator(FBB
) == Fallthrough
))
1008 return &*Fallthrough
;
1010 // If it's an unconditional branch to some block not the fall through, it
1011 // doesn't fall through.
1012 if (Cond
.empty()) return nullptr;
1014 // Otherwise, if it is conditional and has no explicit false block, it falls
1016 return (FBB
== nullptr) ? &*Fallthrough
: nullptr;
1019 bool MachineBasicBlock::canFallThrough() {
1020 return getFallThrough() != nullptr;
1023 MachineBasicBlock
*MachineBasicBlock::splitAt(MachineInstr
&MI
,
1025 LiveIntervals
*LIS
) {
1026 MachineBasicBlock::iterator
SplitPoint(&MI
);
1029 if (SplitPoint
== end()) {
1030 // Don't bother with a new block.
1034 MachineFunction
*MF
= getParent();
1036 LivePhysRegs LiveRegs
;
1037 if (UpdateLiveIns
) {
1038 // Make sure we add any physregs we define in the block as liveins to the
1040 MachineBasicBlock::iterator
Prev(&MI
);
1041 LiveRegs
.init(*MF
->getSubtarget().getRegisterInfo());
1042 LiveRegs
.addLiveOuts(*this);
1043 for (auto I
= rbegin(), E
= Prev
.getReverse(); I
!= E
; ++I
)
1044 LiveRegs
.stepBackward(*I
);
1047 MachineBasicBlock
*SplitBB
= MF
->CreateMachineBasicBlock(getBasicBlock());
1049 MF
->insert(++MachineFunction::iterator(this), SplitBB
);
1050 SplitBB
->splice(SplitBB
->begin(), this, SplitPoint
, end());
1052 SplitBB
->transferSuccessorsAndUpdatePHIs(this);
1053 addSuccessor(SplitBB
);
1056 addLiveIns(*SplitBB
, LiveRegs
);
1059 LIS
->insertMBBInMaps(SplitBB
);
1064 // Returns `true` if there are possibly other users of the jump table at
1065 // `JumpTableIndex` except for the ones in `IgnoreMBB`.
1066 static bool jumpTableHasOtherUses(const MachineFunction
&MF
,
1067 const MachineBasicBlock
&IgnoreMBB
,
1068 int JumpTableIndex
) {
1069 assert(JumpTableIndex
>= 0 && "need valid index");
1070 const MachineJumpTableInfo
&MJTI
= *MF
.getJumpTableInfo();
1071 const MachineJumpTableEntry
&MJTE
= MJTI
.getJumpTables()[JumpTableIndex
];
1072 // Take any basic block from the table; every user of the jump table must
1073 // show up in the predecessor list.
1074 const MachineBasicBlock
*MBB
= nullptr;
1075 for (MachineBasicBlock
*B
: MJTE
.MBBs
) {
1082 return true; // can't rule out other users if there isn't any block.
1083 const TargetInstrInfo
&TII
= *MF
.getSubtarget().getInstrInfo();
1084 SmallVector
<MachineOperand
, 4> Cond
;
1085 for (MachineBasicBlock
*Pred
: MBB
->predecessors()) {
1086 if (Pred
== &IgnoreMBB
)
1088 MachineBasicBlock
*DummyT
= nullptr;
1089 MachineBasicBlock
*DummyF
= nullptr;
1091 if (!TII
.analyzeBranch(*Pred
, DummyT
, DummyF
, Cond
,
1092 /*AllowModify=*/false)) {
1093 // analyzable direct jump
1096 int PredJTI
= findJumpTableIndex(*Pred
);
1098 if (PredJTI
== JumpTableIndex
)
1102 // Be conservative for unanalyzable jumps.
1108 class SlotIndexUpdateDelegate
: public MachineFunction::Delegate
{
1110 MachineFunction
&MF
;
1111 SlotIndexes
*Indexes
;
1112 SmallSetVector
<MachineInstr
*, 2> Insertions
;
1115 SlotIndexUpdateDelegate(MachineFunction
&MF
, SlotIndexes
*Indexes
)
1116 : MF(MF
), Indexes(Indexes
) {
1117 MF
.setDelegate(this);
1120 ~SlotIndexUpdateDelegate() {
1121 MF
.resetDelegate(this);
1122 for (auto MI
: Insertions
)
1123 Indexes
->insertMachineInstrInMaps(*MI
);
1126 void MF_HandleInsertion(MachineInstr
&MI
) override
{
1127 // This is called before MI is inserted into block so defer index update.
1129 Insertions
.insert(&MI
);
1132 void MF_HandleRemoval(MachineInstr
&MI
) override
{
1133 if (Indexes
&& !Insertions
.remove(&MI
))
1134 Indexes
->removeMachineInstrFromMaps(MI
);
1138 #define GET_RESULT(RESULT, GETTER, INFIX) \
1141 auto *Wrapper = P->getAnalysisIfAvailable<RESULT##INFIX##WrapperPass>(); \
1142 return Wrapper ? &Wrapper->GETTER() : nullptr; \
1144 return MFAM->getCachedResult<RESULT##Analysis>(*MF); \
1147 MachineBasicBlock
*MachineBasicBlock::SplitCriticalEdge(
1148 MachineBasicBlock
*Succ
, Pass
*P
, MachineFunctionAnalysisManager
*MFAM
,
1149 std::vector
<SparseBitVector
<>> *LiveInSets
) {
1150 assert((P
|| MFAM
) && "Need a way to get analysis results!");
1151 if (!canSplitCriticalEdge(Succ
))
1154 MachineFunction
*MF
= getParent();
1155 MachineBasicBlock
*PrevFallthrough
= getNextNode();
1157 MachineBasicBlock
*NMBB
= MF
->CreateMachineBasicBlock();
1158 NMBB
->setCallFrameSize(Succ
->getCallFrameSize());
1160 // Is there an indirect jump with jump table?
1161 bool ChangedIndirectJump
= false;
1162 int JTI
= findJumpTableIndex(*this);
1164 MachineJumpTableInfo
&MJTI
= *MF
->getJumpTableInfo();
1165 MJTI
.ReplaceMBBInJumpTable(JTI
, Succ
, NMBB
);
1166 ChangedIndirectJump
= true;
1169 MF
->insert(std::next(MachineFunction::iterator(this)), NMBB
);
1170 LLVM_DEBUG(dbgs() << "Splitting critical edge: " << printMBBReference(*this)
1171 << " -- " << printMBBReference(*NMBB
) << " -- "
1172 << printMBBReference(*Succ
) << '\n');
1174 LiveIntervals
*LIS
= GET_RESULT(LiveIntervals
, getLIS
, );
1175 SlotIndexes
*Indexes
= GET_RESULT(SlotIndexes
, getSI
, );
1177 LIS
->insertMBBInMaps(NMBB
);
1179 Indexes
->insertMBBInMaps(NMBB
);
1181 // On some targets like Mips, branches may kill virtual registers. Make sure
1182 // that LiveVariables is properly updated after updateTerminator replaces the
1184 LiveVariables
*LV
= GET_RESULT(LiveVariables
, getLV
, );
1186 // Collect a list of virtual registers killed by the terminators.
1187 SmallVector
<Register
, 4> KilledRegs
;
1189 for (MachineInstr
&MI
:
1190 llvm::make_range(getFirstInstrTerminator(), instr_end())) {
1191 for (MachineOperand
&MO
: MI
.all_uses()) {
1192 if (MO
.getReg() == 0 || !MO
.isKill() || MO
.isUndef())
1194 Register Reg
= MO
.getReg();
1195 if (Reg
.isPhysical() || LV
->getVarInfo(Reg
).removeKill(MI
)) {
1196 KilledRegs
.push_back(Reg
);
1197 LLVM_DEBUG(dbgs() << "Removing terminator kill: " << MI
);
1198 MO
.setIsKill(false);
1203 SmallVector
<Register
, 4> UsedRegs
;
1205 for (MachineInstr
&MI
:
1206 llvm::make_range(getFirstInstrTerminator(), instr_end())) {
1207 for (const MachineOperand
&MO
: MI
.operands()) {
1208 if (!MO
.isReg() || MO
.getReg() == 0)
1211 Register Reg
= MO
.getReg();
1212 if (!is_contained(UsedRegs
, Reg
))
1213 UsedRegs
.push_back(Reg
);
1218 ReplaceUsesOfBlockWith(Succ
, NMBB
);
1220 // Since we replaced all uses of Succ with NMBB, that should also be treated
1221 // as the fallthrough successor
1222 if (Succ
== PrevFallthrough
)
1223 PrevFallthrough
= NMBB
;
1225 if (!ChangedIndirectJump
) {
1226 SlotIndexUpdateDelegate
SlotUpdater(*MF
, Indexes
);
1227 updateTerminator(PrevFallthrough
);
1230 // Insert unconditional "jump Succ" instruction in NMBB if necessary.
1231 NMBB
->addSuccessor(Succ
);
1232 if (!NMBB
->isLayoutSuccessor(Succ
)) {
1233 SlotIndexUpdateDelegate
SlotUpdater(*MF
, Indexes
);
1234 SmallVector
<MachineOperand
, 4> Cond
;
1235 const TargetInstrInfo
*TII
= getParent()->getSubtarget().getInstrInfo();
1237 // In original 'this' BB, there must be a branch instruction targeting at
1238 // Succ. We can not find it out since currently getBranchDestBlock was not
1239 // implemented for all targets. However, if the merged DL has column or line
1240 // number, the scope and non-zero column and line number is same with that
1241 // branch instruction so we can safely use it.
1242 DebugLoc DL
, MergedDL
= findBranchDebugLoc();
1243 if (MergedDL
&& (MergedDL
.getLine() || MergedDL
.getCol()))
1245 TII
->insertBranch(*NMBB
, Succ
, nullptr, Cond
, DL
);
1248 // Fix PHI nodes in Succ so they refer to NMBB instead of this.
1249 Succ
->replacePhiUsesWith(this, NMBB
);
1251 // Inherit live-ins from the successor
1252 for (const auto &LI
: Succ
->liveins())
1253 NMBB
->addLiveIn(LI
);
1255 // Update LiveVariables.
1256 const TargetRegisterInfo
*TRI
= MF
->getSubtarget().getRegisterInfo();
1258 // Restore kills of virtual registers that were killed by the terminators.
1259 while (!KilledRegs
.empty()) {
1260 Register Reg
= KilledRegs
.pop_back_val();
1261 for (instr_iterator I
= instr_end(), E
= instr_begin(); I
!= E
;) {
1262 if (!(--I
)->addRegisterKilled(Reg
, TRI
, /* AddIfNotFound= */ false))
1264 if (Reg
.isVirtual())
1265 LV
->getVarInfo(Reg
).Kills
.push_back(&*I
);
1266 LLVM_DEBUG(dbgs() << "Restored terminator kill: " << *I
);
1270 // Update relevant live-through information.
1271 if (LiveInSets
!= nullptr)
1272 LV
->addNewBlock(NMBB
, this, Succ
, *LiveInSets
);
1274 LV
->addNewBlock(NMBB
, this, Succ
);
1278 // After splitting the edge and updating SlotIndexes, live intervals may be
1279 // in one of two situations, depending on whether this block was the last in
1280 // the function. If the original block was the last in the function, all
1281 // live intervals will end prior to the beginning of the new split block. If
1282 // the original block was not at the end of the function, all live intervals
1283 // will extend to the end of the new split block.
1286 std::next(MachineFunction::iterator(NMBB
)) == getParent()->end();
1288 SlotIndex StartIndex
= Indexes
->getMBBEndIdx(this);
1289 SlotIndex PrevIndex
= StartIndex
.getPrevSlot();
1290 SlotIndex EndIndex
= Indexes
->getMBBEndIdx(NMBB
);
1292 // Find the registers used from NMBB in PHIs in Succ.
1293 SmallSet
<Register
, 8> PHISrcRegs
;
1294 for (MachineBasicBlock::instr_iterator
1295 I
= Succ
->instr_begin(), E
= Succ
->instr_end();
1296 I
!= E
&& I
->isPHI(); ++I
) {
1297 for (unsigned ni
= 1, ne
= I
->getNumOperands(); ni
!= ne
; ni
+= 2) {
1298 if (I
->getOperand(ni
+1).getMBB() == NMBB
) {
1299 MachineOperand
&MO
= I
->getOperand(ni
);
1300 Register Reg
= MO
.getReg();
1301 PHISrcRegs
.insert(Reg
);
1305 LiveInterval
&LI
= LIS
->getInterval(Reg
);
1306 VNInfo
*VNI
= LI
.getVNInfoAt(PrevIndex
);
1308 "PHI sources should be live out of their predecessors.");
1309 LI
.addSegment(LiveInterval::Segment(StartIndex
, EndIndex
, VNI
));
1310 for (auto &SR
: LI
.subranges())
1311 SR
.addSegment(LiveInterval::Segment(StartIndex
, EndIndex
, VNI
));
1316 MachineRegisterInfo
*MRI
= &getParent()->getRegInfo();
1317 for (unsigned i
= 0, e
= MRI
->getNumVirtRegs(); i
!= e
; ++i
) {
1318 Register Reg
= Register::index2VirtReg(i
);
1319 if (PHISrcRegs
.count(Reg
) || !LIS
->hasInterval(Reg
))
1322 LiveInterval
&LI
= LIS
->getInterval(Reg
);
1323 if (!LI
.liveAt(PrevIndex
))
1326 bool isLiveOut
= LI
.liveAt(LIS
->getMBBStartIdx(Succ
));
1327 if (isLiveOut
&& isLastMBB
) {
1328 VNInfo
*VNI
= LI
.getVNInfoAt(PrevIndex
);
1329 assert(VNI
&& "LiveInterval should have VNInfo where it is live.");
1330 LI
.addSegment(LiveInterval::Segment(StartIndex
, EndIndex
, VNI
));
1331 // Update subranges with live values
1332 for (auto &SR
: LI
.subranges()) {
1333 VNInfo
*VNI
= SR
.getVNInfoAt(PrevIndex
);
1335 SR
.addSegment(LiveInterval::Segment(StartIndex
, EndIndex
, VNI
));
1337 } else if (!isLiveOut
&& !isLastMBB
) {
1338 LI
.removeSegment(StartIndex
, EndIndex
);
1339 for (auto &SR
: LI
.subranges())
1340 SR
.removeSegment(StartIndex
, EndIndex
);
1344 // Update all intervals for registers whose uses may have been modified by
1345 // updateTerminator().
1346 LIS
->repairIntervalsInRange(this, getFirstTerminator(), end(), UsedRegs
);
1349 if (auto *MDT
= GET_RESULT(MachineDominatorTree
, getDomTree
, ))
1350 MDT
->recordSplitCriticalEdge(this, Succ
, NMBB
);
1352 if (MachineLoopInfo
*MLI
= GET_RESULT(MachineLoop
, getLI
, Info
))
1353 if (MachineLoop
*TIL
= MLI
->getLoopFor(this)) {
1354 // If one or the other blocks were not in a loop, the new block is not
1355 // either, and thus LI doesn't need to be updated.
1356 if (MachineLoop
*DestLoop
= MLI
->getLoopFor(Succ
)) {
1357 if (TIL
== DestLoop
) {
1358 // Both in the same loop, the NMBB joins loop.
1359 DestLoop
->addBasicBlockToLoop(NMBB
, *MLI
);
1360 } else if (TIL
->contains(DestLoop
)) {
1361 // Edge from an outer loop to an inner loop. Add to the outer loop.
1362 TIL
->addBasicBlockToLoop(NMBB
, *MLI
);
1363 } else if (DestLoop
->contains(TIL
)) {
1364 // Edge from an inner loop to an outer loop. Add to the outer loop.
1365 DestLoop
->addBasicBlockToLoop(NMBB
, *MLI
);
1367 // Edge from two loops with no containment relation. Because these
1368 // are natural loops, we know that the destination block must be the
1369 // header of its loop (adding a branch into a loop elsewhere would
1370 // create an irreducible loop).
1371 assert(DestLoop
->getHeader() == Succ
&&
1372 "Should not create irreducible loops!");
1373 if (MachineLoop
*P
= DestLoop
->getParentLoop())
1374 P
->addBasicBlockToLoop(NMBB
, *MLI
);
1382 bool MachineBasicBlock::canSplitCriticalEdge(
1383 const MachineBasicBlock
*Succ
) const {
1384 // Splitting the critical edge to a landing pad block is non-trivial. Don't do
1385 // it in this generic function.
1386 if (Succ
->isEHPad())
1389 // Splitting the critical edge to a callbr's indirect block isn't advised.
1390 // Don't do it in this generic function.
1391 if (Succ
->isInlineAsmBrIndirectTarget())
1394 const MachineFunction
*MF
= getParent();
1395 // Performance might be harmed on HW that implements branching using exec mask
1396 // where both sides of the branches are always executed.
1397 if (MF
->getTarget().requiresStructuredCFG())
1400 // Do we have an Indirect jump with a jumptable that we can rewrite?
1401 int JTI
= findJumpTableIndex(*this);
1402 if (JTI
>= 0 && !jumpTableHasOtherUses(*MF
, *this, JTI
))
1405 // We may need to update this's terminator, but we can't do that if
1406 // analyzeBranch fails.
1407 const TargetInstrInfo
*TII
= MF
->getSubtarget().getInstrInfo();
1408 MachineBasicBlock
*TBB
= nullptr, *FBB
= nullptr;
1409 SmallVector
<MachineOperand
, 4> Cond
;
1410 // AnalyzeBanch should modify this, since we did not allow modification.
1411 if (TII
->analyzeBranch(*const_cast<MachineBasicBlock
*>(this), TBB
, FBB
, Cond
,
1412 /*AllowModify*/ false))
1415 // Avoid bugpoint weirdness: A block may end with a conditional branch but
1416 // jumps to the same MBB is either case. We have duplicate CFG edges in that
1417 // case that we can't handle. Since this never happens in properly optimized
1418 // code, just skip those edges.
1419 if (TBB
&& TBB
== FBB
) {
1420 LLVM_DEBUG(dbgs() << "Won't split critical edge after degenerate "
1421 << printMBBReference(*this) << '\n');
1427 /// Prepare MI to be removed from its bundle. This fixes bundle flags on MI's
1428 /// neighboring instructions so the bundle won't be broken by removing MI.
1429 static void unbundleSingleMI(MachineInstr
*MI
) {
1430 // Removing the first instruction in a bundle.
1431 if (MI
->isBundledWithSucc() && !MI
->isBundledWithPred())
1432 MI
->unbundleFromSucc();
1433 // Removing the last instruction in a bundle.
1434 if (MI
->isBundledWithPred() && !MI
->isBundledWithSucc())
1435 MI
->unbundleFromPred();
1436 // If MI is not bundled, or if it is internal to a bundle, the neighbor flags
1437 // are already fine.
1440 MachineBasicBlock::instr_iterator
1441 MachineBasicBlock::erase(MachineBasicBlock::instr_iterator I
) {
1442 unbundleSingleMI(&*I
);
1443 return Insts
.erase(I
);
1446 MachineInstr
*MachineBasicBlock::remove_instr(MachineInstr
*MI
) {
1447 unbundleSingleMI(MI
);
1448 MI
->clearFlag(MachineInstr::BundledPred
);
1449 MI
->clearFlag(MachineInstr::BundledSucc
);
1450 return Insts
.remove(MI
);
1453 MachineBasicBlock::instr_iterator
1454 MachineBasicBlock::insert(instr_iterator I
, MachineInstr
*MI
) {
1455 assert(!MI
->isBundledWithPred() && !MI
->isBundledWithSucc() &&
1456 "Cannot insert instruction with bundle flags");
1457 // Set the bundle flags when inserting inside a bundle.
1458 if (I
!= instr_end() && I
->isBundledWithPred()) {
1459 MI
->setFlag(MachineInstr::BundledPred
);
1460 MI
->setFlag(MachineInstr::BundledSucc
);
1462 return Insts
.insert(I
, MI
);
1465 /// This method unlinks 'this' from the containing function, and returns it, but
1466 /// does not delete it.
1467 MachineBasicBlock
*MachineBasicBlock::removeFromParent() {
1468 assert(getParent() && "Not embedded in a function!");
1469 getParent()->remove(this);
1473 /// This method unlinks 'this' from the containing function, and deletes it.
1474 void MachineBasicBlock::eraseFromParent() {
1475 assert(getParent() && "Not embedded in a function!");
1476 getParent()->erase(this);
1479 /// Given a machine basic block that branched to 'Old', change the code and CFG
1480 /// so that it branches to 'New' instead.
1481 void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock
*Old
,
1482 MachineBasicBlock
*New
) {
1483 assert(Old
!= New
&& "Cannot replace self with self!");
1485 MachineBasicBlock::instr_iterator I
= instr_end();
1486 while (I
!= instr_begin()) {
1488 if (!I
->isTerminator()) break;
1490 // Scan the operands of this machine instruction, replacing any uses of Old
1492 for (MachineOperand
&MO
: I
->operands())
1493 if (MO
.isMBB() && MO
.getMBB() == Old
)
1497 // Update the successor information.
1498 replaceSuccessor(Old
, New
);
1501 void MachineBasicBlock::replacePhiUsesWith(MachineBasicBlock
*Old
,
1502 MachineBasicBlock
*New
) {
1503 for (MachineInstr
&MI
: phis())
1504 for (unsigned i
= 2, e
= MI
.getNumOperands() + 1; i
!= e
; i
+= 2) {
1505 MachineOperand
&MO
= MI
.getOperand(i
);
1506 if (MO
.getMBB() == Old
)
1511 /// Find the next valid DebugLoc starting at MBBI, skipping any debug
1512 /// instructions. Return UnknownLoc if there is none.
1514 MachineBasicBlock::findDebugLoc(instr_iterator MBBI
) {
1515 // Skip debug declarations, we don't want a DebugLoc from them.
1516 MBBI
= skipDebugInstructionsForward(MBBI
, instr_end());
1517 if (MBBI
!= instr_end())
1518 return MBBI
->getDebugLoc();
1522 DebugLoc
MachineBasicBlock::rfindDebugLoc(reverse_instr_iterator MBBI
) {
1523 if (MBBI
== instr_rend())
1524 return findDebugLoc(instr_begin());
1525 // Skip debug declarations, we don't want a DebugLoc from them.
1526 MBBI
= skipDebugInstructionsBackward(MBBI
, instr_rbegin());
1527 if (!MBBI
->isDebugInstr())
1528 return MBBI
->getDebugLoc();
1532 /// Find the previous valid DebugLoc preceding MBBI, skipping any debug
1533 /// instructions. Return UnknownLoc if there is none.
1534 DebugLoc
MachineBasicBlock::findPrevDebugLoc(instr_iterator MBBI
) {
1535 if (MBBI
== instr_begin())
1537 // Skip debug instructions, we don't want a DebugLoc from them.
1538 MBBI
= prev_nodbg(MBBI
, instr_begin());
1539 if (!MBBI
->isDebugInstr())
1540 return MBBI
->getDebugLoc();
1544 DebugLoc
MachineBasicBlock::rfindPrevDebugLoc(reverse_instr_iterator MBBI
) {
1545 if (MBBI
== instr_rend())
1547 // Skip debug declarations, we don't want a DebugLoc from them.
1548 MBBI
= next_nodbg(MBBI
, instr_rend());
1549 if (MBBI
!= instr_rend())
1550 return MBBI
->getDebugLoc();
1554 /// Find and return the merged DebugLoc of the branch instructions of the block.
1555 /// Return UnknownLoc if there is none.
1557 MachineBasicBlock::findBranchDebugLoc() {
1559 auto TI
= getFirstTerminator();
1560 while (TI
!= end() && !TI
->isBranch())
1564 DL
= TI
->getDebugLoc();
1565 for (++TI
; TI
!= end() ; ++TI
)
1567 DL
= DILocation::getMergedLocation(DL
, TI
->getDebugLoc());
1572 /// Return probability of the edge from this block to MBB.
1574 MachineBasicBlock::getSuccProbability(const_succ_iterator Succ
) const {
1576 return BranchProbability(1, succ_size());
1578 const auto &Prob
= *getProbabilityIterator(Succ
);
1579 if (Prob
.isUnknown()) {
1580 // For unknown probabilities, collect the sum of all known ones, and evenly
1581 // ditribute the complemental of the sum to each unknown probability.
1582 unsigned KnownProbNum
= 0;
1583 auto Sum
= BranchProbability::getZero();
1584 for (const auto &P
: Probs
) {
1585 if (!P
.isUnknown()) {
1590 return Sum
.getCompl() / (Probs
.size() - KnownProbNum
);
1595 /// Set successor probability of a given iterator.
1596 void MachineBasicBlock::setSuccProbability(succ_iterator I
,
1597 BranchProbability Prob
) {
1598 assert(!Prob
.isUnknown());
1601 *getProbabilityIterator(I
) = Prob
;
1604 /// Return probability iterator corresonding to the I successor iterator
1605 MachineBasicBlock::const_probability_iterator
1606 MachineBasicBlock::getProbabilityIterator(
1607 MachineBasicBlock::const_succ_iterator I
) const {
1608 assert(Probs
.size() == Successors
.size() && "Async probability list!");
1609 const size_t index
= std::distance(Successors
.begin(), I
);
1610 assert(index
< Probs
.size() && "Not a current successor!");
1611 return Probs
.begin() + index
;
1614 /// Return probability iterator corresonding to the I successor iterator.
1615 MachineBasicBlock::probability_iterator
1616 MachineBasicBlock::getProbabilityIterator(MachineBasicBlock::succ_iterator I
) {
1617 assert(Probs
.size() == Successors
.size() && "Async probability list!");
1618 const size_t index
= std::distance(Successors
.begin(), I
);
1619 assert(index
< Probs
.size() && "Not a current successor!");
1620 return Probs
.begin() + index
;
1623 /// Return whether (physical) register "Reg" has been <def>ined and not <kill>ed
1624 /// as of just before "MI".
1626 /// Search is localised to a neighborhood of
1627 /// Neighborhood instructions before (searching for defs or kills) and N
1628 /// instructions after (searching just for defs) MI.
1629 MachineBasicBlock::LivenessQueryResult
1630 MachineBasicBlock::computeRegisterLiveness(const TargetRegisterInfo
*TRI
,
1631 MCRegister Reg
, const_iterator Before
,
1632 unsigned Neighborhood
) const {
1633 unsigned N
= Neighborhood
;
1635 // Try searching forwards from Before, looking for reads or defs.
1636 const_iterator
I(Before
);
1637 for (; I
!= end() && N
> 0; ++I
) {
1638 if (I
->isDebugOrPseudoInstr())
1643 PhysRegInfo Info
= AnalyzePhysRegInBundle(*I
, Reg
, TRI
);
1645 // Register is live when we read it here.
1648 // Register is dead if we can fully overwrite or clobber it here.
1649 if (Info
.FullyDefined
|| Info
.Clobbered
)
1653 // If we reached the end, it is safe to clobber Reg at the end of a block of
1654 // no successor has it live in.
1656 for (MachineBasicBlock
*S
: successors()) {
1657 for (const MachineBasicBlock::RegisterMaskPair
&LI
: S
->liveins()) {
1658 if (TRI
->regsOverlap(LI
.PhysReg
, Reg
))
1669 // Start by searching backwards from Before, looking for kills, reads or defs.
1670 I
= const_iterator(Before
);
1671 // If this is the first insn in the block, don't search backwards.
1676 if (I
->isDebugOrPseudoInstr())
1681 PhysRegInfo Info
= AnalyzePhysRegInBundle(*I
, Reg
, TRI
);
1683 // Defs happen after uses so they take precedence if both are present.
1685 // Register is dead after a dead def of the full register.
1688 // Register is (at least partially) live after a def.
1690 if (!Info
.PartialDeadDef
)
1692 // As soon as we saw a partial definition (dead or not),
1693 // we cannot tell if the value is partial live without
1694 // tracking the lanemasks. We are not going to do this,
1695 // so fall back on the remaining of the analysis.
1698 // Register is dead after a full kill or clobber and no def.
1699 if (Info
.Killed
|| Info
.Clobbered
)
1701 // Register must be live if we read it.
1705 } while (I
!= begin() && N
> 0);
1708 // If all the instructions before this in the block are debug instructions,
1710 while (I
!= begin() && std::prev(I
)->isDebugOrPseudoInstr())
1713 // Did we get to the start of the block?
1715 // If so, the register's state is definitely defined by the live-in state.
1716 for (const MachineBasicBlock::RegisterMaskPair
&LI
: liveins())
1717 if (TRI
->regsOverlap(LI
.PhysReg
, Reg
))
1723 // At this point we have no idea of the liveness of the register.
1728 MachineBasicBlock::getBeginClobberMask(const TargetRegisterInfo
*TRI
) const {
1729 // EH funclet entry does not preserve any registers.
1730 return isEHFuncletEntry() ? TRI
->getNoPreservedMask() : nullptr;
1734 MachineBasicBlock::getEndClobberMask(const TargetRegisterInfo
*TRI
) const {
1735 // If we see a return block with successors, this must be a funclet return,
1736 // which does not preserve any registers. If there are no successors, we don't
1737 // care what kind of return it is, putting a mask after it is a no-op.
1738 return isReturnBlock() && !succ_empty() ? TRI
->getNoPreservedMask() : nullptr;
1741 void MachineBasicBlock::clearLiveIns() {
1745 void MachineBasicBlock::clearLiveIns(
1746 std::vector
<RegisterMaskPair
> &OldLiveIns
) {
1747 assert(OldLiveIns
.empty() && "Vector must be empty");
1748 std::swap(LiveIns
, OldLiveIns
);
1751 MachineBasicBlock::livein_iterator
MachineBasicBlock::livein_begin() const {
1752 assert(getParent()->getProperties().hasProperty(
1753 MachineFunctionProperties::Property::TracksLiveness
) &&
1754 "Liveness information is accurate");
1755 return LiveIns
.begin();
1758 MachineBasicBlock::liveout_iterator
MachineBasicBlock::liveout_begin() const {
1759 const MachineFunction
&MF
= *getParent();
1760 assert(MF
.getProperties().hasProperty(
1761 MachineFunctionProperties::Property::TracksLiveness
) &&
1762 "Liveness information is accurate");
1764 const TargetLowering
&TLI
= *MF
.getSubtarget().getTargetLowering();
1765 MCPhysReg ExceptionPointer
= 0, ExceptionSelector
= 0;
1766 if (MF
.getFunction().hasPersonalityFn()) {
1767 auto PersonalityFn
= MF
.getFunction().getPersonalityFn();
1768 ExceptionPointer
= TLI
.getExceptionPointerRegister(PersonalityFn
);
1769 ExceptionSelector
= TLI
.getExceptionSelectorRegister(PersonalityFn
);
1772 return liveout_iterator(*this, ExceptionPointer
, ExceptionSelector
, false);
1775 bool MachineBasicBlock::sizeWithoutDebugLargerThan(unsigned Limit
) const {
1777 auto R
= instructionsWithoutDebug(begin(), end());
1778 for (auto I
= R
.begin(), E
= R
.end(); I
!= E
; ++I
) {
1785 const MBBSectionID
MBBSectionID::ColdSectionID(MBBSectionID::SectionType::Cold
);
1787 MBBSectionID::ExceptionSectionID(MBBSectionID::SectionType::Exception
);