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 const StringRef Prefix
= Ctx
.getAsmInfo()->getPrivateLabelPrefix();
84 CachedMCSymbol
= Ctx
.getOrCreateSymbol(Twine(Prefix
) + "BB" +
85 Twine(MF
->getFunctionNumber()) +
86 "_" + Twine(getNumber()));
89 return CachedMCSymbol
;
92 MCSymbol
*MachineBasicBlock::getEHCatchretSymbol() const {
93 if (!CachedEHCatchretMCSymbol
) {
94 const MachineFunction
*MF
= getParent();
95 SmallString
<128> SymbolName
;
96 raw_svector_ostream(SymbolName
)
97 << "$ehgcr_" << MF
->getFunctionNumber() << '_' << getNumber();
98 CachedEHCatchretMCSymbol
= MF
->getContext().getOrCreateSymbol(SymbolName
);
100 return CachedEHCatchretMCSymbol
;
103 MCSymbol
*MachineBasicBlock::getEndSymbol() const {
104 if (!CachedEndMCSymbol
) {
105 const MachineFunction
*MF
= getParent();
106 MCContext
&Ctx
= MF
->getContext();
107 auto Prefix
= Ctx
.getAsmInfo()->getPrivateLabelPrefix();
108 CachedEndMCSymbol
= Ctx
.getOrCreateSymbol(Twine(Prefix
) + "BB_END" +
109 Twine(MF
->getFunctionNumber()) +
110 "_" + Twine(getNumber()));
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 StringRef
MachineBasicBlock::getName() const {
319 if (const BasicBlock
*LBB
= getBasicBlock())
320 return LBB
->getName();
322 return StringRef("", 0);
325 /// Return a hopefully unique identifier for this block.
326 std::string
MachineBasicBlock::getFullName() const {
329 Name
= (getParent()->getName() + ":").str();
331 Name
+= getBasicBlock()->getName();
333 Name
+= ("BB" + Twine(getNumber())).str();
337 void MachineBasicBlock::print(raw_ostream
&OS
, const SlotIndexes
*Indexes
,
338 bool IsStandalone
) const {
339 const MachineFunction
*MF
= getParent();
341 OS
<< "Can't print out MachineBasicBlock because parent MachineFunction"
345 const Function
&F
= MF
->getFunction();
346 const Module
*M
= F
.getParent();
347 ModuleSlotTracker
MST(M
);
348 MST
.incorporateFunction(F
);
349 print(OS
, MST
, Indexes
, IsStandalone
);
352 void MachineBasicBlock::print(raw_ostream
&OS
, ModuleSlotTracker
&MST
,
353 const SlotIndexes
*Indexes
,
354 bool IsStandalone
) const {
355 const MachineFunction
*MF
= getParent();
357 OS
<< "Can't print out MachineBasicBlock because parent MachineFunction"
362 if (Indexes
&& PrintSlotIndexes
)
363 OS
<< Indexes
->getMBBStartIdx(this) << '\t';
365 printName(OS
, PrintNameIr
| PrintNameAttributes
, &MST
);
368 const TargetRegisterInfo
*TRI
= MF
->getSubtarget().getRegisterInfo();
369 const MachineRegisterInfo
&MRI
= MF
->getRegInfo();
370 const TargetInstrInfo
&TII
= *getParent()->getSubtarget().getInstrInfo();
371 bool HasLineAttributes
= false;
373 // Print the preds of this block according to the CFG.
374 if (!pred_empty() && IsStandalone
) {
375 if (Indexes
) OS
<< '\t';
376 // Don't indent(2), align with previous line attributes.
377 OS
<< "; predecessors: ";
379 for (auto *Pred
: predecessors())
380 OS
<< LS
<< printMBBReference(*Pred
);
382 HasLineAttributes
= true;
386 if (Indexes
) OS
<< '\t';
387 // Print the successors
388 OS
.indent(2) << "successors: ";
390 for (auto I
= succ_begin(), E
= succ_end(); I
!= E
; ++I
) {
391 OS
<< LS
<< printMBBReference(**I
);
394 << format("0x%08" PRIx32
, getSuccProbability(I
).getNumerator())
397 if (!Probs
.empty() && IsStandalone
) {
398 // Print human readable probabilities as comments.
401 for (auto I
= succ_begin(), E
= succ_end(); I
!= E
; ++I
) {
402 const BranchProbability
&BP
= getSuccProbability(I
);
403 OS
<< LS
<< printMBBReference(**I
) << '('
405 rint(((double)BP
.getNumerator() / BP
.getDenominator()) *
413 HasLineAttributes
= true;
416 if (!livein_empty() && MRI
.tracksLiveness()) {
417 if (Indexes
) OS
<< '\t';
418 OS
.indent(2) << "liveins: ";
421 for (const auto &LI
: liveins()) {
422 OS
<< LS
<< printReg(LI
.PhysReg
, TRI
);
423 if (!LI
.LaneMask
.all())
424 OS
<< ":0x" << PrintLaneMask(LI
.LaneMask
);
426 HasLineAttributes
= true;
429 if (HasLineAttributes
)
432 bool IsInBundle
= false;
433 for (const MachineInstr
&MI
: instrs()) {
434 if (Indexes
&& PrintSlotIndexes
) {
435 if (Indexes
->hasIndex(MI
))
436 OS
<< Indexes
->getInstructionIndex(MI
);
440 if (IsInBundle
&& !MI
.isInsideBundle()) {
441 OS
.indent(2) << "}\n";
445 OS
.indent(IsInBundle
? 4 : 2);
446 MI
.print(OS
, MST
, IsStandalone
, /*SkipOpers=*/false, /*SkipDebugLoc=*/false,
447 /*AddNewLine=*/false, &TII
);
449 if (!IsInBundle
&& MI
.getFlag(MachineInstr::BundledSucc
)) {
457 OS
.indent(2) << "}\n";
459 if (IrrLoopHeaderWeight
&& IsStandalone
) {
460 if (Indexes
) OS
<< '\t';
461 OS
.indent(2) << "; Irreducible loop header weight: " << *IrrLoopHeaderWeight
466 /// Print the basic block's name as:
468 /// bb.{number}[.{ir-name}] [(attributes...)]
470 /// The {ir-name} is only printed when the \ref PrintNameIr flag is passed
471 /// (which is the default). If the IR block has no name, it is identified
472 /// numerically using the attribute syntax as "(%ir-block.{ir-slot})".
474 /// When the \ref PrintNameAttributes flag is passed, additional attributes
475 /// of the block are printed when set.
477 /// \param printNameFlags Combination of \ref PrintNameFlag flags indicating
478 /// the parts to print.
479 /// \param moduleSlotTracker Optional ModuleSlotTracker. This method will
480 /// incorporate its own tracker when necessary to
481 /// determine the block's IR name.
482 void MachineBasicBlock::printName(raw_ostream
&os
, unsigned printNameFlags
,
483 ModuleSlotTracker
*moduleSlotTracker
) const {
484 os
<< "bb." << getNumber();
485 bool hasAttributes
= false;
487 auto PrintBBRef
= [&](const BasicBlock
*bb
) {
494 if (moduleSlotTracker
) {
495 slot
= moduleSlotTracker
->getLocalSlot(bb
);
496 } else if (bb
->getParent()) {
497 ModuleSlotTracker
tmpTracker(bb
->getModule(), false);
498 tmpTracker
.incorporateFunction(*bb
->getParent());
499 slot
= tmpTracker
.getLocalSlot(bb
);
503 os
<< "<ir-block badref>";
509 if (printNameFlags
& PrintNameIr
) {
510 if (const auto *bb
= getBasicBlock()) {
512 os
<< '.' << bb
->getName();
514 hasAttributes
= true;
521 if (printNameFlags
& PrintNameAttributes
) {
522 if (isMachineBlockAddressTaken()) {
523 os
<< (hasAttributes
? ", " : " (");
524 os
<< "machine-block-address-taken";
525 hasAttributes
= true;
527 if (isIRBlockAddressTaken()) {
528 os
<< (hasAttributes
? ", " : " (");
529 os
<< "ir-block-address-taken ";
530 PrintBBRef(getAddressTakenIRBlock());
531 hasAttributes
= true;
534 os
<< (hasAttributes
? ", " : " (");
536 hasAttributes
= true;
538 if (isInlineAsmBrIndirectTarget()) {
539 os
<< (hasAttributes
? ", " : " (");
540 os
<< "inlineasm-br-indirect-target";
541 hasAttributes
= true;
543 if (isEHFuncletEntry()) {
544 os
<< (hasAttributes
? ", " : " (");
545 os
<< "ehfunclet-entry";
546 hasAttributes
= true;
548 if (getAlignment() != Align(1)) {
549 os
<< (hasAttributes
? ", " : " (");
550 os
<< "align " << getAlignment().value();
551 hasAttributes
= true;
553 if (getSectionID() != MBBSectionID(0)) {
554 os
<< (hasAttributes
? ", " : " (");
556 switch (getSectionID().Type
) {
557 case MBBSectionID::SectionType::Exception
:
560 case MBBSectionID::SectionType::Cold
:
564 os
<< getSectionID().Number
;
566 hasAttributes
= true;
568 if (getBBID().has_value()) {
569 os
<< (hasAttributes
? ", " : " (");
570 os
<< "bb_id " << getBBID()->BaseID
;
571 if (getBBID()->CloneID
!= 0)
572 os
<< " " << getBBID()->CloneID
;
573 hasAttributes
= true;
575 if (CallFrameSize
!= 0) {
576 os
<< (hasAttributes
? ", " : " (");
577 os
<< "call-frame-size " << CallFrameSize
;
578 hasAttributes
= true;
586 void MachineBasicBlock::printAsOperand(raw_ostream
&OS
,
587 bool /*PrintType*/) const {
592 void MachineBasicBlock::removeLiveIn(MCPhysReg Reg
, LaneBitmask LaneMask
) {
593 LiveInVector::iterator I
= find_if(
594 LiveIns
, [Reg
](const RegisterMaskPair
&LI
) { return LI
.PhysReg
== Reg
; });
595 if (I
== LiveIns
.end())
598 I
->LaneMask
&= ~LaneMask
;
599 if (I
->LaneMask
.none())
603 MachineBasicBlock::livein_iterator
604 MachineBasicBlock::removeLiveIn(MachineBasicBlock::livein_iterator I
) {
605 // Get non-const version of iterator.
606 LiveInVector::iterator LI
= LiveIns
.begin() + (I
- LiveIns
.begin());
607 return LiveIns
.erase(LI
);
610 bool MachineBasicBlock::isLiveIn(MCPhysReg Reg
, LaneBitmask LaneMask
) const {
611 livein_iterator I
= find_if(
612 LiveIns
, [Reg
](const RegisterMaskPair
&LI
) { return LI
.PhysReg
== Reg
; });
613 return I
!= livein_end() && (I
->LaneMask
& LaneMask
).any();
616 void MachineBasicBlock::sortUniqueLiveIns() {
618 [](const RegisterMaskPair
&LI0
, const RegisterMaskPair
&LI1
) {
619 return LI0
.PhysReg
< LI1
.PhysReg
;
621 // Liveins are sorted by physreg now we can merge their lanemasks.
622 LiveInVector::const_iterator I
= LiveIns
.begin();
623 LiveInVector::const_iterator J
;
624 LiveInVector::iterator Out
= LiveIns
.begin();
625 for (; I
!= LiveIns
.end(); ++Out
, I
= J
) {
626 MCRegister PhysReg
= I
->PhysReg
;
627 LaneBitmask LaneMask
= I
->LaneMask
;
628 for (J
= std::next(I
); J
!= LiveIns
.end() && J
->PhysReg
== PhysReg
; ++J
)
629 LaneMask
|= J
->LaneMask
;
630 Out
->PhysReg
= PhysReg
;
631 Out
->LaneMask
= LaneMask
;
633 LiveIns
.erase(Out
, LiveIns
.end());
637 MachineBasicBlock::addLiveIn(MCRegister PhysReg
, const TargetRegisterClass
*RC
) {
638 assert(getParent() && "MBB must be inserted in function");
639 assert(Register::isPhysicalRegister(PhysReg
) && "Expected physreg");
640 assert(RC
&& "Register class is required");
641 assert((isEHPad() || this == &getParent()->front()) &&
642 "Only the entry block and landing pads can have physreg live ins");
644 bool LiveIn
= isLiveIn(PhysReg
);
645 iterator I
= SkipPHIsAndLabels(begin()), E
= end();
646 MachineRegisterInfo
&MRI
= getParent()->getRegInfo();
647 const TargetInstrInfo
&TII
= *getParent()->getSubtarget().getInstrInfo();
649 // Look for an existing copy.
651 for (;I
!= E
&& I
->isCopy(); ++I
)
652 if (I
->getOperand(1).getReg() == PhysReg
) {
653 Register VirtReg
= I
->getOperand(0).getReg();
654 if (!MRI
.constrainRegClass(VirtReg
, RC
))
655 llvm_unreachable("Incompatible live-in register class.");
659 // No luck, create a virtual register.
660 Register VirtReg
= MRI
.createVirtualRegister(RC
);
661 BuildMI(*this, I
, DebugLoc(), TII
.get(TargetOpcode::COPY
), VirtReg
)
662 .addReg(PhysReg
, RegState::Kill
);
668 void MachineBasicBlock::moveBefore(MachineBasicBlock
*NewAfter
) {
669 getParent()->splice(NewAfter
->getIterator(), getIterator());
672 void MachineBasicBlock::moveAfter(MachineBasicBlock
*NewBefore
) {
673 getParent()->splice(++NewBefore
->getIterator(), getIterator());
676 static int findJumpTableIndex(const MachineBasicBlock
&MBB
) {
677 MachineBasicBlock::const_iterator TerminatorI
= MBB
.getFirstTerminator();
678 if (TerminatorI
== MBB
.end())
680 const MachineInstr
&Terminator
= *TerminatorI
;
681 const TargetInstrInfo
*TII
= MBB
.getParent()->getSubtarget().getInstrInfo();
682 return TII
->getJumpTableIndex(Terminator
);
685 void MachineBasicBlock::updateTerminator(
686 MachineBasicBlock
*PreviousLayoutSuccessor
) {
687 LLVM_DEBUG(dbgs() << "Updating terminators on " << printMBBReference(*this)
690 const TargetInstrInfo
*TII
= getParent()->getSubtarget().getInstrInfo();
691 // A block with no successors has no concerns with fall-through edges.
692 if (this->succ_empty())
695 MachineBasicBlock
*TBB
= nullptr, *FBB
= nullptr;
696 SmallVector
<MachineOperand
, 4> Cond
;
697 DebugLoc DL
= findBranchDebugLoc();
698 bool B
= TII
->analyzeBranch(*this, TBB
, FBB
, Cond
);
700 assert(!B
&& "UpdateTerminators requires analyzable predecessors!");
703 // The block has an unconditional branch. If its successor is now its
704 // layout successor, delete the branch.
705 if (isLayoutSuccessor(TBB
))
706 TII
->removeBranch(*this);
708 // The block has an unconditional fallthrough, or the end of the block is
711 // Unfortunately, whether the end of the block is unreachable is not
712 // immediately obvious; we must fall back to checking the successor list,
713 // and assuming that if the passed in block is in the succesor list and
714 // not an EHPad, it must be the intended target.
715 if (!PreviousLayoutSuccessor
|| !isSuccessor(PreviousLayoutSuccessor
) ||
716 PreviousLayoutSuccessor
->isEHPad())
719 // If the unconditional successor block is not the current layout
720 // successor, insert a branch to jump to it.
721 if (!isLayoutSuccessor(PreviousLayoutSuccessor
))
722 TII
->insertBranch(*this, PreviousLayoutSuccessor
, nullptr, Cond
, DL
);
728 // The block has a non-fallthrough conditional branch. If one of its
729 // successors is its layout successor, rewrite it to a fallthrough
730 // conditional branch.
731 if (isLayoutSuccessor(TBB
)) {
732 if (TII
->reverseBranchCondition(Cond
))
734 TII
->removeBranch(*this);
735 TII
->insertBranch(*this, FBB
, nullptr, Cond
, DL
);
736 } else if (isLayoutSuccessor(FBB
)) {
737 TII
->removeBranch(*this);
738 TII
->insertBranch(*this, TBB
, nullptr, Cond
, DL
);
743 // We now know we're going to fallthrough to PreviousLayoutSuccessor.
744 assert(PreviousLayoutSuccessor
);
745 assert(!PreviousLayoutSuccessor
->isEHPad());
746 assert(isSuccessor(PreviousLayoutSuccessor
));
748 if (PreviousLayoutSuccessor
== TBB
) {
749 // We had a fallthrough to the same basic block as the conditional jump
750 // targets. Remove the conditional jump, leaving an unconditional
751 // fallthrough or an unconditional jump.
752 TII
->removeBranch(*this);
753 if (!isLayoutSuccessor(TBB
)) {
755 TII
->insertBranch(*this, TBB
, nullptr, Cond
, DL
);
760 // The block has a fallthrough conditional branch.
761 if (isLayoutSuccessor(TBB
)) {
762 if (TII
->reverseBranchCondition(Cond
)) {
763 // We can't reverse the condition, add an unconditional branch.
765 TII
->insertBranch(*this, PreviousLayoutSuccessor
, nullptr, Cond
, DL
);
768 TII
->removeBranch(*this);
769 TII
->insertBranch(*this, PreviousLayoutSuccessor
, nullptr, Cond
, DL
);
770 } else if (!isLayoutSuccessor(PreviousLayoutSuccessor
)) {
771 TII
->removeBranch(*this);
772 TII
->insertBranch(*this, TBB
, PreviousLayoutSuccessor
, Cond
, DL
);
776 void MachineBasicBlock::validateSuccProbs() const {
779 for (auto Prob
: Probs
)
780 Sum
+= Prob
.getNumerator();
781 // Due to precision issue, we assume that the sum of probabilities is one if
782 // the difference between the sum of their numerators and the denominator is
783 // no greater than the number of successors.
784 assert((uint64_t)std::abs(Sum
- BranchProbability::getDenominator()) <=
786 "The sum of successors's probabilities exceeds one.");
790 void MachineBasicBlock::addSuccessor(MachineBasicBlock
*Succ
,
791 BranchProbability Prob
) {
792 // Probability list is either empty (if successor list isn't empty, this means
793 // disabled optimization) or has the same size as successor list.
794 if (!(Probs
.empty() && !Successors
.empty()))
795 Probs
.push_back(Prob
);
796 Successors
.push_back(Succ
);
797 Succ
->addPredecessor(this);
800 void MachineBasicBlock::addSuccessorWithoutProb(MachineBasicBlock
*Succ
) {
801 // We need to make sure probability list is either empty or has the same size
802 // of successor list. When this function is called, we can safely delete all
803 // probability in the list.
805 Successors
.push_back(Succ
);
806 Succ
->addPredecessor(this);
809 void MachineBasicBlock::splitSuccessor(MachineBasicBlock
*Old
,
810 MachineBasicBlock
*New
,
811 bool NormalizeSuccProbs
) {
812 succ_iterator OldI
= llvm::find(successors(), Old
);
813 assert(OldI
!= succ_end() && "Old is not a successor of this block!");
814 assert(!llvm::is_contained(successors(), New
) &&
815 "New is already a successor of this block!");
817 // Add a new successor with equal probability as the original one. Note
818 // that we directly copy the probability using the iterator rather than
819 // getting a potentially synthetic probability computed when unknown. This
820 // preserves the probabilities as-is and then we can renormalize them and
821 // query them effectively afterward.
822 addSuccessor(New
, Probs
.empty() ? BranchProbability::getUnknown()
823 : *getProbabilityIterator(OldI
));
824 if (NormalizeSuccProbs
)
825 normalizeSuccProbs();
828 void MachineBasicBlock::removeSuccessor(MachineBasicBlock
*Succ
,
829 bool NormalizeSuccProbs
) {
830 succ_iterator I
= find(Successors
, Succ
);
831 removeSuccessor(I
, NormalizeSuccProbs
);
834 MachineBasicBlock::succ_iterator
835 MachineBasicBlock::removeSuccessor(succ_iterator I
, bool NormalizeSuccProbs
) {
836 assert(I
!= Successors
.end() && "Not a current successor!");
838 // If probability list is empty it means we don't use it (disabled
840 if (!Probs
.empty()) {
841 probability_iterator WI
= getProbabilityIterator(I
);
843 if (NormalizeSuccProbs
)
844 normalizeSuccProbs();
847 (*I
)->removePredecessor(this);
848 return Successors
.erase(I
);
851 void MachineBasicBlock::replaceSuccessor(MachineBasicBlock
*Old
,
852 MachineBasicBlock
*New
) {
856 succ_iterator E
= succ_end();
857 succ_iterator NewI
= E
;
858 succ_iterator OldI
= E
;
859 for (succ_iterator I
= succ_begin(); I
!= E
; ++I
) {
871 assert(OldI
!= E
&& "Old is not a successor of this block");
873 // If New isn't already a successor, let it take Old's place.
875 Old
->removePredecessor(this);
876 New
->addPredecessor(this);
881 // New is already a successor.
882 // Update its probability instead of adding a duplicate edge.
883 if (!Probs
.empty()) {
884 auto ProbIter
= getProbabilityIterator(NewI
);
885 if (!ProbIter
->isUnknown())
886 *ProbIter
+= *getProbabilityIterator(OldI
);
888 removeSuccessor(OldI
);
891 void MachineBasicBlock::copySuccessor(const MachineBasicBlock
*Orig
,
893 if (!Orig
->Probs
.empty())
894 addSuccessor(*I
, Orig
->getSuccProbability(I
));
896 addSuccessorWithoutProb(*I
);
899 void MachineBasicBlock::addPredecessor(MachineBasicBlock
*Pred
) {
900 Predecessors
.push_back(Pred
);
903 void MachineBasicBlock::removePredecessor(MachineBasicBlock
*Pred
) {
904 pred_iterator I
= find(Predecessors
, Pred
);
905 assert(I
!= Predecessors
.end() && "Pred is not a predecessor of this block!");
906 Predecessors
.erase(I
);
909 void MachineBasicBlock::transferSuccessors(MachineBasicBlock
*FromMBB
) {
913 while (!FromMBB
->succ_empty()) {
914 MachineBasicBlock
*Succ
= *FromMBB
->succ_begin();
916 // If probability list is empty it means we don't use it (disabled
918 if (!FromMBB
->Probs
.empty()) {
919 auto Prob
= *FromMBB
->Probs
.begin();
920 addSuccessor(Succ
, Prob
);
922 addSuccessorWithoutProb(Succ
);
924 FromMBB
->removeSuccessor(Succ
);
929 MachineBasicBlock::transferSuccessorsAndUpdatePHIs(MachineBasicBlock
*FromMBB
) {
933 while (!FromMBB
->succ_empty()) {
934 MachineBasicBlock
*Succ
= *FromMBB
->succ_begin();
935 if (!FromMBB
->Probs
.empty()) {
936 auto Prob
= *FromMBB
->Probs
.begin();
937 addSuccessor(Succ
, Prob
);
939 addSuccessorWithoutProb(Succ
);
940 FromMBB
->removeSuccessor(Succ
);
942 // Fix up any PHI nodes in the successor.
943 Succ
->replacePhiUsesWith(FromMBB
, this);
945 normalizeSuccProbs();
948 bool MachineBasicBlock::isPredecessor(const MachineBasicBlock
*MBB
) const {
949 return is_contained(predecessors(), MBB
);
952 bool MachineBasicBlock::isSuccessor(const MachineBasicBlock
*MBB
) const {
953 return is_contained(successors(), MBB
);
956 bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock
*MBB
) const {
957 MachineFunction::const_iterator
I(this);
958 return std::next(I
) == MachineFunction::const_iterator(MBB
);
961 const MachineBasicBlock
*MachineBasicBlock::getSingleSuccessor() const {
962 return Successors
.size() == 1 ? Successors
[0] : nullptr;
965 const MachineBasicBlock
*MachineBasicBlock::getSinglePredecessor() const {
966 return Predecessors
.size() == 1 ? Predecessors
[0] : nullptr;
969 MachineBasicBlock
*MachineBasicBlock::getFallThrough(bool JumpToFallThrough
) {
970 MachineFunction::iterator Fallthrough
= getIterator();
972 // If FallthroughBlock is off the end of the function, it can't fall through.
973 if (Fallthrough
== getParent()->end())
976 // If FallthroughBlock isn't a successor, no fallthrough is possible.
977 if (!isSuccessor(&*Fallthrough
))
980 // Analyze the branches, if any, at the end of the block.
981 MachineBasicBlock
*TBB
= nullptr, *FBB
= nullptr;
982 SmallVector
<MachineOperand
, 4> Cond
;
983 const TargetInstrInfo
*TII
= getParent()->getSubtarget().getInstrInfo();
984 if (TII
->analyzeBranch(*this, TBB
, FBB
, Cond
)) {
985 // If we couldn't analyze the branch, examine the last instruction.
986 // If the block doesn't end in a known control barrier, assume fallthrough
987 // is possible. The isPredicated check is needed because this code can be
988 // called during IfConversion, where an instruction which is normally a
989 // Barrier is predicated and thus no longer an actual control barrier.
990 return (empty() || !back().isBarrier() || TII
->isPredicated(back()))
995 // If there is no branch, control always falls through.
996 if (!TBB
) return &*Fallthrough
;
998 // If there is some explicit branch to the fallthrough block, it can obviously
999 // reach, even though the branch should get folded to fall through implicitly.
1000 if (JumpToFallThrough
&& (MachineFunction::iterator(TBB
) == Fallthrough
||
1001 MachineFunction::iterator(FBB
) == Fallthrough
))
1002 return &*Fallthrough
;
1004 // If it's an unconditional branch to some block not the fall through, it
1005 // doesn't fall through.
1006 if (Cond
.empty()) return nullptr;
1008 // Otherwise, if it is conditional and has no explicit false block, it falls
1010 return (FBB
== nullptr) ? &*Fallthrough
: nullptr;
1013 bool MachineBasicBlock::canFallThrough() {
1014 return getFallThrough() != nullptr;
1017 MachineBasicBlock
*MachineBasicBlock::splitAt(MachineInstr
&MI
,
1019 LiveIntervals
*LIS
) {
1020 MachineBasicBlock::iterator
SplitPoint(&MI
);
1023 if (SplitPoint
== end()) {
1024 // Don't bother with a new block.
1028 MachineFunction
*MF
= getParent();
1030 LivePhysRegs LiveRegs
;
1031 if (UpdateLiveIns
) {
1032 // Make sure we add any physregs we define in the block as liveins to the
1034 MachineBasicBlock::iterator
Prev(&MI
);
1035 LiveRegs
.init(*MF
->getSubtarget().getRegisterInfo());
1036 LiveRegs
.addLiveOuts(*this);
1037 for (auto I
= rbegin(), E
= Prev
.getReverse(); I
!= E
; ++I
)
1038 LiveRegs
.stepBackward(*I
);
1041 MachineBasicBlock
*SplitBB
= MF
->CreateMachineBasicBlock(getBasicBlock());
1043 MF
->insert(++MachineFunction::iterator(this), SplitBB
);
1044 SplitBB
->splice(SplitBB
->begin(), this, SplitPoint
, end());
1046 SplitBB
->transferSuccessorsAndUpdatePHIs(this);
1047 addSuccessor(SplitBB
);
1050 addLiveIns(*SplitBB
, LiveRegs
);
1053 LIS
->insertMBBInMaps(SplitBB
);
1058 // Returns `true` if there are possibly other users of the jump table at
1059 // `JumpTableIndex` except for the ones in `IgnoreMBB`.
1060 static bool jumpTableHasOtherUses(const MachineFunction
&MF
,
1061 const MachineBasicBlock
&IgnoreMBB
,
1062 int JumpTableIndex
) {
1063 assert(JumpTableIndex
>= 0 && "need valid index");
1064 const MachineJumpTableInfo
&MJTI
= *MF
.getJumpTableInfo();
1065 const MachineJumpTableEntry
&MJTE
= MJTI
.getJumpTables()[JumpTableIndex
];
1066 // Take any basic block from the table; every user of the jump table must
1067 // show up in the predecessor list.
1068 const MachineBasicBlock
*MBB
= nullptr;
1069 for (MachineBasicBlock
*B
: MJTE
.MBBs
) {
1076 return true; // can't rule out other users if there isn't any block.
1077 const TargetInstrInfo
&TII
= *MF
.getSubtarget().getInstrInfo();
1078 SmallVector
<MachineOperand
, 4> Cond
;
1079 for (MachineBasicBlock
*Pred
: MBB
->predecessors()) {
1080 if (Pred
== &IgnoreMBB
)
1082 MachineBasicBlock
*DummyT
= nullptr;
1083 MachineBasicBlock
*DummyF
= nullptr;
1085 if (!TII
.analyzeBranch(*Pred
, DummyT
, DummyF
, Cond
,
1086 /*AllowModify=*/false)) {
1087 // analyzable direct jump
1090 int PredJTI
= findJumpTableIndex(*Pred
);
1092 if (PredJTI
== JumpTableIndex
)
1096 // Be conservative for unanalyzable jumps.
1102 class SlotIndexUpdateDelegate
: public MachineFunction::Delegate
{
1104 MachineFunction
&MF
;
1105 SlotIndexes
*Indexes
;
1106 SmallSetVector
<MachineInstr
*, 2> Insertions
;
1109 SlotIndexUpdateDelegate(MachineFunction
&MF
, SlotIndexes
*Indexes
)
1110 : MF(MF
), Indexes(Indexes
) {
1111 MF
.setDelegate(this);
1114 ~SlotIndexUpdateDelegate() {
1115 MF
.resetDelegate(this);
1116 for (auto MI
: Insertions
)
1117 Indexes
->insertMachineInstrInMaps(*MI
);
1120 void MF_HandleInsertion(MachineInstr
&MI
) override
{
1121 // This is called before MI is inserted into block so defer index update.
1123 Insertions
.insert(&MI
);
1126 void MF_HandleRemoval(MachineInstr
&MI
) override
{
1127 if (Indexes
&& !Insertions
.remove(&MI
))
1128 Indexes
->removeMachineInstrFromMaps(MI
);
1132 MachineBasicBlock
*MachineBasicBlock::SplitCriticalEdge(
1133 MachineBasicBlock
*Succ
, Pass
&P
,
1134 std::vector
<SparseBitVector
<>> *LiveInSets
) {
1135 if (!canSplitCriticalEdge(Succ
))
1138 MachineFunction
*MF
= getParent();
1139 MachineBasicBlock
*PrevFallthrough
= getNextNode();
1140 DebugLoc DL
; // FIXME: this is nowhere
1142 MachineBasicBlock
*NMBB
= MF
->CreateMachineBasicBlock();
1143 NMBB
->setCallFrameSize(Succ
->getCallFrameSize());
1145 // Is there an indirect jump with jump table?
1146 bool ChangedIndirectJump
= false;
1147 int JTI
= findJumpTableIndex(*this);
1149 MachineJumpTableInfo
&MJTI
= *MF
->getJumpTableInfo();
1150 MJTI
.ReplaceMBBInJumpTable(JTI
, Succ
, NMBB
);
1151 ChangedIndirectJump
= true;
1154 MF
->insert(std::next(MachineFunction::iterator(this)), NMBB
);
1155 LLVM_DEBUG(dbgs() << "Splitting critical edge: " << printMBBReference(*this)
1156 << " -- " << printMBBReference(*NMBB
) << " -- "
1157 << printMBBReference(*Succ
) << '\n');
1159 LiveIntervals
*LIS
= P
.getAnalysisIfAvailable
<LiveIntervals
>();
1160 SlotIndexes
*Indexes
= P
.getAnalysisIfAvailable
<SlotIndexes
>();
1162 LIS
->insertMBBInMaps(NMBB
);
1164 Indexes
->insertMBBInMaps(NMBB
);
1166 // On some targets like Mips, branches may kill virtual registers. Make sure
1167 // that LiveVariables is properly updated after updateTerminator replaces the
1169 LiveVariables
*LV
= P
.getAnalysisIfAvailable
<LiveVariables
>();
1171 // Collect a list of virtual registers killed by the terminators.
1172 SmallVector
<Register
, 4> KilledRegs
;
1174 for (MachineInstr
&MI
:
1175 llvm::make_range(getFirstInstrTerminator(), instr_end())) {
1176 for (MachineOperand
&MO
: MI
.all_uses()) {
1177 if (MO
.getReg() == 0 || !MO
.isKill() || MO
.isUndef())
1179 Register Reg
= MO
.getReg();
1180 if (Reg
.isPhysical() || LV
->getVarInfo(Reg
).removeKill(MI
)) {
1181 KilledRegs
.push_back(Reg
);
1182 LLVM_DEBUG(dbgs() << "Removing terminator kill: " << MI
);
1183 MO
.setIsKill(false);
1188 SmallVector
<Register
, 4> UsedRegs
;
1190 for (MachineInstr
&MI
:
1191 llvm::make_range(getFirstInstrTerminator(), instr_end())) {
1192 for (const MachineOperand
&MO
: MI
.operands()) {
1193 if (!MO
.isReg() || MO
.getReg() == 0)
1196 Register Reg
= MO
.getReg();
1197 if (!is_contained(UsedRegs
, Reg
))
1198 UsedRegs
.push_back(Reg
);
1203 ReplaceUsesOfBlockWith(Succ
, NMBB
);
1205 // Since we replaced all uses of Succ with NMBB, that should also be treated
1206 // as the fallthrough successor
1207 if (Succ
== PrevFallthrough
)
1208 PrevFallthrough
= NMBB
;
1210 if (!ChangedIndirectJump
) {
1211 SlotIndexUpdateDelegate
SlotUpdater(*MF
, Indexes
);
1212 updateTerminator(PrevFallthrough
);
1215 // Insert unconditional "jump Succ" instruction in NMBB if necessary.
1216 NMBB
->addSuccessor(Succ
);
1217 if (!NMBB
->isLayoutSuccessor(Succ
)) {
1218 SlotIndexUpdateDelegate
SlotUpdater(*MF
, Indexes
);
1219 SmallVector
<MachineOperand
, 4> Cond
;
1220 const TargetInstrInfo
*TII
= getParent()->getSubtarget().getInstrInfo();
1221 TII
->insertBranch(*NMBB
, Succ
, nullptr, Cond
, DL
);
1224 // Fix PHI nodes in Succ so they refer to NMBB instead of this.
1225 Succ
->replacePhiUsesWith(this, NMBB
);
1227 // Inherit live-ins from the successor
1228 for (const auto &LI
: Succ
->liveins())
1229 NMBB
->addLiveIn(LI
);
1231 // Update LiveVariables.
1232 const TargetRegisterInfo
*TRI
= MF
->getSubtarget().getRegisterInfo();
1234 // Restore kills of virtual registers that were killed by the terminators.
1235 while (!KilledRegs
.empty()) {
1236 Register Reg
= KilledRegs
.pop_back_val();
1237 for (instr_iterator I
= instr_end(), E
= instr_begin(); I
!= E
;) {
1238 if (!(--I
)->addRegisterKilled(Reg
, TRI
, /* AddIfNotFound= */ false))
1240 if (Reg
.isVirtual())
1241 LV
->getVarInfo(Reg
).Kills
.push_back(&*I
);
1242 LLVM_DEBUG(dbgs() << "Restored terminator kill: " << *I
);
1246 // Update relevant live-through information.
1247 if (LiveInSets
!= nullptr)
1248 LV
->addNewBlock(NMBB
, this, Succ
, *LiveInSets
);
1250 LV
->addNewBlock(NMBB
, this, Succ
);
1254 // After splitting the edge and updating SlotIndexes, live intervals may be
1255 // in one of two situations, depending on whether this block was the last in
1256 // the function. If the original block was the last in the function, all
1257 // live intervals will end prior to the beginning of the new split block. If
1258 // the original block was not at the end of the function, all live intervals
1259 // will extend to the end of the new split block.
1262 std::next(MachineFunction::iterator(NMBB
)) == getParent()->end();
1264 SlotIndex StartIndex
= Indexes
->getMBBEndIdx(this);
1265 SlotIndex PrevIndex
= StartIndex
.getPrevSlot();
1266 SlotIndex EndIndex
= Indexes
->getMBBEndIdx(NMBB
);
1268 // Find the registers used from NMBB in PHIs in Succ.
1269 SmallSet
<Register
, 8> PHISrcRegs
;
1270 for (MachineBasicBlock::instr_iterator
1271 I
= Succ
->instr_begin(), E
= Succ
->instr_end();
1272 I
!= E
&& I
->isPHI(); ++I
) {
1273 for (unsigned ni
= 1, ne
= I
->getNumOperands(); ni
!= ne
; ni
+= 2) {
1274 if (I
->getOperand(ni
+1).getMBB() == NMBB
) {
1275 MachineOperand
&MO
= I
->getOperand(ni
);
1276 Register Reg
= MO
.getReg();
1277 PHISrcRegs
.insert(Reg
);
1281 LiveInterval
&LI
= LIS
->getInterval(Reg
);
1282 VNInfo
*VNI
= LI
.getVNInfoAt(PrevIndex
);
1284 "PHI sources should be live out of their predecessors.");
1285 LI
.addSegment(LiveInterval::Segment(StartIndex
, EndIndex
, VNI
));
1286 for (auto &SR
: LI
.subranges())
1287 SR
.addSegment(LiveInterval::Segment(StartIndex
, EndIndex
, VNI
));
1292 MachineRegisterInfo
*MRI
= &getParent()->getRegInfo();
1293 for (unsigned i
= 0, e
= MRI
->getNumVirtRegs(); i
!= e
; ++i
) {
1294 Register Reg
= Register::index2VirtReg(i
);
1295 if (PHISrcRegs
.count(Reg
) || !LIS
->hasInterval(Reg
))
1298 LiveInterval
&LI
= LIS
->getInterval(Reg
);
1299 if (!LI
.liveAt(PrevIndex
))
1302 bool isLiveOut
= LI
.liveAt(LIS
->getMBBStartIdx(Succ
));
1303 if (isLiveOut
&& isLastMBB
) {
1304 VNInfo
*VNI
= LI
.getVNInfoAt(PrevIndex
);
1305 assert(VNI
&& "LiveInterval should have VNInfo where it is live.");
1306 LI
.addSegment(LiveInterval::Segment(StartIndex
, EndIndex
, VNI
));
1307 // Update subranges with live values
1308 for (auto &SR
: LI
.subranges()) {
1309 VNInfo
*VNI
= SR
.getVNInfoAt(PrevIndex
);
1311 SR
.addSegment(LiveInterval::Segment(StartIndex
, EndIndex
, VNI
));
1313 } else if (!isLiveOut
&& !isLastMBB
) {
1314 LI
.removeSegment(StartIndex
, EndIndex
);
1315 for (auto &SR
: LI
.subranges())
1316 SR
.removeSegment(StartIndex
, EndIndex
);
1320 // Update all intervals for registers whose uses may have been modified by
1321 // updateTerminator().
1322 LIS
->repairIntervalsInRange(this, getFirstTerminator(), end(), UsedRegs
);
1325 if (MachineDominatorTree
*MDT
=
1326 P
.getAnalysisIfAvailable
<MachineDominatorTree
>())
1327 MDT
->recordSplitCriticalEdge(this, Succ
, NMBB
);
1329 if (MachineLoopInfo
*MLI
= P
.getAnalysisIfAvailable
<MachineLoopInfo
>())
1330 if (MachineLoop
*TIL
= MLI
->getLoopFor(this)) {
1331 // If one or the other blocks were not in a loop, the new block is not
1332 // either, and thus LI doesn't need to be updated.
1333 if (MachineLoop
*DestLoop
= MLI
->getLoopFor(Succ
)) {
1334 if (TIL
== DestLoop
) {
1335 // Both in the same loop, the NMBB joins loop.
1336 DestLoop
->addBasicBlockToLoop(NMBB
, MLI
->getBase());
1337 } else if (TIL
->contains(DestLoop
)) {
1338 // Edge from an outer loop to an inner loop. Add to the outer loop.
1339 TIL
->addBasicBlockToLoop(NMBB
, MLI
->getBase());
1340 } else if (DestLoop
->contains(TIL
)) {
1341 // Edge from an inner loop to an outer loop. Add to the outer loop.
1342 DestLoop
->addBasicBlockToLoop(NMBB
, MLI
->getBase());
1344 // Edge from two loops with no containment relation. Because these
1345 // are natural loops, we know that the destination block must be the
1346 // header of its loop (adding a branch into a loop elsewhere would
1347 // create an irreducible loop).
1348 assert(DestLoop
->getHeader() == Succ
&&
1349 "Should not create irreducible loops!");
1350 if (MachineLoop
*P
= DestLoop
->getParentLoop())
1351 P
->addBasicBlockToLoop(NMBB
, MLI
->getBase());
1359 bool MachineBasicBlock::canSplitCriticalEdge(
1360 const MachineBasicBlock
*Succ
) const {
1361 // Splitting the critical edge to a landing pad block is non-trivial. Don't do
1362 // it in this generic function.
1363 if (Succ
->isEHPad())
1366 // Splitting the critical edge to a callbr's indirect block isn't advised.
1367 // Don't do it in this generic function.
1368 if (Succ
->isInlineAsmBrIndirectTarget())
1371 const MachineFunction
*MF
= getParent();
1372 // Performance might be harmed on HW that implements branching using exec mask
1373 // where both sides of the branches are always executed.
1374 if (MF
->getTarget().requiresStructuredCFG())
1377 // Do we have an Indirect jump with a jumptable that we can rewrite?
1378 int JTI
= findJumpTableIndex(*this);
1379 if (JTI
>= 0 && !jumpTableHasOtherUses(*MF
, *this, JTI
))
1382 // We may need to update this's terminator, but we can't do that if
1383 // analyzeBranch fails.
1384 const TargetInstrInfo
*TII
= MF
->getSubtarget().getInstrInfo();
1385 MachineBasicBlock
*TBB
= nullptr, *FBB
= nullptr;
1386 SmallVector
<MachineOperand
, 4> Cond
;
1387 // AnalyzeBanch should modify this, since we did not allow modification.
1388 if (TII
->analyzeBranch(*const_cast<MachineBasicBlock
*>(this), TBB
, FBB
, Cond
,
1389 /*AllowModify*/ false))
1392 // Avoid bugpoint weirdness: A block may end with a conditional branch but
1393 // jumps to the same MBB is either case. We have duplicate CFG edges in that
1394 // case that we can't handle. Since this never happens in properly optimized
1395 // code, just skip those edges.
1396 if (TBB
&& TBB
== FBB
) {
1397 LLVM_DEBUG(dbgs() << "Won't split critical edge after degenerate "
1398 << printMBBReference(*this) << '\n');
1404 /// Prepare MI to be removed from its bundle. This fixes bundle flags on MI's
1405 /// neighboring instructions so the bundle won't be broken by removing MI.
1406 static void unbundleSingleMI(MachineInstr
*MI
) {
1407 // Removing the first instruction in a bundle.
1408 if (MI
->isBundledWithSucc() && !MI
->isBundledWithPred())
1409 MI
->unbundleFromSucc();
1410 // Removing the last instruction in a bundle.
1411 if (MI
->isBundledWithPred() && !MI
->isBundledWithSucc())
1412 MI
->unbundleFromPred();
1413 // If MI is not bundled, or if it is internal to a bundle, the neighbor flags
1414 // are already fine.
1417 MachineBasicBlock::instr_iterator
1418 MachineBasicBlock::erase(MachineBasicBlock::instr_iterator I
) {
1419 unbundleSingleMI(&*I
);
1420 return Insts
.erase(I
);
1423 MachineInstr
*MachineBasicBlock::remove_instr(MachineInstr
*MI
) {
1424 unbundleSingleMI(MI
);
1425 MI
->clearFlag(MachineInstr::BundledPred
);
1426 MI
->clearFlag(MachineInstr::BundledSucc
);
1427 return Insts
.remove(MI
);
1430 MachineBasicBlock::instr_iterator
1431 MachineBasicBlock::insert(instr_iterator I
, MachineInstr
*MI
) {
1432 assert(!MI
->isBundledWithPred() && !MI
->isBundledWithSucc() &&
1433 "Cannot insert instruction with bundle flags");
1434 // Set the bundle flags when inserting inside a bundle.
1435 if (I
!= instr_end() && I
->isBundledWithPred()) {
1436 MI
->setFlag(MachineInstr::BundledPred
);
1437 MI
->setFlag(MachineInstr::BundledSucc
);
1439 return Insts
.insert(I
, MI
);
1442 /// This method unlinks 'this' from the containing function, and returns it, but
1443 /// does not delete it.
1444 MachineBasicBlock
*MachineBasicBlock::removeFromParent() {
1445 assert(getParent() && "Not embedded in a function!");
1446 getParent()->remove(this);
1450 /// This method unlinks 'this' from the containing function, and deletes it.
1451 void MachineBasicBlock::eraseFromParent() {
1452 assert(getParent() && "Not embedded in a function!");
1453 getParent()->erase(this);
1456 /// Given a machine basic block that branched to 'Old', change the code and CFG
1457 /// so that it branches to 'New' instead.
1458 void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock
*Old
,
1459 MachineBasicBlock
*New
) {
1460 assert(Old
!= New
&& "Cannot replace self with self!");
1462 MachineBasicBlock::instr_iterator I
= instr_end();
1463 while (I
!= instr_begin()) {
1465 if (!I
->isTerminator()) break;
1467 // Scan the operands of this machine instruction, replacing any uses of Old
1469 for (unsigned i
= 0, e
= I
->getNumOperands(); i
!= e
; ++i
)
1470 if (I
->getOperand(i
).isMBB() &&
1471 I
->getOperand(i
).getMBB() == Old
)
1472 I
->getOperand(i
).setMBB(New
);
1475 // Update the successor information.
1476 replaceSuccessor(Old
, New
);
1479 void MachineBasicBlock::replacePhiUsesWith(MachineBasicBlock
*Old
,
1480 MachineBasicBlock
*New
) {
1481 for (MachineInstr
&MI
: phis())
1482 for (unsigned i
= 2, e
= MI
.getNumOperands() + 1; i
!= e
; i
+= 2) {
1483 MachineOperand
&MO
= MI
.getOperand(i
);
1484 if (MO
.getMBB() == Old
)
1489 /// Find the next valid DebugLoc starting at MBBI, skipping any debug
1490 /// instructions. Return UnknownLoc if there is none.
1492 MachineBasicBlock::findDebugLoc(instr_iterator MBBI
) {
1493 // Skip debug declarations, we don't want a DebugLoc from them.
1494 MBBI
= skipDebugInstructionsForward(MBBI
, instr_end());
1495 if (MBBI
!= instr_end())
1496 return MBBI
->getDebugLoc();
1500 DebugLoc
MachineBasicBlock::rfindDebugLoc(reverse_instr_iterator MBBI
) {
1501 if (MBBI
== instr_rend())
1502 return findDebugLoc(instr_begin());
1503 // Skip debug declarations, we don't want a DebugLoc from them.
1504 MBBI
= skipDebugInstructionsBackward(MBBI
, instr_rbegin());
1505 if (!MBBI
->isDebugInstr())
1506 return MBBI
->getDebugLoc();
1510 /// Find the previous valid DebugLoc preceding MBBI, skipping any debug
1511 /// instructions. Return UnknownLoc if there is none.
1512 DebugLoc
MachineBasicBlock::findPrevDebugLoc(instr_iterator MBBI
) {
1513 if (MBBI
== instr_begin())
1515 // Skip debug instructions, we don't want a DebugLoc from them.
1516 MBBI
= prev_nodbg(MBBI
, instr_begin());
1517 if (!MBBI
->isDebugInstr())
1518 return MBBI
->getDebugLoc();
1522 DebugLoc
MachineBasicBlock::rfindPrevDebugLoc(reverse_instr_iterator MBBI
) {
1523 if (MBBI
== instr_rend())
1525 // Skip debug declarations, we don't want a DebugLoc from them.
1526 MBBI
= next_nodbg(MBBI
, instr_rend());
1527 if (MBBI
!= instr_rend())
1528 return MBBI
->getDebugLoc();
1532 /// Find and return the merged DebugLoc of the branch instructions of the block.
1533 /// Return UnknownLoc if there is none.
1535 MachineBasicBlock::findBranchDebugLoc() {
1537 auto TI
= getFirstTerminator();
1538 while (TI
!= end() && !TI
->isBranch())
1542 DL
= TI
->getDebugLoc();
1543 for (++TI
; TI
!= end() ; ++TI
)
1545 DL
= DILocation::getMergedLocation(DL
, TI
->getDebugLoc());
1550 /// Return probability of the edge from this block to MBB.
1552 MachineBasicBlock::getSuccProbability(const_succ_iterator Succ
) const {
1554 return BranchProbability(1, succ_size());
1556 const auto &Prob
= *getProbabilityIterator(Succ
);
1557 if (Prob
.isUnknown()) {
1558 // For unknown probabilities, collect the sum of all known ones, and evenly
1559 // ditribute the complemental of the sum to each unknown probability.
1560 unsigned KnownProbNum
= 0;
1561 auto Sum
= BranchProbability::getZero();
1562 for (const auto &P
: Probs
) {
1563 if (!P
.isUnknown()) {
1568 return Sum
.getCompl() / (Probs
.size() - KnownProbNum
);
1573 /// Set successor probability of a given iterator.
1574 void MachineBasicBlock::setSuccProbability(succ_iterator I
,
1575 BranchProbability Prob
) {
1576 assert(!Prob
.isUnknown());
1579 *getProbabilityIterator(I
) = Prob
;
1582 /// Return probability iterator corresonding to the I successor iterator
1583 MachineBasicBlock::const_probability_iterator
1584 MachineBasicBlock::getProbabilityIterator(
1585 MachineBasicBlock::const_succ_iterator I
) const {
1586 assert(Probs
.size() == Successors
.size() && "Async probability list!");
1587 const size_t index
= std::distance(Successors
.begin(), I
);
1588 assert(index
< Probs
.size() && "Not a current successor!");
1589 return Probs
.begin() + index
;
1592 /// Return probability iterator corresonding to the I successor iterator.
1593 MachineBasicBlock::probability_iterator
1594 MachineBasicBlock::getProbabilityIterator(MachineBasicBlock::succ_iterator I
) {
1595 assert(Probs
.size() == Successors
.size() && "Async probability list!");
1596 const size_t index
= std::distance(Successors
.begin(), I
);
1597 assert(index
< Probs
.size() && "Not a current successor!");
1598 return Probs
.begin() + index
;
1601 /// Return whether (physical) register "Reg" has been <def>ined and not <kill>ed
1602 /// as of just before "MI".
1604 /// Search is localised to a neighborhood of
1605 /// Neighborhood instructions before (searching for defs or kills) and N
1606 /// instructions after (searching just for defs) MI.
1607 MachineBasicBlock::LivenessQueryResult
1608 MachineBasicBlock::computeRegisterLiveness(const TargetRegisterInfo
*TRI
,
1609 MCRegister Reg
, const_iterator Before
,
1610 unsigned Neighborhood
) const {
1611 unsigned N
= Neighborhood
;
1613 // Try searching forwards from Before, looking for reads or defs.
1614 const_iterator
I(Before
);
1615 for (; I
!= end() && N
> 0; ++I
) {
1616 if (I
->isDebugOrPseudoInstr())
1621 PhysRegInfo Info
= AnalyzePhysRegInBundle(*I
, Reg
, TRI
);
1623 // Register is live when we read it here.
1626 // Register is dead if we can fully overwrite or clobber it here.
1627 if (Info
.FullyDefined
|| Info
.Clobbered
)
1631 // If we reached the end, it is safe to clobber Reg at the end of a block of
1632 // no successor has it live in.
1634 for (MachineBasicBlock
*S
: successors()) {
1635 for (const MachineBasicBlock::RegisterMaskPair
&LI
: S
->liveins()) {
1636 if (TRI
->regsOverlap(LI
.PhysReg
, Reg
))
1647 // Start by searching backwards from Before, looking for kills, reads or defs.
1648 I
= const_iterator(Before
);
1649 // If this is the first insn in the block, don't search backwards.
1654 if (I
->isDebugOrPseudoInstr())
1659 PhysRegInfo Info
= AnalyzePhysRegInBundle(*I
, Reg
, TRI
);
1661 // Defs happen after uses so they take precedence if both are present.
1663 // Register is dead after a dead def of the full register.
1666 // Register is (at least partially) live after a def.
1668 if (!Info
.PartialDeadDef
)
1670 // As soon as we saw a partial definition (dead or not),
1671 // we cannot tell if the value is partial live without
1672 // tracking the lanemasks. We are not going to do this,
1673 // so fall back on the remaining of the analysis.
1676 // Register is dead after a full kill or clobber and no def.
1677 if (Info
.Killed
|| Info
.Clobbered
)
1679 // Register must be live if we read it.
1683 } while (I
!= begin() && N
> 0);
1686 // If all the instructions before this in the block are debug instructions,
1688 while (I
!= begin() && std::prev(I
)->isDebugOrPseudoInstr())
1691 // Did we get to the start of the block?
1693 // If so, the register's state is definitely defined by the live-in state.
1694 for (const MachineBasicBlock::RegisterMaskPair
&LI
: liveins())
1695 if (TRI
->regsOverlap(LI
.PhysReg
, Reg
))
1701 // At this point we have no idea of the liveness of the register.
1706 MachineBasicBlock::getBeginClobberMask(const TargetRegisterInfo
*TRI
) const {
1707 // EH funclet entry does not preserve any registers.
1708 return isEHFuncletEntry() ? TRI
->getNoPreservedMask() : nullptr;
1712 MachineBasicBlock::getEndClobberMask(const TargetRegisterInfo
*TRI
) const {
1713 // If we see a return block with successors, this must be a funclet return,
1714 // which does not preserve any registers. If there are no successors, we don't
1715 // care what kind of return it is, putting a mask after it is a no-op.
1716 return isReturnBlock() && !succ_empty() ? TRI
->getNoPreservedMask() : nullptr;
1719 void MachineBasicBlock::clearLiveIns() {
1723 MachineBasicBlock::livein_iterator
MachineBasicBlock::livein_begin() const {
1724 assert(getParent()->getProperties().hasProperty(
1725 MachineFunctionProperties::Property::TracksLiveness
) &&
1726 "Liveness information is accurate");
1727 return LiveIns
.begin();
1730 MachineBasicBlock::liveout_iterator
MachineBasicBlock::liveout_begin() const {
1731 const MachineFunction
&MF
= *getParent();
1732 assert(MF
.getProperties().hasProperty(
1733 MachineFunctionProperties::Property::TracksLiveness
) &&
1734 "Liveness information is accurate");
1736 const TargetLowering
&TLI
= *MF
.getSubtarget().getTargetLowering();
1737 MCPhysReg ExceptionPointer
= 0, ExceptionSelector
= 0;
1738 if (MF
.getFunction().hasPersonalityFn()) {
1739 auto PersonalityFn
= MF
.getFunction().getPersonalityFn();
1740 ExceptionPointer
= TLI
.getExceptionPointerRegister(PersonalityFn
);
1741 ExceptionSelector
= TLI
.getExceptionSelectorRegister(PersonalityFn
);
1744 return liveout_iterator(*this, ExceptionPointer
, ExceptionSelector
, false);
1747 bool MachineBasicBlock::sizeWithoutDebugLargerThan(unsigned Limit
) const {
1749 auto R
= instructionsWithoutDebug(begin(), end());
1750 for (auto I
= R
.begin(), E
= R
.end(); I
!= E
; ++I
) {
1757 const MBBSectionID
MBBSectionID::ColdSectionID(MBBSectionID::SectionType::Cold
);
1759 MBBSectionID::ExceptionSectionID(MBBSectionID::SectionType::Exception
);