LLVM 24.0.0git
MachineBasicBlock.cpp
Go to the documentation of this file.
1//===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// Collect the sequence of machine instructions for a basic block.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/STLExtras.h"
30#include "llvm/Config/llvm-config.h"
31#include "llvm/IR/BasicBlock.h"
33#include "llvm/IR/Module.h"
35#include "llvm/MC/MCAsmInfo.h"
36#include "llvm/MC/MCContext.h"
37#include "llvm/Support/Debug.h"
40#include <algorithm>
41#include <cmath>
42using namespace llvm;
43
44#define DEBUG_TYPE "codegen"
45
47 "print-slotindexes",
48 cl::desc("When printing machine IR, annotate instructions and blocks with "
49 "SlotIndexes when available"),
50 cl::init(true), cl::Hidden);
51
52MachineBasicBlock::MachineBasicBlock(MachineFunction &MF, const BasicBlock *B)
53 : BB(B), Number(-1), xParent(&MF) {
54 Insts.Parent = this;
55 if (B)
56 IrrLoopHeaderWeight = B->getIrrLoopHeaderWeight();
57}
58
59MachineBasicBlock::~MachineBasicBlock() = default;
60
61/// Return the MCSymbol for this basic block.
63 if (!CachedMCSymbol) {
64 const MachineFunction *MF = getParent();
65 MCContext &Ctx = MF->getContext();
66
67 // We emit a non-temporary symbol -- with a descriptive name -- if it begins
68 // a section (with basic block sections). Otherwise we fall back to use temp
69 // label.
70 if (MF->hasBBSections() && isBeginSection()) {
71 SmallString<5> Suffix;
72 if (SectionID == MBBSectionID::ColdSectionID) {
73 Suffix += ".cold";
74 } else if (SectionID == MBBSectionID::ExceptionSectionID) {
75 Suffix += ".eh";
76 } else {
77 // For symbols that represent basic block sections, we add ".__part." to
78 // allow tools like symbolizers to know that this represents a part of
79 // the original function.
80 Suffix = (Suffix + Twine(".__part.") + Twine(SectionID.Number)).str();
81 }
82 CachedMCSymbol = Ctx.getOrCreateSymbol(MF->getName() + Suffix);
83 } else {
84 // If the block occurs as label in inline assembly, parsing the assembly
85 // needs an actual label name => set AlwaysEmit in these cases.
86 CachedMCSymbol = Ctx.createBlockSymbol(
87 "BB" + Twine(MF->getFunctionNumber()) + "_" + Twine(getNumber()),
88 /*AlwaysEmit=*/hasLabelMustBeEmitted());
89 }
90 }
91 return CachedMCSymbol;
92}
93
95 if (!CachedEHContMCSymbol) {
96 const MachineFunction *MF = getParent();
97 SmallString<128> SymbolName;
98 raw_svector_ostream(SymbolName)
99 << "$ehgcr_" << MF->getFunctionNumber() << '_' << getNumber();
100 CachedEHContMCSymbol = MF->getContext().getOrCreateSymbol(SymbolName);
101 }
102 return CachedEHContMCSymbol;
103}
104
106 if (!CachedEndMCSymbol) {
107 const MachineFunction *MF = getParent();
108 MCContext &Ctx = MF->getContext();
109 CachedEndMCSymbol = Ctx.createBlockSymbol(
110 "BB_END" + Twine(MF->getFunctionNumber()) + "_" + Twine(getNumber()),
111 /*AlwaysEmit=*/false);
112 }
113 return CachedEndMCSymbol;
114}
115
117 MBB.print(OS);
118 return OS;
119}
120
122 return Printable([&MBB](raw_ostream &OS) { return MBB.printAsOperand(OS); });
123}
124
125/// When an MBB is added to an MF, we need to update the parent pointer of the
126/// MBB, the MBB numbering, and any instructions in the MBB to be on the right
127/// operand list for registers.
128///
129/// MBBs start out as #-1. When a MBB is added to a MachineFunction, it
130/// gets the next available unique MBB number. If it is removed from a
131/// MachineFunction, it goes back to being #-1.
134 MachineFunction &MF = *N->getParent();
135 N->Number = MF.addToMBBNumbering(N);
136 N->AnalysisNumber = MF.assignAnalysisNumber();
137
138 // Make sure the instructions have their operands in the reginfo lists.
140 for (MachineInstr &MI : N->instrs())
141 MI.addRegOperandsToUseLists(RegInfo);
142}
143
146 N->getParent()->removeFromMBBNumbering(N->Number);
147 N->Number = -1;
148 N->AnalysisNumber = -1;
149}
150
151/// When we add an instruction to a basic block list, we update its parent
152/// pointer and add its operands from reg use/def lists if appropriate.
154 assert(!N->getParent() && "machine instruction already in a basic block");
155 N->setParent(Parent);
156
157 // Add the instruction's register operands to their corresponding
158 // use/def lists.
159 MachineFunction *MF = Parent->getParent();
160 N->addRegOperandsToUseLists(MF->getRegInfo());
161 MF->handleInsertion(*N);
162}
163
164/// When we remove an instruction from a basic block list, we update its parent
165/// pointer and remove its operands from reg use/def lists if appropriate.
167 assert(N->getParent() && "machine instruction not in a basic block");
168
169 // Remove from the use/def lists.
170 if (MachineFunction *MF = N->getMF()) {
171 MF->handleRemoval(*N);
172 N->removeRegOperandsFromUseLists(MF->getRegInfo());
173 }
174
175 N->setParent(nullptr);
176}
177
178/// When moving a range of instructions from one MBB list to another, we need to
179/// update the parent pointers and the use/def lists.
181 instr_iterator First,
182 instr_iterator Last) {
183 assert(Parent->getParent() == FromList.Parent->getParent() &&
184 "cannot transfer MachineInstrs between MachineFunctions");
185
186 // If it's within the same BB, there's nothing to do.
187 if (this == &FromList)
188 return;
189
190 assert(Parent != FromList.Parent && "Two lists have the same parent?");
191
192 // If splicing between two blocks within the same function, just update the
193 // parent pointers.
194 for (; First != Last; ++First)
195 First->setParent(Parent);
196}
197
199 assert(!MI->getParent() && "MI is still in a block!");
200 Parent->getParent()->deleteMachineInstr(MI);
201}
202
205 while (I != E && I->isPHI())
206 ++I;
207 assert((I == E || !I->isInsideBundle()) &&
208 "First non-phi MI cannot be inside a bundle!");
209 return I;
210}
211
215
216 iterator E = end();
217 while (I != E && (I->isPHI() || I->isPosition() ||
218 TII->isBasicBlockPrologue(*I)))
219 ++I;
220 // FIXME: This needs to change if we wish to bundle labels
221 // inside the bundle.
222 assert((I == E || !I->isInsideBundle()) &&
223 "First non-phi / non-label instruction is inside a bundle!");
224 return I;
225}
226
229 Register Reg, bool SkipPseudoOp) {
231
232 iterator E = end();
233 while (I != E && (I->isPHI() || I->isPosition() || I->isDebugInstr() ||
234 (SkipPseudoOp && I->isPseudoProbe()) ||
235 TII->isBasicBlockPrologue(*I, Reg)))
236 ++I;
237 // FIXME: This needs to change if we wish to bundle labels / dbg_values
238 // inside the bundle.
239 assert((I == E || !I->isInsideBundle()) &&
240 "First non-phi / non-label / non-debug "
241 "instruction is inside a bundle!");
242 return I;
243}
244
246 iterator B = begin(), E = end(), I = E;
247 while (I != B && ((--I)->isTerminator() || I->isDebugInstr()))
248 ; /*noop */
249 while (I != E && !I->isTerminator())
250 ++I;
251 return I;
252}
253
255 instr_iterator B = instr_begin(), E = instr_end(), I = E;
256 while (I != B && ((--I)->isTerminator() || I->isDebugInstr()))
257 ; /*noop */
258 while (I != E && !I->isTerminator())
259 ++I;
260 return I;
261}
262
264 return find_if(instrs(), [](auto &II) { return II.isTerminator(); });
265}
266
269 // Skip over begin-of-block dbg_value instructions.
270 return skipDebugInstructionsForward(begin(), end(), SkipPseudoOp);
271}
272
275 // Skip over end-of-block dbg_value instructions.
277 while (I != B) {
278 --I;
279 // Return instruction that starts a bundle.
280 if (I->isDebugInstr() || I->isInsideBundle())
281 continue;
282 if (SkipPseudoOp && I->isPseudoProbe())
283 continue;
284 return I;
285 }
286 // The block is all debug values.
287 return end();
288}
289
291 for (const MachineBasicBlock *Succ : successors())
292 if (Succ->isEHPad())
293 return true;
294 return false;
295}
296
298 return getParent()->begin() == getIterator();
299}
300
301#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
305#endif
306
308 for (const MachineBasicBlock *Succ : successors()) {
309 if (Succ->isInlineAsmBrIndirectTarget())
310 return true;
311 }
312 return false;
313}
314
317 return false;
318 return true;
319}
320
322 if (const BasicBlock *LBB = getBasicBlock())
323 return LBB->hasName();
324 return false;
325}
326
328 if (const BasicBlock *LBB = getBasicBlock())
329 return LBB->getName();
330 else
331 return StringRef("", 0);
332}
333
334/// Return a hopefully unique identifier for this block.
336 std::string Name;
337 if (getParent())
338 Name = (getParent()->getName() + ":").str();
339 if (getBasicBlock())
340 Name += getBasicBlock()->getName();
341 else
342 Name += ("BB" + Twine(getNumber())).str();
343 return Name;
344}
345
347 bool IsStandalone) const {
348 const MachineFunction *MF = getParent();
349 if (!MF) {
350 OS << "Can't print out MachineBasicBlock because parent MachineFunction"
351 << " is null\n";
352 return;
353 }
354 const Function &F = MF->getFunction();
355 const Module *M = F.getParent();
356 ModuleSlotTracker MST(M);
358 print(OS, MST, Indexes, IsStandalone);
359}
360
362 const SlotIndexes *Indexes,
363 bool IsStandalone) const {
364 const MachineFunction *MF = getParent();
365 if (!MF) {
366 OS << "Can't print out MachineBasicBlock because parent MachineFunction"
367 << " is null\n";
368 return;
369 }
370
371 if (Indexes && PrintSlotIndexes)
372 OS << Indexes->getMBBStartIdx(this) << '\t';
373
375 OS << ":\n";
376
378 const MachineRegisterInfo &MRI = MF->getRegInfo();
380 bool HasLineAttributes = false;
381
382 // Print the preds of this block according to the CFG.
383 if (!pred_empty() && IsStandalone) {
384 if (Indexes) OS << '\t';
385 // Don't indent(2), align with previous line attributes.
386 OS << "; predecessors: ";
387 ListSeparator LS;
388 for (auto *Pred : predecessors())
389 OS << LS << printMBBReference(*Pred);
390 OS << '\n';
391 HasLineAttributes = true;
392 }
393
394 if (!succ_empty()) {
395 if (Indexes) OS << '\t';
396 // Print the successors
397 OS.indent(2) << "successors: ";
398 ListSeparator LS;
399 for (auto I = succ_begin(), E = succ_end(); I != E; ++I) {
400 OS << LS << printMBBReference(**I);
401 if (!Probs.empty())
402 OS << '('
403 << format("0x%08" PRIx32, getSuccProbability(I).getNumerator())
404 << ')';
405 }
406 if (!Probs.empty() && IsStandalone) {
407 // Print human readable probabilities as comments.
408 OS << "; ";
409 ListSeparator LS;
410 for (auto I = succ_begin(), E = succ_end(); I != E; ++I) {
412 OS << LS << printMBBReference(**I) << '('
413 << format("%.2f%%",
414 rint(((double)BP.getNumerator() / BP.getDenominator()) *
415 100.0 * 100.0) /
416 100.0)
417 << ')';
418 }
419 }
420
421 OS << '\n';
422 HasLineAttributes = true;
423 }
424
425 if (!livein_empty() && MRI.tracksLiveness()) {
426 if (Indexes) OS << '\t';
427 OS.indent(2) << "liveins: ";
428
429 ListSeparator LS;
430 for (const auto &LI : liveins()) {
431 OS << LS << printReg(LI.PhysReg, TRI);
432 if (!LI.LaneMask.all())
433 OS << ":0x" << PrintLaneMask(LI.LaneMask);
434 }
435 HasLineAttributes = true;
436 }
437
438 if (HasLineAttributes)
439 OS << '\n';
440
441 bool IsInBundle = false;
442 for (const MachineInstr &MI : instrs()) {
443 if (Indexes && PrintSlotIndexes) {
444 if (Indexes->hasIndex(MI))
445 OS << Indexes->getInstructionIndex(MI);
446 OS << '\t';
447 }
448
449 if (IsInBundle && !MI.isInsideBundle()) {
450 OS.indent(2) << "}\n";
451 IsInBundle = false;
452 }
453
454 OS.indent(IsInBundle ? 4 : 2);
455 MI.print(OS, MST, IsStandalone, /*SkipOpers=*/false, /*SkipDebugLoc=*/false,
456 /*AddNewLine=*/false, &TII);
457
458 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
459 OS << " {";
460 IsInBundle = true;
461 }
462 OS << '\n';
463 }
464
465 if (IsInBundle)
466 OS.indent(2) << "}\n";
467
468 if (IrrLoopHeaderWeight && IsStandalone) {
469 if (Indexes) OS << '\t';
470 OS.indent(2) << "; Irreducible loop header weight: " << *IrrLoopHeaderWeight
471 << '\n';
472 }
473}
474
475/// Print the basic block's name as:
476///
477/// bb.{number}[.{ir-name}] [(attributes...)]
478///
479/// The {ir-name} is only printed when the \ref PrintNameIr flag is passed
480/// (which is the default). If the IR block has no name, it is identified
481/// numerically using the attribute syntax as "(%ir-block.{ir-slot})".
482///
483/// When the \ref PrintNameAttributes flag is passed, additional attributes
484/// of the block are printed when set.
485///
486/// \param printNameFlags Combination of \ref PrintNameFlag flags indicating
487/// the parts to print.
488/// \param moduleSlotTracker Optional ModuleSlotTracker. This method will
489/// incorporate its own tracker when necessary to
490/// determine the block's IR name.
491void MachineBasicBlock::printName(raw_ostream &os, unsigned printNameFlags,
492 ModuleSlotTracker *moduleSlotTracker) const {
493 os << "bb." << getNumber();
494 bool hasAttributes = false;
495
496 auto PrintBBRef = [&](const BasicBlock *bb) {
497 os << "%ir-block.";
498 if (bb->hasName()) {
499 printLLVMNameWithoutPrefix(os, bb->getName());
500 } else {
501 int slot = -1;
502
503 if (moduleSlotTracker) {
504 slot = moduleSlotTracker->getLocalSlot(bb);
505 } else if (bb->getParent()) {
506 ModuleSlotTracker tmpTracker(bb->getModule());
507 tmpTracker.incorporateFunction(*bb->getParent());
508 slot = tmpTracker.getLocalSlot(bb);
509 }
510
511 if (slot == -1)
512 os << "<ir-block badref>";
513 else
514 os << slot;
515 }
516 };
517
518 if (printNameFlags & PrintNameIr) {
519 if (const auto *bb = getBasicBlock()) {
520 if (bb->hasName()) {
521 // Quote if not a plain identifier, or the MIR cannot be parsed back.
522 os << '.';
523 printLLVMNameWithoutPrefix(os, bb->getName());
524 } else {
525 hasAttributes = true;
526 os << " (";
527 PrintBBRef(bb);
528 }
529 }
530 }
531
532 if (printNameFlags & PrintNameAttributes) {
534 os << (hasAttributes ? ", " : " (");
535 os << "machine-block-address-taken";
536 hasAttributes = true;
537 }
538 if (isIRBlockAddressTaken()) {
539 os << (hasAttributes ? ", " : " (");
540 os << "ir-block-address-taken ";
541 PrintBBRef(getAddressTakenIRBlock());
542 hasAttributes = true;
543 }
544 if (isEHPad()) {
545 os << (hasAttributes ? ", " : " (");
546 os << "landing-pad";
547 hasAttributes = true;
548 }
550 os << (hasAttributes ? ", " : " (");
551 os << "inlineasm-br-indirect-target";
552 hasAttributes = true;
553 }
554 if (isEHFuncletEntry()) {
555 os << (hasAttributes ? ", " : " (");
556 os << "ehfunclet-entry";
557 hasAttributes = true;
558 }
559 if (isEHScopeEntry()) {
560 os << (hasAttributes ? ", " : " (");
561 os << "ehscope-entry";
562 hasAttributes = true;
563 }
564 if (isCleanupFuncletEntry()) {
565 os << (hasAttributes ? ", " : " (");
566 os << "cleanup-funclet-entry";
567 hasAttributes = true;
568 }
569 if (isEHContTarget()) {
570 os << (hasAttributes ? ", " : " (");
571 os << "ehcont-target";
572 hasAttributes = true;
573 }
574 if (getAlignment() != Align(1)) {
575 os << (hasAttributes ? ", " : " (");
576 os << "align " << getAlignment().value();
577 hasAttributes = true;
579 os << ", max-bytes-for-alignment " << getMaxBytesForAlignment();
580 }
581 if (getSectionID() != MBBSectionID(0)) {
582 os << (hasAttributes ? ", " : " (");
583 os << "bbsections ";
584 switch (getSectionID().Type) {
586 os << "Exception";
587 break;
589 os << "Cold";
590 break;
591 default:
592 os << getSectionID().Number;
593 }
594 hasAttributes = true;
595 }
596 if (getBBID().has_value()) {
597 os << (hasAttributes ? ", " : " (");
598 os << "bb_id " << getBBID()->BaseID;
599 if (getBBID()->CloneID != 0)
600 os << " " << getBBID()->CloneID;
601 hasAttributes = true;
602 }
603 if (CallFrameSize != 0) {
604 os << (hasAttributes ? ", " : " (");
605 os << "call-frame-size " << CallFrameSize;
606 hasAttributes = true;
607 }
608 }
609
610 if (hasAttributes)
611 os << ')';
612}
613
615 bool /*PrintType*/) const {
616 OS << '%';
617 printName(OS, 0);
618}
619
621 assert(Reg.isPhysical());
622 LiveInVector::iterator I = find_if(
623 LiveIns, [Reg](const RegisterMaskPair &LI) { return LI.PhysReg == Reg; });
624 if (I == LiveIns.end())
625 return;
626
627 I->LaneMask &= ~LaneMask;
628 if (I->LaneMask.none())
629 LiveIns.erase(I);
630}
631
633 const MachineFunction *MF = getParent();
635 // Remove Reg and its subregs from live in set.
636 for (MCPhysReg S : TRI->subregs_inclusive(Reg))
637 removeLiveIn(S);
638
639 // Remove live-in bitmask in super registers as well.
640 for (MCPhysReg Super : TRI->superregs(Reg)) {
641 for (MCSubRegIndexIterator SRI(Super, TRI); SRI.isValid(); ++SRI) {
642 if (Reg == SRI.getSubReg()) {
643 unsigned SubRegIndex = SRI.getSubRegIndex();
644 LaneBitmask SubRegLaneMask = TRI->getSubRegIndexLaneMask(SubRegIndex);
645 removeLiveIn(Super, SubRegLaneMask);
646 break;
647 }
648 }
649 }
650}
651
654 // Get non-const version of iterator.
655 LiveInVector::iterator LI = LiveIns.begin() + (I - LiveIns.begin());
656 return LiveIns.erase(LI);
657}
658
660 assert(Reg.isPhysical());
662 LiveIns, [Reg](const RegisterMaskPair &LI) { return LI.PhysReg == Reg; });
663 return I != livein_end() && (I->LaneMask & LaneMask).any();
664}
665
667 MCRegister Reg, const TargetRegisterInfo &TRI) const {
668 return any_of(LiveIns, [&](const RegisterMaskPair &LI) {
669 if (!TRI.regsOverlap(Reg, LI.PhysReg))
670 return false;
671 if (LI.LaneMask.all())
672 return true;
673 for (MCRegUnitMaskIterator U(LI.PhysReg, &TRI); U.isValid(); ++U) {
674 auto [Unit, UnitMask] = *U;
675 if ((UnitMask & LI.LaneMask).any() && TRI.hasRegUnit(Reg, Unit))
676 return true;
677 }
678 return false;
679 });
680}
681
683 llvm::sort(LiveIns,
684 [](const RegisterMaskPair &LI0, const RegisterMaskPair &LI1) {
685 return LI0.PhysReg < LI1.PhysReg;
686 });
687 // Liveins are sorted by physreg now we can merge their lanemasks.
688 LiveInVector::const_iterator I = LiveIns.begin();
689 LiveInVector::const_iterator J;
690 LiveInVector::iterator Out = LiveIns.begin();
691 for (; I != LiveIns.end(); ++Out, I = J) {
692 MCRegister PhysReg = I->PhysReg;
693 LaneBitmask LaneMask = I->LaneMask;
694 for (J = std::next(I); J != LiveIns.end() && J->PhysReg == PhysReg; ++J)
695 LaneMask |= J->LaneMask;
696 Out->PhysReg = PhysReg;
697 Out->LaneMask = LaneMask;
698 }
699 LiveIns.erase(Out, LiveIns.end());
700}
701
704 assert(getParent() && "MBB must be inserted in function");
705 assert(PhysReg.isPhysical() && "Expected physreg");
706 assert(RC && "Register class is required");
707 assert((isEHPad() || this == &getParent()->front()) &&
708 "Only the entry block and landing pads can have physreg live ins");
709
710 bool LiveIn = isLiveIn(PhysReg);
714
715 // Look for an existing copy.
716 if (LiveIn)
717 for (;I != E && I->isCopy(); ++I)
718 if (I->getOperand(1).getReg() == PhysReg) {
719 Register VirtReg = I->getOperand(0).getReg();
720 if (!MRI.constrainRegClass(VirtReg, RC))
721 llvm_unreachable("Incompatible live-in register class.");
722 return VirtReg;
723 }
724
725 // No luck, create a virtual register.
726 Register VirtReg = MRI.createVirtualRegister(RC);
727 BuildMI(*this, I, DebugLoc(), TII.get(TargetOpcode::COPY), VirtReg)
728 .addReg(PhysReg, RegState::Kill);
729 if (!LiveIn)
730 addLiveIn(PhysReg);
731 return VirtReg;
732}
733
734void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
735 getParent()->splice(NewAfter->getIterator(), getIterator());
736}
737
738void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) {
739 getParent()->splice(++NewBefore->getIterator(), getIterator());
740}
741
743 MachineBasicBlock::const_iterator TerminatorI = MBB.getFirstTerminator();
744 if (TerminatorI == MBB.end())
745 return -1;
746 const MachineInstr &Terminator = *TerminatorI;
747 const TargetInstrInfo *TII = MBB.getParent()->getSubtarget().getInstrInfo();
748 return TII->getJumpTableIndex(Terminator);
749}
750
752 MachineBasicBlock *PreviousLayoutSuccessor) {
753 LLVM_DEBUG(dbgs() << "Updating terminators on " << printMBBReference(*this)
754 << "\n");
755
757 // A block with no successors has no concerns with fall-through edges.
758 if (this->succ_empty())
759 return;
760
761 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
764 bool B = TII->analyzeBranch(*this, TBB, FBB, Cond);
765 (void) B;
766 assert(!B && "UpdateTerminators requires analyzable predecessors!");
767 if (Cond.empty()) {
768 if (TBB) {
769 // The block has an unconditional branch. If its successor is now its
770 // layout successor, delete the branch.
772 TII->removeBranch(*this);
773 } else {
774 // The block has an unconditional fallthrough, or the end of the block is
775 // unreachable.
776
777 // Unfortunately, whether the end of the block is unreachable is not
778 // immediately obvious; we must fall back to checking the successor list,
779 // and assuming that if the passed in block is in the succesor list and
780 // not an EHPad, it must be the intended target.
781 if (!PreviousLayoutSuccessor || !isSuccessor(PreviousLayoutSuccessor) ||
782 PreviousLayoutSuccessor->isEHPad())
783 return;
784
785 // If the unconditional successor block is not the current layout
786 // successor, insert a branch to jump to it.
787 if (!isLayoutSuccessor(PreviousLayoutSuccessor))
788 TII->insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL);
789 }
790 return;
791 }
792
793 if (FBB) {
794 // The block has a non-fallthrough conditional branch. If one of its
795 // successors is its layout successor, rewrite it to a fallthrough
796 // conditional branch.
797 if (isLayoutSuccessor(TBB)) {
798 if (TII->reverseBranchCondition(Cond))
799 return;
800 TII->removeBranch(*this);
801 TII->insertBranch(*this, FBB, nullptr, Cond, DL);
802 } else if (isLayoutSuccessor(FBB)) {
803 TII->removeBranch(*this);
804 TII->insertBranch(*this, TBB, nullptr, Cond, DL);
805 }
806 return;
807 }
808
809 // We now know we're going to fallthrough to PreviousLayoutSuccessor.
810 assert(PreviousLayoutSuccessor);
811 assert(!PreviousLayoutSuccessor->isEHPad());
812 assert(isSuccessor(PreviousLayoutSuccessor));
813
814 if (PreviousLayoutSuccessor == TBB) {
815 // We had a fallthrough to the same basic block as the conditional jump
816 // targets. Remove the conditional jump, leaving an unconditional
817 // fallthrough or an unconditional jump.
818 TII->removeBranch(*this);
819 if (!isLayoutSuccessor(TBB)) {
820 Cond.clear();
821 TII->insertBranch(*this, TBB, nullptr, Cond, DL);
822 }
823 return;
824 }
825
826 // The block has a fallthrough conditional branch.
827 if (isLayoutSuccessor(TBB)) {
828 if (TII->reverseBranchCondition(Cond)) {
829 // We can't reverse the condition, add an unconditional branch.
830 Cond.clear();
831 TII->insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL);
832 return;
833 }
834 TII->removeBranch(*this);
835 TII->insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL);
836 } else if (!isLayoutSuccessor(PreviousLayoutSuccessor)) {
837 TII->removeBranch(*this);
838 TII->insertBranch(*this, TBB, PreviousLayoutSuccessor, Cond, DL);
839 }
840}
841
843#ifndef NDEBUG
844 int64_t Sum = 0;
845 for (auto Prob : Probs)
846 Sum += Prob.getNumerator();
847 // Due to precision issue, we assume that the sum of probabilities is one if
848 // the difference between the sum of their numerators and the denominator is
849 // no greater than the number of successors.
850 assert((uint64_t)std::abs(Sum - BranchProbability::getDenominator()) <=
851 Probs.size() &&
852 "The sum of successors's probabilities exceeds one.");
853#endif // NDEBUG
854}
855
856void MachineBasicBlock::addSuccessor(MachineBasicBlock *Succ,
857 BranchProbability Prob) {
858 // Probability list is either empty (if successor list isn't empty, this means
859 // disabled optimization) or has the same size as successor list.
860 if (!(Probs.empty() && !Successors.empty()))
861 Probs.push_back(Prob);
862 Successors.push_back(Succ);
863 Succ->addPredecessor(this);
864}
865
866void MachineBasicBlock::addSuccessorWithoutProb(MachineBasicBlock *Succ) {
867 // We need to make sure probability list is either empty or has the same size
868 // of successor list. When this function is called, we can safely delete all
869 // probability in the list.
870 Probs.clear();
871 Successors.push_back(Succ);
872 Succ->addPredecessor(this);
873}
874
875void MachineBasicBlock::splitSuccessor(MachineBasicBlock *Old,
876 MachineBasicBlock *New,
877 bool NormalizeSuccProbs) {
878 succ_iterator OldI = llvm::find(successors(), Old);
879 assert(OldI != succ_end() && "Old is not a successor of this block!");
881 "New is already a successor of this block!");
882
883 // Add a new successor with equal probability as the original one. Note
884 // that we directly copy the probability using the iterator rather than
885 // getting a potentially synthetic probability computed when unknown. This
886 // preserves the probabilities as-is and then we can renormalize them and
887 // query them effectively afterward.
888 addSuccessor(New, Probs.empty() ? BranchProbability::getUnknown()
889 : *getProbabilityIterator(OldI));
890 if (NormalizeSuccProbs)
892}
893
894void MachineBasicBlock::removeSuccessor(MachineBasicBlock *Succ,
895 bool NormalizeSuccProbs) {
896 succ_iterator I = find(Successors, Succ);
897 removeSuccessor(I, NormalizeSuccProbs);
898}
899
902 assert(I != Successors.end() && "Not a current successor!");
903
904 // If probability list is empty it means we don't use it (disabled
905 // optimization).
906 if (!Probs.empty()) {
907 probability_iterator WI = getProbabilityIterator(I);
908 Probs.erase(WI);
909 if (NormalizeSuccProbs)
911 }
912
913 (*I)->removePredecessor(this);
914 return Successors.erase(I);
915}
916
917void MachineBasicBlock::replaceSuccessor(MachineBasicBlock *Old,
918 MachineBasicBlock *New) {
919 if (Old == New)
920 return;
921
923 succ_iterator NewI = E;
924 succ_iterator OldI = E;
925 for (succ_iterator I = succ_begin(); I != E; ++I) {
926 if (*I == Old) {
927 OldI = I;
928 if (NewI != E)
929 break;
930 }
931 if (*I == New) {
932 NewI = I;
933 if (OldI != E)
934 break;
935 }
936 }
937 assert(OldI != E && "Old is not a successor of this block");
938
939 // If New isn't already a successor, let it take Old's place.
940 if (NewI == E) {
941 Old->removePredecessor(this);
942 New->addPredecessor(this);
943 *OldI = New;
944 return;
945 }
946
947 // New is already a successor.
948 // Update its probability instead of adding a duplicate edge.
949 if (!Probs.empty()) {
950 auto ProbIter = getProbabilityIterator(NewI);
951 if (!ProbIter->isUnknown())
952 *ProbIter += *getProbabilityIterator(OldI);
953 }
954 removeSuccessor(OldI);
955}
956
957void MachineBasicBlock::copySuccessor(const MachineBasicBlock *Orig,
959 if (!Orig->Probs.empty())
961 else
963}
964
965void MachineBasicBlock::addPredecessor(MachineBasicBlock *Pred) {
966 Predecessors.push_back(Pred);
967}
968
969void MachineBasicBlock::removePredecessor(MachineBasicBlock *Pred) {
970 // This is often called on many predecessors in reverse order.
971 // Do a reverse search and removal to avoid quadratic behavior in such cases.
972 auto RI = llvm::find(reverse(Predecessors), Pred);
973 assert(RI != Predecessors.rend() &&
974 "Pred is not a predecessor of this block!");
975 Predecessors.erase(std::prev(RI.base()));
976}
977
978void MachineBasicBlock::transferSuccessors(MachineBasicBlock *FromMBB) {
979 if (this == FromMBB)
980 return;
981
982 while (!FromMBB->succ_empty()) {
983 MachineBasicBlock *Succ = *FromMBB->succ_begin();
984
985 // If probability list is empty it means we don't use it (disabled
986 // optimization).
987 if (!FromMBB->Probs.empty()) {
988 auto Prob = *FromMBB->Probs.begin();
989 addSuccessor(Succ, Prob);
990 } else
992
993 FromMBB->removeSuccessor(Succ);
994 }
995}
996
997void
999 if (this == FromMBB)
1000 return;
1001
1002 while (!FromMBB->succ_empty()) {
1003 MachineBasicBlock *Succ = *FromMBB->succ_begin();
1004 if (!FromMBB->Probs.empty()) {
1005 auto Prob = *FromMBB->Probs.begin();
1006 addSuccessor(Succ, Prob);
1007 } else
1009 FromMBB->removeSuccessor(Succ);
1010
1011 // Fix up any PHI nodes in the successor.
1012 Succ->replacePhiUsesWith(FromMBB, this);
1013 }
1015}
1016
1017bool MachineBasicBlock::isPredecessor(const MachineBasicBlock *MBB) const {
1018 return is_contained(predecessors(), MBB);
1019}
1020
1021bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const {
1022 return is_contained(successors(), MBB);
1023}
1024
1025bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const {
1027 return std::next(I) == MachineFunction::const_iterator(MBB);
1028}
1029
1030const MachineBasicBlock *MachineBasicBlock::getSingleSuccessor() const {
1031 return Successors.size() == 1 ? Successors[0] : nullptr;
1032}
1033
1034const MachineBasicBlock *MachineBasicBlock::getSinglePredecessor() const {
1035 return Predecessors.size() == 1 ? Predecessors[0] : nullptr;
1036}
1037
1038MachineBasicBlock *MachineBasicBlock::getFallThrough(bool JumpToFallThrough) {
1039 MachineFunction::iterator Fallthrough = getIterator();
1040 ++Fallthrough;
1041 // If FallthroughBlock is off the end of the function, it can't fall through.
1042 if (Fallthrough == getParent()->end())
1043 return nullptr;
1044
1045 // If FallthroughBlock isn't a successor, no fallthrough is possible.
1046 if (!isSuccessor(&*Fallthrough))
1047 return nullptr;
1048
1049 // Analyze the branches, if any, at the end of the block.
1050 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
1053 if (TII->analyzeBranch(*this, TBB, FBB, Cond)) {
1054 // If we couldn't analyze the branch, examine the last instruction.
1055 // If the block doesn't end in a known control barrier, assume fallthrough
1056 // is possible. The isPredicated check is needed because this code can be
1057 // called during IfConversion, where an instruction which is normally a
1058 // Barrier is predicated and thus no longer an actual control barrier.
1059 return (empty() || !back().isBarrier() || TII->isPredicated(back()))
1060 ? &*Fallthrough
1061 : nullptr;
1062 }
1063
1064 // If there is no branch, control always falls through.
1065 if (!TBB) return &*Fallthrough;
1066
1067 // If there is some explicit branch to the fallthrough block, it can obviously
1068 // reach, even though the branch should get folded to fall through implicitly.
1069 if (JumpToFallThrough && (MachineFunction::iterator(TBB) == Fallthrough ||
1070 MachineFunction::iterator(FBB) == Fallthrough))
1071 return &*Fallthrough;
1072
1073 // If it's an unconditional branch to some block not the fall through, it
1074 // doesn't fall through.
1075 if (Cond.empty()) return nullptr;
1076
1077 // Otherwise, if it is conditional and has no explicit false block, it falls
1078 // through.
1079 return (FBB == nullptr) ? &*Fallthrough : nullptr;
1080}
1081
1083 return getFallThrough() != nullptr;
1084}
1085
1087 bool UpdateLiveIns,
1088 LiveIntervals *LIS) {
1089 MachineBasicBlock::iterator SplitPoint(&MI);
1090 ++SplitPoint;
1091
1092 if (SplitPoint == end()) {
1093 // Don't bother with a new block.
1094 return this;
1095 }
1096
1097 MachineFunction *MF = getParent();
1098
1100 if (UpdateLiveIns) {
1101 // Make sure we add any physregs we define in the block as liveins to the
1102 // new block.
1104 LiveRegs.init(*MF->getSubtarget().getRegisterInfo());
1105 LiveRegs.addLiveOuts(*this);
1106 for (auto I = rbegin(), E = Prev.getReverse(); I != E; ++I)
1107 LiveRegs.stepBackward(*I);
1108 }
1109
1110 MachineBasicBlock *SplitBB = MF->CreateMachineBasicBlock(getBasicBlock());
1111
1112 MF->insert(++MachineFunction::iterator(this), SplitBB);
1113 SplitBB->splice(SplitBB->begin(), this, SplitPoint, end());
1114
1115 SplitBB->transferSuccessorsAndUpdatePHIs(this);
1116 addSuccessor(SplitBB);
1117
1118 if (UpdateLiveIns)
1119 addLiveIns(*SplitBB, LiveRegs);
1120
1121 if (LIS)
1122 LIS->splitAt(*this, *SplitBB);
1123
1124 return SplitBB;
1125}
1126
1127// Returns `true` if there are possibly other users of the jump table at
1128// `JumpTableIndex` except for the ones in `IgnoreMBB`.
1130 const MachineBasicBlock &IgnoreMBB,
1131 int JumpTableIndex) {
1132 assert(JumpTableIndex >= 0 && "need valid index");
1133 const MachineJumpTableInfo &MJTI = *MF.getJumpTableInfo();
1134 const MachineJumpTableEntry &MJTE = MJTI.getJumpTables()[JumpTableIndex];
1135 // Take any basic block from the table; every user of the jump table must
1136 // show up in the predecessor list.
1137 const MachineBasicBlock *MBB = nullptr;
1138 for (MachineBasicBlock *B : MJTE.MBBs) {
1139 if (B != nullptr) {
1140 MBB = B;
1141 break;
1142 }
1143 }
1144 if (MBB == nullptr)
1145 return true; // can't rule out other users if there isn't any block.
1148 for (MachineBasicBlock *Pred : MBB->predecessors()) {
1149 if (Pred == &IgnoreMBB)
1150 continue;
1151 MachineBasicBlock *DummyT = nullptr;
1152 MachineBasicBlock *DummyF = nullptr;
1153 Cond.clear();
1154 if (!TII.analyzeBranch(*Pred, DummyT, DummyF, Cond,
1155 /*AllowModify=*/false)) {
1156 // analyzable direct jump
1157 continue;
1158 }
1159 int PredJTI = findJumpTableIndex(*Pred);
1160 if (PredJTI >= 0) {
1161 if (PredJTI == JumpTableIndex)
1162 return true;
1163 continue;
1164 }
1165 // Be conservative for unanalyzable jumps.
1166 return true;
1167 }
1168 return false;
1169}
1170
1172private:
1173 MachineFunction &MF;
1174 SlotIndexes *Indexes;
1176
1177public:
1179 : MF(MF), Indexes(Indexes) {
1180 MF.setDelegate(this);
1181 }
1182
1184 MF.resetDelegate(this);
1185 for (auto MI : Insertions)
1186 Indexes->insertMachineInstrInMaps(*MI);
1187 }
1188
1190 // This is called before MI is inserted into block so defer index update.
1191 if (Indexes)
1192 Insertions.insert(&MI);
1193 }
1194
1196 if (Indexes && !Insertions.remove(&MI))
1197 Indexes->removeMachineInstrFromMaps(MI);
1198 }
1199};
1200
1204 MachineDomTreeUpdater *MDTU) {
1205#define GET_RESULT(RESULT, GETTER, INFIX) \
1206 [MF, P, MFAM]() { \
1207 if (P) { \
1208 auto *Wrapper = P->getAnalysisIfAvailable<RESULT##INFIX##WrapperPass>(); \
1209 return Wrapper ? &Wrapper->GETTER() : nullptr; \
1210 } \
1211 return MFAM->getCachedResult<RESULT##Analysis>(*MF); \
1212 }()
1213
1214 assert((P || MFAM) && "Need a way to get analysis results!");
1215 MachineFunction *MF = getParent();
1216 LiveIntervals *LIS = GET_RESULT(LiveIntervals, getLIS, );
1217 SlotIndexes *Indexes = GET_RESULT(SlotIndexes, getSI, );
1218 MachineLoopInfo *MLI = GET_RESULT(MachineLoop, getLI, Info);
1219 return SplitCriticalEdge(Succ, {LIS, Indexes, MLI}, MDTU);
1220#undef GET_RESULT
1221}
1222
1225 const SplitCriticalEdgeAnalyses &Analyses,
1226 MachineDomTreeUpdater *MDTU) {
1227 if (!canSplitCriticalEdge(Succ, Analyses.MLI))
1228 return nullptr;
1229
1230 MachineFunction *MF = getParent();
1231 MachineBasicBlock *PrevFallthrough = getNextNode();
1232
1233 MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock();
1234 NMBB->setCallFrameSize(Succ->getCallFrameSize());
1235
1236 // Is there an indirect jump with jump table?
1237 bool ChangedIndirectJump = false;
1238 int JTI = findJumpTableIndex(*this);
1239 if (JTI >= 0) {
1241 MJTI.ReplaceMBBInJumpTable(JTI, Succ, NMBB);
1242 ChangedIndirectJump = true;
1243 }
1244
1245 MF->insert(std::next(MachineFunction::iterator(this)), NMBB);
1246 LLVM_DEBUG(dbgs() << "Splitting critical edge: " << printMBBReference(*this)
1247 << " -- " << printMBBReference(*NMBB) << " -- "
1248 << printMBBReference(*Succ) << '\n');
1249 auto *LIS = Analyses.LIS;
1250 if (LIS)
1251 LIS->insertMBBInMaps(NMBB);
1252 else if (Analyses.SI)
1253 Analyses.SI->insertMBBInMaps(NMBB);
1254
1255 SmallVector<Register, 4> UsedRegs;
1256 if (LIS) {
1257 for (MachineInstr &MI :
1259 for (const MachineOperand &MO : MI.operands()) {
1260 if (!MO.isReg() || MO.getReg() == 0)
1261 continue;
1262
1263 Register Reg = MO.getReg();
1264 if (!is_contained(UsedRegs, Reg))
1265 UsedRegs.push_back(Reg);
1266 }
1267 }
1268 }
1269
1270 ReplaceUsesOfBlockWith(Succ, NMBB);
1271
1272 // Since we replaced all uses of Succ with NMBB, that should also be treated
1273 // as the fallthrough successor
1274 if (Succ == PrevFallthrough)
1275 PrevFallthrough = NMBB;
1276 auto *Indexes = Analyses.SI;
1277 if (!ChangedIndirectJump) {
1278 SlotIndexUpdateDelegate SlotUpdater(*MF, Indexes);
1279 updateTerminator(PrevFallthrough);
1280 }
1281
1282 // Insert unconditional "jump Succ" instruction in NMBB if necessary.
1283 NMBB->addSuccessor(Succ);
1284 if (!NMBB->isLayoutSuccessor(Succ)) {
1285 SlotIndexUpdateDelegate SlotUpdater(*MF, Indexes);
1288
1289 // In original 'this' BB, there must be a branch instruction targeting at
1290 // Succ. We can not find it out since currently getBranchDestBlock was not
1291 // implemented for all targets. However, if the merged DL has column or line
1292 // number, the scope and non-zero column and line number is same with that
1293 // branch instruction so we can safely use it.
1294 DebugLoc DL, MergedDL = findBranchDebugLoc();
1295 if (MergedDL && (MergedDL.getLine() || MergedDL.getCol()))
1296 DL = MergedDL;
1297 TII->insertBranch(*NMBB, Succ, nullptr, Cond, DL);
1298 }
1299
1300 // Fix PHI nodes in Succ so they refer to NMBB instead of this, and find the
1301 // registers used from NMBB in them.
1302 SmallSet<Register, 8> PHISrcRegs;
1303 for (MachineInstr &MI : Succ->phis()) {
1304 for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) {
1305 MachineOperand &MBBOp = MI.getOperand(I + 1);
1306 if (MBBOp.getMBB() != this)
1307 continue;
1308 MBBOp.setMBB(NMBB);
1309 const MachineOperand &MO = MI.getOperand(I);
1310 if (LIS && !MO.isUndef())
1311 PHISrcRegs.insert(MO.getReg());
1312 }
1313 }
1314
1315 // Inherit live-ins from the successor
1316 for (const auto &LI : Succ->liveins())
1317 NMBB->addLiveIn(LI);
1318
1319 if (LIS) {
1320 // After splitting the edge and updating SlotIndexes, live intervals may be
1321 // in one of two situations, depending on whether this block was the last in
1322 // the function. If the original block was the last in the function, all
1323 // live intervals will end prior to the beginning of the new split block. If
1324 // the original block was not at the end of the function, all live intervals
1325 // will extend to the end of the new split block.
1326
1327 bool isLastMBB =
1328 std::next(MachineFunction::iterator(NMBB)) == getParent()->end();
1329
1330 SlotIndex StartIndex = Indexes->getMBBEndIdx(this);
1331 SlotIndex PrevIndex = StartIndex.getPrevSlot();
1332 SlotIndex EndIndex = Indexes->getMBBEndIdx(NMBB);
1333
1334 for (Register Reg : PHISrcRegs) {
1335 LiveInterval &LI = LIS->getInterval(Reg);
1336 VNInfo *VNI = LI.getVNInfoAt(PrevIndex);
1337 assert(VNI && "PHI sources should be live out of their predecessors.");
1338 LI.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI));
1339 for (auto &SR : LI.subranges()) {
1340 if (VNInfo *SRVNI = SR.getVNInfoAt(PrevIndex))
1341 SR.addSegment(LiveInterval::Segment(StartIndex, EndIndex, SRVNI));
1342 }
1343 }
1344
1345 auto UpdateLiveOutReg = [&](Register Reg) {
1346 if (PHISrcRegs.count(Reg))
1347 return true;
1348 if (!LIS->hasInterval(Reg))
1349 return false;
1350
1351 LiveInterval &LI = LIS->getInterval(Reg);
1352 if (!LI.liveAt(PrevIndex))
1353 return false;
1354
1355 bool isLiveOut = LI.liveAt(LIS->getMBBStartIdx(Succ));
1356 if (isLiveOut && isLastMBB) {
1357 VNInfo *VNI = LI.getVNInfoAt(PrevIndex);
1358 assert(VNI && "LiveInterval should have VNInfo where it is live.");
1359 LI.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI));
1360 // Update subranges with live values
1361 for (auto &SR : LI.subranges()) {
1362 VNInfo *VNI = SR.getVNInfoAt(PrevIndex);
1363 if (VNI)
1364 SR.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI));
1365 }
1366 } else if (!isLiveOut && !isLastMBB) {
1367 LI.removeSegment(StartIndex, EndIndex);
1368 // The main range is live across NMBB, but an individual lane need not
1369 // be.
1370 for (auto &SR : LI.subranges()) {
1371 if (SR.liveAt(PrevIndex))
1372 SR.removeSegment(StartIndex, EndIndex);
1373 }
1374 }
1375 return isLiveOut;
1376 };
1377
1378 if (std::vector<SparseBitVector<>> *LiveOutSets = Analyses.LiveOutSets) {
1379 LiveOutSets->resize(MF->getNumBlockIDs());
1380 const SparseBitVector<> &LiveOut = (*LiveOutSets)[getNumber()];
1381 SparseBitVector<> &NewLiveOut = (*LiveOutSets)[NMBB->getNumber()];
1382 for (unsigned Idx : LiveOut) {
1383 if (UpdateLiveOutReg(Register::index2VirtReg(Idx)))
1384 NewLiveOut.set(Idx);
1385 }
1386 } else {
1388 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i)
1389 UpdateLiveOutReg(Register::index2VirtReg(i));
1390 }
1391
1392 // Update all intervals for registers whose uses may have been modified by
1393 // updateTerminator().
1394 LIS->repairIntervalsInRange(this, getFirstTerminator(), end(), UsedRegs);
1395
1396 // repairIntervalsInRange() does not update physregs; clear their ranges
1397 // since updateTerminator() may have replaced defs.
1398 for (Register Reg : UsedRegs) {
1399 if (Reg.isPhysical())
1400 LIS->removeAllRegUnitsForPhysReg(Reg.asMCReg());
1401 }
1402 }
1403
1404 if (MDTU)
1405 MDTU->splitCriticalEdge(this, Succ, NMBB);
1406
1407 if (MachineLoopInfo *MLI = Analyses.MLI)
1408 if (MachineLoop *TIL = MLI->getLoopFor(this)) {
1409 // If one or the other blocks were not in a loop, the new block is not
1410 // either, and thus LI doesn't need to be updated.
1411 if (MachineLoop *DestLoop = MLI->getLoopFor(Succ)) {
1412 if (TIL == DestLoop) {
1413 // Both in the same loop, the NMBB joins loop.
1414 DestLoop->addBasicBlockToLoop(NMBB, *MLI);
1415 } else if (TIL->contains(DestLoop)) {
1416 // Edge from an outer loop to an inner loop. Add to the outer loop.
1417 TIL->addBasicBlockToLoop(NMBB, *MLI);
1418 } else if (DestLoop->contains(TIL)) {
1419 // Edge from an inner loop to an outer loop. Add to the outer loop.
1420 DestLoop->addBasicBlockToLoop(NMBB, *MLI);
1421 } else {
1422 // Edge from two loops with no containment relation. Because these
1423 // are natural loops, we know that the destination block must be the
1424 // header of its loop (adding a branch into a loop elsewhere would
1425 // create an irreducible loop).
1426 assert(DestLoop->getHeader() == Succ &&
1427 "Should not create irreducible loops!");
1428 if (MachineLoop *P = DestLoop->getParentLoop())
1429 P->addBasicBlockToLoop(NMBB, *MLI);
1430 }
1431 }
1432 }
1433
1434 return NMBB;
1435}
1436
1437bool MachineBasicBlock::canSplitCriticalEdge(const MachineBasicBlock *Succ,
1438 const MachineLoopInfo *MLI) const {
1439 // Splitting the critical edge to a landing pad block is non-trivial. Don't do
1440 // it in this generic function.
1441 if (Succ->isEHPad())
1442 return false;
1443
1444 // Splitting the critical edge to a callbr's indirect block isn't advised.
1445 // Don't do it in this generic function.
1446 if (Succ->isInlineAsmBrIndirectTarget())
1447 return false;
1448
1449 const MachineFunction *MF = getParent();
1450 // Performance might be harmed on HW that implements branching using exec mask
1451 // where both sides of the branches are always executed.
1452
1453 if (MF->getTarget().requiresStructuredCFG()) {
1454 if (!MLI)
1455 return false;
1456 const MachineLoop *L = MLI->getLoopFor(Succ);
1457 // Only if `Succ` is a loop header, splitting the critical edge will not
1458 // break structured CFG. And fallthrough to check if this's terminator is
1459 // analyzable.
1460 if (!L || L->getHeader() != Succ)
1461 return false;
1462 }
1463
1464 // Do we have an Indirect jump with a jumptable that we can rewrite?
1465 int JTI = findJumpTableIndex(*this);
1466 if (JTI >= 0 && !jumpTableHasOtherUses(*MF, *this, JTI))
1467 return true;
1468
1469 // We may need to update this's terminator, but we can't do that if
1470 // analyzeBranch fails.
1472 const MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
1474 // AnalyzeBanch should modify this, since we did not allow modification.
1475 if (TII->analyzeBranch(*this, TBB, FBB, Cond))
1476 return false;
1477
1478 // Handle weird inputs (e.g., generated by a test case reducer/fuzzer): A
1479 // block may end with a conditional branch but jumps to the same MBB is either
1480 // case. We have duplicate CFG edges in that case that we can't handle. Since
1481 // this never happens in properly optimized code, just skip those edges.
1482 if (TBB && TBB == FBB) {
1483 LLVM_DEBUG(dbgs() << "Won't split critical edge after degenerate "
1484 << printMBBReference(*this) << '\n');
1485 return false;
1486 }
1487 return true;
1488}
1489
1490/// Prepare MI to be removed from its bundle. This fixes bundle flags on MI's
1491/// neighboring instructions so the bundle won't be broken by removing MI.
1493 // Removing the first instruction in a bundle.
1494 if (MI->isBundledWithSucc() && !MI->isBundledWithPred())
1495 MI->unbundleFromSucc();
1496 // Removing the last instruction in a bundle.
1497 if (MI->isBundledWithPred() && !MI->isBundledWithSucc())
1498 MI->unbundleFromPred();
1499 // If MI is not bundled, or if it is internal to a bundle, the neighbor flags
1500 // are already fine.
1501}
1502
1508
1511 MI->clearFlag(MachineInstr::BundledPred);
1512 MI->clearFlag(MachineInstr::BundledSucc);
1513 return Insts.remove(MI);
1514}
1515
1518 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
1519 "Cannot insert instruction with bundle flags");
1520 // Set the bundle flags when inserting inside a bundle.
1521 if (I != instr_end() && I->isBundledWithPred()) {
1522 MI->setFlag(MachineInstr::BundledPred);
1523 MI->setFlag(MachineInstr::BundledSucc);
1524 }
1525 return Insts.insert(I, MI);
1526}
1527
1528/// This method unlinks 'this' from the containing function, and returns it, but
1529/// does not delete it.
1531 assert(getParent() && "Not embedded in a function!");
1532 getParent()->remove(this);
1533 return this;
1534}
1535
1536/// This method unlinks 'this' from the containing function, and deletes it.
1538 assert(getParent() && "Not embedded in a function!");
1539 getParent()->erase(this);
1540}
1541
1542/// Given a machine basic block that branched to 'Old', change the code and CFG
1543/// so that it branches to 'New' instead.
1545 MachineBasicBlock *New) {
1546 assert(Old != New && "Cannot replace self with self!");
1547
1549 while (I != instr_begin()) {
1550 --I;
1551 if (!I->isTerminator()) break;
1552
1553 // Scan the operands of this machine instruction, replacing any uses of Old
1554 // with New.
1555 for (MachineOperand &MO : I->operands())
1556 if (MO.isMBB() && MO.getMBB() == Old)
1557 MO.setMBB(New);
1558 }
1559
1560 // Update the successor information.
1561 replaceSuccessor(Old, New);
1562}
1563
1564void MachineBasicBlock::replacePhiUsesWith(MachineBasicBlock *Old,
1565 MachineBasicBlock *New) {
1566 for (MachineInstr &MI : phis())
1567 for (unsigned i = 2, e = MI.getNumOperands() + 1; i != e; i += 2) {
1568 MachineOperand &MO = MI.getOperand(i);
1569 if (MO.getMBB() == Old)
1570 MO.setMBB(New);
1571 }
1572}
1573
1574/// Find the next valid DebugLoc starting at MBBI, skipping any debug
1575/// instructions. Return UnknownLoc if there is none.
1578 // Skip debug declarations, we don't want a DebugLoc from them.
1580 if (MBBI != instr_end())
1581 return MBBI->getDebugLoc();
1582 return {};
1583}
1584
1586 if (MBBI == instr_rend())
1587 return findDebugLoc(instr_begin());
1588 // Skip debug declarations, we don't want a DebugLoc from them.
1590 if (!MBBI->isDebugInstr())
1591 return MBBI->getDebugLoc();
1592 return {};
1593}
1594
1595/// Find the previous valid DebugLoc preceding MBBI, skipping any debug
1596/// instructions. Return UnknownLoc if there is none.
1598 if (MBBI == instr_begin())
1599 return {};
1600 // Skip debug instructions, we don't want a DebugLoc from them.
1602 if (!MBBI->isDebugInstr())
1603 return MBBI->getDebugLoc();
1604 return {};
1605}
1606
1608 if (MBBI == instr_rend())
1609 return {};
1610 // Skip debug declarations, we don't want a DebugLoc from them.
1612 if (MBBI != instr_rend())
1613 return MBBI->getDebugLoc();
1614 return {};
1615}
1616
1617/// Find and return the merged DebugLoc of the branch instructions of the block.
1618/// Return UnknownLoc if there is none.
1621 DebugLoc DL;
1622 auto TI = getFirstTerminator();
1623 while (TI != end() && !TI->isBranch())
1624 ++TI;
1625
1626 if (TI != end()) {
1627 DL = TI->getDebugLoc();
1628 for (++TI ; TI != end() ; ++TI)
1629 if (TI->isBranch())
1630 DL = DebugLoc::getMergedLocation(DL, TI->getDebugLoc());
1631 }
1632 return DL;
1633}
1634
1635/// Return probability of the edge from this block to MBB.
1638 if (Probs.empty())
1639 return BranchProbability(1, succ_size());
1640
1641 const auto &Prob = *getProbabilityIterator(Succ);
1642 if (!Prob.isUnknown())
1643 return Prob;
1644 // For unknown probabilities, collect the sum of all known ones, and evenly
1645 // ditribute the complemental of the sum to each unknown probability.
1646 unsigned KnownProbNum = 0;
1647 auto Sum = BranchProbability::getZero();
1648 for (const auto &P : Probs) {
1649 if (!P.isUnknown()) {
1650 Sum += P;
1651 KnownProbNum++;
1652 }
1653 }
1654 return Sum.getCompl() / (Probs.size() - KnownProbNum);
1655}
1656
1658 if (succ_size() <= 1)
1659 return true;
1661 return true;
1662
1663 SmallVector<BranchProbability, 8> Normalized(Probs.begin(), Probs.end());
1665
1666 // Normalize assuming unknown probabilities. This will assign equal
1667 // probabilities to all successors.
1668 SmallVector<BranchProbability, 8> Equal(Normalized.size());
1670
1671 return llvm::equal(Normalized, Equal);
1672}
1673
1674/// Set successor probability of a given iterator.
1676 BranchProbability Prob) {
1677 assert(!Prob.isUnknown());
1678 if (Probs.empty())
1679 return;
1680 *getProbabilityIterator(I) = Prob;
1681}
1682
1683/// Return probability iterator corresonding to the I successor iterator
1684MachineBasicBlock::const_probability_iterator
1685MachineBasicBlock::getProbabilityIterator(
1687 assert(Probs.size() == Successors.size() && "Async probability list!");
1688 const size_t index = std::distance(Successors.begin(), I);
1689 assert(index < Probs.size() && "Not a current successor!");
1690 return Probs.begin() + index;
1691}
1692
1693/// Return probability iterator corresonding to the I successor iterator.
1694MachineBasicBlock::probability_iterator
1695MachineBasicBlock::getProbabilityIterator(MachineBasicBlock::succ_iterator I) {
1696 assert(Probs.size() == Successors.size() && "Async probability list!");
1697 const size_t index = std::distance(Successors.begin(), I);
1698 assert(index < Probs.size() && "Not a current successor!");
1699 return Probs.begin() + index;
1700}
1701
1702/// Return whether (physical) register "Reg" has been <def>ined and not <kill>ed
1703/// as of just before "MI".
1704///
1705/// Search is localised to a neighborhood of
1706/// Neighborhood instructions before (searching for defs or kills) and N
1707/// instructions after (searching just for defs) MI.
1710 MCRegister Reg, const_iterator Before,
1711 unsigned Neighborhood) const {
1712 assert(Reg.isPhysical());
1713 unsigned N = Neighborhood;
1714
1715 // Try searching forwards from Before, looking for reads or defs.
1716 const_iterator I(Before);
1717 for (; I != end() && N > 0; ++I) {
1718 if (I->isDebugOrPseudoInstr())
1719 continue;
1720
1721 --N;
1722
1723 PhysRegInfo Info = AnalyzePhysRegInBundle(*I, Reg, TRI);
1724
1725 // Register is live when we read it here.
1726 if (Info.Read)
1727 return LQR_Live;
1728 // Register is dead if we can fully overwrite or clobber it here.
1729 if (Info.FullyDefined || Info.Clobbered)
1730 return LQR_Dead;
1731 }
1732
1733 // If we reached the end, it is safe to clobber Reg at the end of a block of
1734 // no successor has it live in.
1735 if (I == end()) {
1736 for (MachineBasicBlock *S : successors()) {
1737 for (const MachineBasicBlock::RegisterMaskPair &LI : S->liveins()) {
1738 if (TRI->regsOverlap(LI.PhysReg, Reg))
1739 return LQR_Live;
1740 }
1741 }
1742
1743 return LQR_Dead;
1744 }
1745
1746
1747 N = Neighborhood;
1748
1749 // Start by searching backwards from Before, looking for kills, reads or defs.
1750 I = const_iterator(Before);
1751 // If this is the first insn in the block, don't search backwards.
1752 if (I != begin()) {
1753 do {
1754 --I;
1755
1756 if (I->isDebugOrPseudoInstr())
1757 continue;
1758
1759 --N;
1760
1761 PhysRegInfo Info = AnalyzePhysRegInBundle(*I, Reg, TRI);
1762
1763 // Defs happen after uses so they take precedence if both are present.
1764
1765 // Register is dead after a dead def of the full register.
1766 if (Info.DeadDef)
1767 return LQR_Dead;
1768 // Register is (at least partially) live after a def.
1769 if (Info.Defined) {
1770 if (!Info.PartialDeadDef)
1771 return LQR_Live;
1772 // As soon as we saw a partial definition (dead or not),
1773 // we cannot tell if the value is partial live without
1774 // tracking the lanemasks. We are not going to do this,
1775 // so fall back on the remaining of the analysis.
1776 break;
1777 }
1778 // Register is dead after a full kill or clobber and no def.
1779 if (Info.Killed || Info.Clobbered)
1780 return LQR_Dead;
1781 // Register must be live if we read it.
1782 if (Info.Read)
1783 return LQR_Live;
1784
1785 } while (I != begin() && N > 0);
1786 }
1787
1788 // If all the instructions before this in the block are debug instructions,
1789 // skip over them.
1790 while (I != begin() && std::prev(I)->isDebugOrPseudoInstr())
1791 --I;
1792
1793 // Did we get to the start of the block?
1794 if (I == begin()) {
1795 // If so, the register's state is definitely defined by the live-in state.
1797 if (TRI->regsOverlap(LI.PhysReg, Reg))
1798 return LQR_Live;
1799
1800 return LQR_Dead;
1801 }
1802
1803 // At this point we have no idea of the liveness of the register.
1804 return LQR_Unknown;
1805}
1806
1807const uint32_t *
1809 // EH funclet entry does not preserve any registers.
1810 return isEHFuncletEntry() ? TRI->getNoPreservedMask() : nullptr;
1811}
1812
1813const uint32_t *
1815 // If we see a return block with successors, this must be a funclet return,
1816 // which does not preserve any registers. If there are no successors, we don't
1817 // care what kind of return it is, putting a mask after it is a no-op.
1818 return isReturnBlock() && !succ_empty() ? TRI->getNoPreservedMask() : nullptr;
1819}
1820
1822 LiveIns.clear();
1823}
1824
1826 std::vector<RegisterMaskPair> &OldLiveIns) {
1827 assert(OldLiveIns.empty() && "Vector must be empty");
1828 std::swap(LiveIns, OldLiveIns);
1829}
1830
1832 assert(getParent()->getProperties().hasTracksLiveness() &&
1833 "Liveness information is accurate");
1834 return LiveIns.begin();
1835}
1836
1838 const MachineFunction &MF = *getParent();
1839 const TargetLowering &TLI = *MF.getSubtarget().getTargetLowering();
1840 MCRegister ExceptionPointer, ExceptionSelector;
1841 if (MF.getFunction().hasPersonalityFn()) {
1842 auto PersonalityFn = MF.getFunction().getPersonalityFn();
1844 ExceptionPointer = TLI.getExceptionPointerRegister(EH, PersonalityFn);
1845 ExceptionSelector = TLI.getExceptionSelectorRegister(EH, PersonalityFn);
1846 }
1847
1848 return liveout_iterator(*this, ExceptionPointer, ExceptionSelector, false);
1849}
1850
1852 unsigned Cntr = 0;
1853 auto R = instructionsWithoutDebug(begin(), end());
1854 for (auto I = R.begin(), E = R.end(); I != E; ++I) {
1855 if (++Cntr > Limit)
1856 return true;
1857 }
1858 return false;
1859}
1860
1862 const MachineBasicBlock &PredMBB) {
1863 for (MachineInstr &Phi : phis())
1864 Phi.removePHIIncomingValueFor(PredMBB);
1865}
1866
1868const MBBSectionID
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:682
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
This file contains an interface for creating legacy passes to print out IR in various granularities.
Module.h This file contains the declarations for the Module class.
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define GET_RESULT(RESULT, GETTER, INFIX)
static bool jumpTableHasOtherUses(const MachineFunction &MF, const MachineBasicBlock &IgnoreMBB, int JumpTableIndex)
static void unbundleSingleMI(MachineInstr *MI)
Prepare MI to be removed from its bundle.
static int findJumpTableIndex(const MachineBasicBlock &MBB)
static cl::opt< bool > PrintSlotIndexes("print-slotindexes", cl::desc("When printing machine IR, annotate instructions and blocks with " "SlotIndexes when available"), cl::init(true), cl::Hidden)
Register const TargetRegisterInfo * TRI
uint64_t IntrinsicInst * II
#define P(N)
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
static bool isLiveOut(const MachineBasicBlock &MBB, unsigned Reg)
This file contains some templates that are useful if you are working with the STL at all.
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:119
This file describes how to lower LLVM code to machine code.
SlotIndexUpdateDelegate(MachineFunction &MF, SlotIndexes *Indexes)
void MF_HandleRemoval(MachineInstr &MI) override
Callback before a removal. This should not modify the MI directly.
void MF_HandleInsertion(MachineInstr &MI) override
Callback after an insertion. This should not modify the MI directly.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
static uint32_t getDenominator()
static constexpr BranchProbability getUnknown()
static constexpr BranchProbability getZero()
uint32_t getNumerator() const
static void normalizeProbabilities(ProbabilityIter Begin, ProbabilityIter End)
A debug info location.
Definition DebugLoc.h:126
LLVM_ABI unsigned getLine() const
Definition DebugLoc.cpp:43
static LLVM_ABI DebugLoc getMergedLocation(DebugLoc LocA, DebugLoc LocB)
When two instructions are combined into a single instruction we also need to combine the original loc...
Definition DebugLoc.cpp:186
LLVM_ABI unsigned getCol() const
Definition DebugLoc.cpp:48
bool hasPersonalityFn() const
Check whether this function has a personality function.
Definition Function.h:890
Constant * getPersonalityFn() const
Get the personality function associated with this function.
void splitCriticalEdge(BasicBlockT *FromBB, BasicBlockT *ToBB, BasicBlockT *NewBB)
Apply updates that the critical edge (FromBB, ToBB) has been split with NewBB.
Module * getParent()
Get the module that this global value is contained inside of...
A helper class to return the specified delimiter string after the first invocation of operator String...
LiveInterval - This class represents the liveness of a register, or stack slot.
iterator_range< subrange_iterator > subranges()
void insertMBBInMaps(MachineBasicBlock *MBB)
Adds an empty block MBB to the SlotIndexes and regmask maps.
void splitAt(MachineBasicBlock &Orig, MachineBasicBlock &SplitBB)
After the tail of Orig has been sliced into SplitBB, updates the SlotIndexes and regmask maps and re-...
A set of physical registers with utility functions to track liveness when walking backward/forward th...
LLVM_ABI iterator addSegment(Segment S)
Add the specified Segment to this range, merging segments as appropriate.
bool liveAt(SlotIndex index) const
LLVM_ABI void removeSegment(SlotIndex Start, SlotIndex End, bool RemoveDeadValNo=false)
Remove the specified interval from this live range.
VNInfo * getVNInfoAt(SlotIndex Idx) const
getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Context object for machine code objects.
Definition MCContext.h:83
LLVM_ABI MCSymbol * createBlockSymbol(const Twine &Name, bool AlwaysEmit=false)
Get or create a symbol for a basic block.
LLVM_ABI MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
MCRegUnitMaskIterator enumerates a list of register units and their associated lane masks for Reg.
bool isValid() const
Returns true if this iterator is not yet at the end.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition MCRegister.h:72
Iterator that enumerates the sub-registers of a Reg and the associated sub-register indices.
bool isValid() const
Returns true if this iterator is not yet at the end.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
bool isInlineAsmBrIndirectTarget() const
Returns true if this is the indirect dest of an INLINEASM_BR.
LLVM_ABI DebugLoc rfindPrevDebugLoc(reverse_instr_iterator MBBI)
Has exact same behavior as findPrevDebugLoc (it also searches towards the beginning of this MBB) exce...
LLVM_ABI void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
LLVM_ABI bool hasEHPadSuccessor() const
void normalizeSuccProbs()
Normalize probabilities of all successors so that the sum of them becomes one.
livein_iterator livein_end() const
LLVM_ABI iterator getFirstTerminatorForward()
Finds the first terminator in a block by scanning forward.
bool isEHPad() const
Returns true if the block is a landing pad.
LLVM_ABI void replacePhiUsesWith(MachineBasicBlock *Old, MachineBasicBlock *New)
Update all phi nodes in this basic block to refer to basic block New instead of basic block Old.
LLVM_ABI MachineInstr * remove_instr(MachineInstr *I)
Remove the possibly bundled instruction from the instruction list without deleting it.
MachineInstrBundleIterator< const MachineInstr > const_iterator
LLVM_ABI MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
LLVM_ABI void moveBefore(MachineBasicBlock *NewAfter)
Move 'this' block before or after the specified block.
LLVM_ABI void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New)
Replace successor OLD with NEW and update probability info.
LLVM_ABI MachineBasicBlock * getFallThrough(bool JumpToFallThrough=true)
Return the fallthrough block if the block can implicitly transfer control to the block after it by fa...
LLVM_ABI void transferSuccessors(MachineBasicBlock *FromMBB)
Transfers all the successors from MBB to this machine basic block (i.e., copies all the successors Fr...
bool hasLabelMustBeEmitted() const
Test whether this block must have its label emitted.
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
LLVM_ABI BranchProbability getSuccProbability(const_succ_iterator Succ) const
Return probability of the edge from this block to MBB.
iterator_range< livein_iterator > liveins() const
iterator_range< iterator > phis()
Returns a range that iterates over the phis in the basic block.
reverse_instr_iterator instr_rbegin()
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
LLVM_ABI iterator SkipPHIsAndLabels(iterator I)
Return the first instruction in MBB after I that is not a PHI or a label.
LLVM_ABI void addSuccessorWithoutProb(MachineBasicBlock *Succ)
Add Succ as a successor of this MachineBasicBlock.
SmallVectorImpl< MachineBasicBlock * >::const_iterator const_succ_iterator
LLVM_ABI bool hasName() const
Check if there is a name of corresponding LLVM basic block.
void setCallFrameSize(unsigned N)
Set the call frame size on entry to this basic block.
std::optional< UniqueBBID > getBBID() const
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI MCSymbol * getEHContSymbol() const
Return the Windows EH Continuation Symbol for this basic block.
LLVM_ABI void splitSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New, bool NormalizeSuccProbs=false)
Split the old successor into old plus new and updates the probability info.
@ PrintNameIr
Add IR name where available.
@ PrintNameAttributes
Print attributes.
LLVM_ABI void updateTerminator(MachineBasicBlock *PreviousLayoutSuccessor)
Update the terminator instructions in block to account for changes to block layout which may have bee...
LLVM_ABI const MachineBasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor.
LLVM_ABI iterator SkipPHIsLabelsAndDebug(iterator I, Register Reg=Register(), bool SkipPseudoOp=true)
Return the first instruction in MBB after I that is not a PHI, label or debug.
LLVM_ABI bool canFallThrough()
Return true if the block can implicitly transfer control to the block after it by falling off the end...
LLVM_ABI void setSuccProbability(succ_iterator I, BranchProbability Prob)
Set successor probability of a given iterator.
LLVM_ABI iterator getFirstNonDebugInstr(bool SkipPseudoOp=true)
Returns an iterator to the first non-debug instruction in the basic block, or end().
LLVM_ABI void removeLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll())
Remove the specified register from the live in set.
LLVM_ABI void printAsOperand(raw_ostream &OS, bool PrintType=true) const
unsigned getMaxBytesForAlignment() const
Return the maximum amount of padding allowed for aligning the basic block.
LLVM_ABI void validateSuccProbs() const
Validate successors' probabilities and check if the sum of them is approximate one.
bool isIRBlockAddressTaken() const
Test whether this block is the target of an IR BlockAddress.
LiveInVector::const_iterator livein_iterator
LLVM_ABI MCSymbol * getEndSymbol() const
Returns the MCSymbol marking the end of this basic block.
LLVM_ABI void clearLiveIns()
Clear live in list.
bool isEHFuncletEntry() const
Returns true if this is the entry block of an EH funclet.
LLVM_ABI LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI, MCRegister Reg, const_iterator Before, unsigned Neighborhood=10) const
Return whether (physical) register Reg has been defined and not killed as of just before Before.
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
LLVM_ABI livein_iterator livein_begin() const
bool isReturnBlock() const
Convenience function that returns true if the block ends in a return instruction.
LLVM_ABI const uint32_t * getBeginClobberMask(const TargetRegisterInfo *TRI) const
Get the clobber mask for the start of this basic block.
LLVM_ABI void removePHIsIncomingValuesForPredecessor(const MachineBasicBlock &PredMBB)
Iterate over block PHI instructions and remove all incoming values for PredMBB.
MBBSectionID getSectionID() const
Returns the section ID of this basic block.
LLVM_ABI void dump() const
bool isEHScopeEntry() const
Returns true if this is the entry block of an EH scope, i.e., the block that used to have a catchpad ...
LLVM_ABI bool isEntryBlock() const
Returns true if this is the entry block of the function.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
LLVM_ABI void copySuccessor(const MachineBasicBlock *Orig, succ_iterator I)
Copy a successor (and any probability info) from original block to this block's.
SmallVectorImpl< MachineBasicBlock * >::iterator succ_iterator
BasicBlock * getAddressTakenIRBlock() const
Retrieves the BasicBlock which corresponds to this MachineBasicBlock.
LLVM_ABI void sortUniqueLiveIns()
Sorts and uniques the LiveIns vector.
LLVM_ABI const MachineBasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
LLVM_ABI liveout_iterator liveout_begin() const
Iterator scanning successor basic blocks' liveins to determine the registers potentially live at the ...
LLVM_ABI void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
LLVM_ABI iterator getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
LLVM_ABI bool isPredecessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a predecessor of this block.
bool hasSuccessorProbabilities() const
Return true if any of the successors have probabilities attached to them.
LLVM_ABI DebugLoc rfindDebugLoc(reverse_instr_iterator MBBI)
Has exact same behavior as findDebugLoc (it also searches towards the end of this MBB) except that th...
LLVM_ABI void print(raw_ostream &OS, const SlotIndexes *=nullptr, bool IsStandalone=true) const
reverse_instr_iterator instr_rend()
LLVM_ABI DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any debug instructions.
Instructions::iterator instr_iterator
LLVM_ABI iterator getLastNonDebugInstr(bool SkipPseudoOp=true)
Returns an iterator to the last non-debug instruction in the basic block, or end().
LLVM_ABI void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New)
Given a machine basic block that branched to 'Old', change the code and CFG so that it branches to 'N...
LLVM_ABI bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
LLVM_ABI DebugLoc findPrevDebugLoc(instr_iterator MBBI)
Find the previous valid DebugLoc preceding MBBI, skipping any debug instructions.
LLVM_ABI MachineBasicBlock * splitAt(MachineInstr &SplitInst, bool UpdateLiveIns=true, LiveIntervals *LIS=nullptr)
Split a basic block into 2 pieces at SplitPoint.
LLVM_ABI bool canSplitCriticalEdge(const MachineBasicBlock *Succ, const MachineLoopInfo *MLI=nullptr) const
Check if the edge between this block and the given successor Succ, can be split.
LLVM_ABI void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
LLVM_ABI void removeLiveInOverlappedWith(MCRegister Reg)
Remove the specified register from any overlapped live in.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
LLVM_ABI bool isLiveInOverlappedWith(MCRegister Reg, const TargetRegisterInfo &TRI) const
Return true if any part of Reg is live in, accounting for live-ins of overlapping registers and their...
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
LLVM_ABI std::string getFullName() const
Return a formatted string to identify this block and its parent function.
bool isBeginSection() const
Returns true if this block begins any section.
MachineBasicBlock * SplitCriticalEdge(MachineBasicBlock *Succ, Pass &P, MachineDomTreeUpdater *MDTU=nullptr)
bool isEHContTarget() const
Returns true if this is a target of Windows EH Continuation Guard.
unsigned getCallFrameSize() const
Return the call frame size on entry to this basic block.
LLVM_ABI DebugLoc findBranchDebugLoc()
Find and return the merged DebugLoc of the branch instructions of the block.
iterator_range< succ_iterator > successors()
LLVM_ABI instr_iterator getFirstInstrTerminator()
Same getFirstTerminator but it ignores bundles and return an instr_iterator instead.
reverse_iterator rbegin()
bool isMachineBlockAddressTaken() const
Test whether this block is used as something other than the target of a terminator,...
LLVM_ABI void printName(raw_ostream &os, unsigned printNameFlags=PrintNameIr, ModuleSlotTracker *moduleSlotTracker=nullptr) const
Print the basic block's name as:
LLVM_ABI bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
iterator_range< pred_iterator > predecessors()
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
Align getAlignment() const
Return alignment of the basic block.
MachineInstrBundleIterator< MachineInstr > iterator
LLVM_ABI bool isLegalToHoistInto() const
Returns true if it is legal to hoist instructions into this block.
LLVM_ABI bool canPredictBranchProbabilities() const
LLVM_ABI StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
LLVM_ABI bool mayHaveInlineAsmBr() const
Returns true if this block may have an INLINEASM_BR (overestimate, by checking if any of the successo...
LivenessQueryResult
Possible outcome of a register liveness query to computeRegisterLiveness()
@ LQR_Dead
Register is known to be fully dead.
@ LQR_Live
Register is known to be (at least partially) live.
@ LQR_Unknown
Register liveness not decidable from local neighborhood.
LLVM_ABI void moveAfter(MachineBasicBlock *NewBefore)
LLVM_ABI const uint32_t * getEndClobberMask(const TargetRegisterInfo *TRI) const
Get the clobber mask for the end of the basic block.
LLVM_ABI bool sizeWithoutDebugLargerThan(unsigned Limit) const
LLVM_ABI bool isLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
LLVM_ABI MachineBasicBlock * removeFromParent()
This method unlinks 'this' from the containing function, and returns it, but does not delete it.
Instructions::reverse_iterator reverse_instr_iterator
bool isCleanupFuncletEntry() const
Returns true if this is the entry block of a cleanup funclet.
unsigned addToMBBNumbering(MachineBasicBlock *MBB)
Adds the MBB to the internal numbering.
unsigned getFunctionNumber() const
getFunctionNumber - Return a unique ID for the current function.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
bool hasBBSections() const
Returns true if this function has basic block sections enabled.
MCContext & getContext() const
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
BasicBlockListType::iterator iterator
void remove(iterator MBBI)
const MachineJumpTableInfo * getJumpTableInfo() const
getJumpTableInfo - Return the jump table info object for the current function.
void splice(iterator InsertPt, iterator MBBI)
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void erase(iterator MBBI)
void insert(iterator MBBI, MachineBasicBlock *MBB)
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
BasicBlockListType::const_iterator const_iterator
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
reverse_iterator getReverse() const
Get a reverse iterator to the same node.
Representation of each machine instruction.
LLVM_ABI bool ReplaceMBBInJumpTable(unsigned Idx, MachineBasicBlock *Old, MachineBasicBlock *New)
ReplaceMBBInJumpTable - If Old is a target of the jump tables, update the jump table to branch to New...
const std::vector< MachineJumpTableEntry > & getJumpTables() const
MachineOperand class - Representation of each machine instruction operand.
MachineBasicBlock * getMBB() const
void setMBB(MachineBasicBlock *MBB)
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
bool tracksLiveness() const
tracksLiveness - Returns true when tracking register liveness accurately.
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
LLVM_ABI const TargetRegisterClass * constrainRegClass(Register Reg, const TargetRegisterClass *RC, unsigned MinNumRegs=0)
constrainRegClass - Constrain the register class of the specified virtual register to be a common sub...
unsigned getNumVirtRegs() const
getNumVirtRegs - Return the number of virtual registers created.
Manage lifetime of a slot tracker for printing IR.
int getLocalSlot(const Value *V)
Return the slot number of the specified local value.
void incorporateFunction(const Function &F)
Incorporate the given function.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
ExceptionHandling getExceptionModel() const
Returns the exception model recorded by the "exception-model" module flag, or the target's default ex...
Definition Module.cpp:719
Pass interface - Implemented by all 'passes'.
Definition Pass.h:97
Simple wrapper around std::function<void(raw_ostream&)>.
Definition Printable.h:38
Wrapper class representing virtual and physical registers.
Definition Register.h:20
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
Definition Register.h:72
SlotIndex - An opaque wrapper around machine indexes.
Definition SlotIndexes.h:70
SlotIndex getPrevSlot() const
Returns the previous slot in the index list.
SlotIndexes pass.
void insertMBBInMaps(MachineBasicBlock *mbb)
Add the given MachineBasicBlock into the maps.
SlotIndex getInstructionIndex(const MachineInstr &MI, bool IgnoreBundle=false) const
Returns the base index for the given instruction.
bool hasIndex(const MachineInstr &instr) const
Returns true if the given machine instr is mapped to an index, otherwise returns false.
SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const
Returns the first index in the given basic block.
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:345
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.h:134
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
iterator erase(const_iterator CI)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
void set(unsigned Idx)
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
TargetInstrInfo - Interface to description of machine instruction set.
virtual Register getExceptionSelectorRegister(ExceptionHandling EH, const Constant *PersonalityFn) const
If a physical register, this returns the register that receives the exception typeid on entry to a la...
virtual Register getExceptionPointerRegister(ExceptionHandling EH, const Constant *PersonalityFn) const
If a physical register, this returns the register that receives the exception address on entry to an ...
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
bool requiresStructuredCFG() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
virtual const TargetLowering * getTargetLowering() const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
VNInfo - Value Number Information.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:315
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
A raw_ostream that writes to an SmallVector or SmallString.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
IterT next_nodbg(IterT It, IterT End, bool SkipPseudoOp=true)
Increment It, then continue incrementing it while it points to a debug instruction.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1781
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
@ Kill
The last use of a register.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI PhysRegInfo AnalyzePhysRegInBundle(const MachineInstr &MI, Register Reg, const TargetRegisterInfo *TRI)
AnalyzePhysRegInBundle - Analyze how the current instruction or bundle uses a physical register.
Printable PrintLaneMask(LaneBitmask LaneMask)
Create Printable object to print LaneBitmasks on a raw_ostream.
Definition LaneBitmask.h:92
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
IterT skipDebugInstructionsForward(IterT It, IterT End, bool SkipPseudoOp=true)
Increment It until it points to a non-debug instruction or to End and return the resulting iterator.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1652
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
auto instructionsWithoutDebug(IterT It, IterT End, bool SkipPseudoOp=true)
Construct a range iterator which begins at It and moves forwards until End is reached,...
IterT skipDebugInstructionsBackward(IterT It, IterT Begin, bool SkipPseudoOp=true)
Decrement It until it points to a non-debug instruction or to Begin and return the resulting iterator...
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition Format.h:102
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
ExceptionHandling
Definition CodeGen.h:54
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1788
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
bool equal(L &&LRange, R &&RRange)
Wrapper function around std::equal to detect if pair-wise elements between two ranges are the same.
Definition STLExtras.h:2162
IterT prev_nodbg(IterT It, IterT Begin, bool SkipPseudoOp=true)
Decrement It, then continue decrementing it while it points to a debug instruction.
LLVM_ABI void printLLVMNameWithoutPrefix(raw_ostream &OS, StringRef Name)
Print out a name of an LLVM value without any prefixes.
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
LLVM_ABI void addLiveIns(MachineBasicBlock &MBB, const LivePhysRegs &LiveRegs)
Adds registers contained in LiveRegs to the block live-in list of MBB.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:82
constexpr bool all() const
Definition LaneBitmask.h:54
This represents a simple continuous liveness interval for a value.
LLVM_ABI static const MBBSectionID ExceptionSectionID
LLVM_ABI static const MBBSectionID ColdSectionID
Pair of physical register and lane mask.
Split the critical edge from this block to the given successor block, and return the newly created bl...
MachineJumpTableEntry - One jump table in the jump table info.
std::vector< MachineBasicBlock * > MBBs
MBBs - The vector of basic blocks from which to create the jump table.
Information about how a physical register Reg is used by a set of operands.
static void deleteNode(NodeTy *V)
Definition ilist.h:42
void removeNodeFromList(NodeTy *)
Definition ilist.h:67
void addNodeToList(NodeTy *)
When an MBB is added to an MF, we need to update the parent pointer of the MBB, the MBB numbering,...
Definition ilist.h:66
void transferNodesFromList(ilist_callback_traits &OldList, Iterator, Iterator)
Callback before transferring nodes to this list.
Definition ilist.h:72
Template traits for intrusive list.
Definition ilist.h:90