1f22ef01cSRoman Divacky //===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===//
2f22ef01cSRoman Divacky //
3f22ef01cSRoman Divacky //                     The LLVM Compiler Infrastructure
4f22ef01cSRoman Divacky //
5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7f22ef01cSRoman Divacky //
8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9f22ef01cSRoman Divacky //
10f22ef01cSRoman Divacky // Collect the sequence of machine instructions for a basic block.
11f22ef01cSRoman Divacky //
12f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
13f22ef01cSRoman Divacky 
14f22ef01cSRoman Divacky #include "llvm/CodeGen/MachineBasicBlock.h"
15139f7f9bSDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
162cab237bSDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
17ffd1746dSEd Schouten #include "llvm/CodeGen/LiveVariables.h"
18ffd1746dSEd Schouten #include "llvm/CodeGen/MachineDominators.h"
19f22ef01cSRoman Divacky #include "llvm/CodeGen/MachineFunction.h"
20*6beeb091SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
21ffd1746dSEd Schouten #include "llvm/CodeGen/MachineLoopInfo.h"
22139f7f9bSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
232754fe60SDimitry Andric #include "llvm/CodeGen/SlotIndexes.h"
242cab237bSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
252cab237bSDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
262cab237bSDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
27139f7f9bSDimitry Andric #include "llvm/IR/BasicBlock.h"
28139f7f9bSDimitry Andric #include "llvm/IR/DataLayout.h"
297a7e6055SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
303dac3a9bSDimitry Andric #include "llvm/IR/ModuleSlotTracker.h"
31f22ef01cSRoman Divacky #include "llvm/MC/MCAsmInfo.h"
32f22ef01cSRoman Divacky #include "llvm/MC/MCContext.h"
337d523365SDimitry Andric #include "llvm/Support/DataTypes.h"
34f22ef01cSRoman Divacky #include "llvm/Support/Debug.h"
35f22ef01cSRoman Divacky #include "llvm/Support/raw_ostream.h"
36139f7f9bSDimitry Andric #include "llvm/Target/TargetMachine.h"
37f22ef01cSRoman Divacky #include <algorithm>
38f22ef01cSRoman Divacky using namespace llvm;
39f22ef01cSRoman Divacky 
4091bc56edSDimitry Andric #define DEBUG_TYPE "codegen"
4191bc56edSDimitry Andric 
427d523365SDimitry Andric MachineBasicBlock::MachineBasicBlock(MachineFunction &MF, const BasicBlock *B)
437d523365SDimitry Andric     : BB(B), Number(-1), xParent(&MF) {
44f22ef01cSRoman Divacky   Insts.Parent = this;
452cab237bSDimitry Andric   if (B)
462cab237bSDimitry Andric     IrrLoopHeaderWeight = B->getIrrLoopHeaderWeight();
47f22ef01cSRoman Divacky }
48f22ef01cSRoman Divacky 
49f22ef01cSRoman Divacky MachineBasicBlock::~MachineBasicBlock() {
50f22ef01cSRoman Divacky }
51f22ef01cSRoman Divacky 
527d523365SDimitry Andric /// Return the MCSymbol for this basic block.
53f22ef01cSRoman Divacky MCSymbol *MachineBasicBlock::getSymbol() const {
54284c1978SDimitry Andric   if (!CachedMCSymbol) {
55f22ef01cSRoman Divacky     const MachineFunction *MF = getParent();
56f22ef01cSRoman Divacky     MCContext &Ctx = MF->getContext();
57d88c1a5aSDimitry Andric     auto Prefix = Ctx.getAsmInfo()->getPrivateLabelPrefix();
587d523365SDimitry Andric     assert(getNumber() >= 0 && "cannot get label for unreachable MBB");
59ff0cc061SDimitry Andric     CachedMCSymbol = Ctx.getOrCreateSymbol(Twine(Prefix) + "BB" +
60284c1978SDimitry Andric                                            Twine(MF->getFunctionNumber()) +
61284c1978SDimitry Andric                                            "_" + Twine(getNumber()));
62284c1978SDimitry Andric   }
63284c1978SDimitry Andric 
64284c1978SDimitry Andric   return CachedMCSymbol;
65f22ef01cSRoman Divacky }
66f22ef01cSRoman Divacky 
67f22ef01cSRoman Divacky 
68f22ef01cSRoman Divacky raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) {
69f22ef01cSRoman Divacky   MBB.print(OS);
70f22ef01cSRoman Divacky   return OS;
71f22ef01cSRoman Divacky }
72f22ef01cSRoman Divacky 
732cab237bSDimitry Andric Printable llvm::printMBBReference(const MachineBasicBlock &MBB) {
742cab237bSDimitry Andric   return Printable([&MBB](raw_ostream &OS) { return MBB.printAsOperand(OS); });
752cab237bSDimitry Andric }
762cab237bSDimitry Andric 
777d523365SDimitry Andric /// When an MBB is added to an MF, we need to update the parent pointer of the
787d523365SDimitry Andric /// MBB, the MBB numbering, and any instructions in the MBB to be on the right
797d523365SDimitry Andric /// operand list for registers.
80f22ef01cSRoman Divacky ///
81f22ef01cSRoman Divacky /// MBBs start out as #-1. When a MBB is added to a MachineFunction, it
82f22ef01cSRoman Divacky /// gets the next available unique MBB number. If it is removed from a
83f22ef01cSRoman Divacky /// MachineFunction, it goes back to being #-1.
84d88c1a5aSDimitry Andric void ilist_callback_traits<MachineBasicBlock>::addNodeToList(
85d88c1a5aSDimitry Andric     MachineBasicBlock *N) {
86f22ef01cSRoman Divacky   MachineFunction &MF = *N->getParent();
87f22ef01cSRoman Divacky   N->Number = MF.addToMBBNumbering(N);
88f22ef01cSRoman Divacky 
89f22ef01cSRoman Divacky   // Make sure the instructions have their operands in the reginfo lists.
90f22ef01cSRoman Divacky   MachineRegisterInfo &RegInfo = MF.getRegInfo();
91dff0c46cSDimitry Andric   for (MachineBasicBlock::instr_iterator
92dff0c46cSDimitry Andric          I = N->instr_begin(), E = N->instr_end(); I != E; ++I)
93f22ef01cSRoman Divacky     I->AddRegOperandsToUseLists(RegInfo);
94f22ef01cSRoman Divacky }
95f22ef01cSRoman Divacky 
96d88c1a5aSDimitry Andric void ilist_callback_traits<MachineBasicBlock>::removeNodeFromList(
97d88c1a5aSDimitry Andric     MachineBasicBlock *N) {
98f22ef01cSRoman Divacky   N->getParent()->removeFromMBBNumbering(N->Number);
99f22ef01cSRoman Divacky   N->Number = -1;
100f22ef01cSRoman Divacky }
101f22ef01cSRoman Divacky 
1027d523365SDimitry Andric /// When we add an instruction to a basic block list, we update its parent
1037d523365SDimitry Andric /// pointer and add its operands from reg use/def lists if appropriate.
104f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) {
10591bc56edSDimitry Andric   assert(!N->getParent() && "machine instruction already in a basic block");
106f22ef01cSRoman Divacky   N->setParent(Parent);
107f22ef01cSRoman Divacky 
108f22ef01cSRoman Divacky   // Add the instruction's register operands to their corresponding
109f22ef01cSRoman Divacky   // use/def lists.
110f22ef01cSRoman Divacky   MachineFunction *MF = Parent->getParent();
111f22ef01cSRoman Divacky   N->AddRegOperandsToUseLists(MF->getRegInfo());
112f22ef01cSRoman Divacky }
113f22ef01cSRoman Divacky 
1147d523365SDimitry Andric /// When we remove an instruction from a basic block list, we update its parent
1157d523365SDimitry Andric /// pointer and remove its operands from reg use/def lists if appropriate.
116f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) {
11791bc56edSDimitry Andric   assert(N->getParent() && "machine instruction not in a basic block");
118f22ef01cSRoman Divacky 
119f22ef01cSRoman Divacky   // Remove from the use/def lists.
1202cab237bSDimitry Andric   if (MachineFunction *MF = N->getMF())
1217ae0e2c9SDimitry Andric     N->RemoveRegOperandsFromUseLists(MF->getRegInfo());
122f22ef01cSRoman Divacky 
12391bc56edSDimitry Andric   N->setParent(nullptr);
124f22ef01cSRoman Divacky }
125f22ef01cSRoman Divacky 
1267d523365SDimitry Andric /// When moving a range of instructions from one MBB list to another, we need to
1277d523365SDimitry Andric /// update the parent pointers and the use/def lists.
128d88c1a5aSDimitry Andric void ilist_traits<MachineInstr>::transferNodesFromList(ilist_traits &FromList,
129d88c1a5aSDimitry Andric                                                        instr_iterator First,
130d88c1a5aSDimitry Andric                                                        instr_iterator Last) {
1317d523365SDimitry Andric   assert(Parent->getParent() == FromList.Parent->getParent() &&
132f22ef01cSRoman Divacky         "MachineInstr parent mismatch!");
133d88c1a5aSDimitry Andric   assert(this != &FromList && "Called without a real transfer...");
134d88c1a5aSDimitry Andric   assert(Parent != FromList.Parent && "Two lists have the same parent?");
135f22ef01cSRoman Divacky 
136f22ef01cSRoman Divacky   // If splicing between two blocks within the same function, just update the
137f22ef01cSRoman Divacky   // parent pointers.
1387d523365SDimitry Andric   for (; First != Last; ++First)
1397d523365SDimitry Andric     First->setParent(Parent);
140f22ef01cSRoman Divacky }
141f22ef01cSRoman Divacky 
142f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::deleteNode(MachineInstr *MI) {
143f22ef01cSRoman Divacky   assert(!MI->getParent() && "MI is still in a block!");
144f22ef01cSRoman Divacky   Parent->getParent()->DeleteMachineInstr(MI);
145f22ef01cSRoman Divacky }
146f22ef01cSRoman Divacky 
147ffd1746dSEd Schouten MachineBasicBlock::iterator MachineBasicBlock::getFirstNonPHI() {
148dff0c46cSDimitry Andric   instr_iterator I = instr_begin(), E = instr_end();
149dff0c46cSDimitry Andric   while (I != E && I->isPHI())
150ffd1746dSEd Schouten     ++I;
1513861d79fSDimitry Andric   assert((I == E || !I->isInsideBundle()) &&
1523861d79fSDimitry Andric          "First non-phi MI cannot be inside a bundle!");
153ffd1746dSEd Schouten   return I;
154ffd1746dSEd Schouten }
155ffd1746dSEd Schouten 
1562754fe60SDimitry Andric MachineBasicBlock::iterator
1572754fe60SDimitry Andric MachineBasicBlock::SkipPHIsAndLabels(MachineBasicBlock::iterator I) {
1587a7e6055SDimitry Andric   const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo();
1597a7e6055SDimitry Andric 
160dff0c46cSDimitry Andric   iterator E = end();
1617a7e6055SDimitry Andric   while (I != E && (I->isPHI() || I->isPosition() ||
1627a7e6055SDimitry Andric                     TII->isBasicBlockPrologue(*I)))
163d88c1a5aSDimitry Andric     ++I;
164d88c1a5aSDimitry Andric   // FIXME: This needs to change if we wish to bundle labels
165d88c1a5aSDimitry Andric   // inside the bundle.
166d88c1a5aSDimitry Andric   assert((I == E || !I->isInsideBundle()) &&
167d88c1a5aSDimitry Andric          "First non-phi / non-label instruction is inside a bundle!");
168d88c1a5aSDimitry Andric   return I;
169d88c1a5aSDimitry Andric }
170d88c1a5aSDimitry Andric 
171d88c1a5aSDimitry Andric MachineBasicBlock::iterator
172d88c1a5aSDimitry Andric MachineBasicBlock::SkipPHIsLabelsAndDebug(MachineBasicBlock::iterator I) {
1737a7e6055SDimitry Andric   const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo();
1747a7e6055SDimitry Andric 
175d88c1a5aSDimitry Andric   iterator E = end();
1767a7e6055SDimitry Andric   while (I != E && (I->isPHI() || I->isPosition() || I->isDebugValue() ||
1777a7e6055SDimitry Andric                     TII->isBasicBlockPrologue(*I)))
1782754fe60SDimitry Andric     ++I;
179dff0c46cSDimitry Andric   // FIXME: This needs to change if we wish to bundle labels / dbg_values
180dff0c46cSDimitry Andric   // inside the bundle.
1813861d79fSDimitry Andric   assert((I == E || !I->isInsideBundle()) &&
182d88c1a5aSDimitry Andric          "First non-phi / non-label / non-debug "
183d88c1a5aSDimitry Andric          "instruction is inside a bundle!");
1842754fe60SDimitry Andric   return I;
1852754fe60SDimitry Andric }
1862754fe60SDimitry Andric 
187f22ef01cSRoman Divacky MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
188dff0c46cSDimitry Andric   iterator B = begin(), E = end(), I = E;
189dff0c46cSDimitry Andric   while (I != B && ((--I)->isTerminator() || I->isDebugValue()))
190f22ef01cSRoman Divacky     ; /*noop */
191dff0c46cSDimitry Andric   while (I != E && !I->isTerminator())
192dff0c46cSDimitry Andric     ++I;
193dff0c46cSDimitry Andric   return I;
194dff0c46cSDimitry Andric }
195dff0c46cSDimitry Andric 
196dff0c46cSDimitry Andric MachineBasicBlock::instr_iterator MachineBasicBlock::getFirstInstrTerminator() {
197dff0c46cSDimitry Andric   instr_iterator B = instr_begin(), E = instr_end(), I = E;
198dff0c46cSDimitry Andric   while (I != B && ((--I)->isTerminator() || I->isDebugValue()))
199dff0c46cSDimitry Andric     ; /*noop */
200dff0c46cSDimitry Andric   while (I != E && !I->isTerminator())
2012754fe60SDimitry Andric     ++I;
202f22ef01cSRoman Divacky   return I;
203f22ef01cSRoman Divacky }
204f22ef01cSRoman Divacky 
2053dac3a9bSDimitry Andric MachineBasicBlock::iterator MachineBasicBlock::getFirstNonDebugInstr() {
2063dac3a9bSDimitry Andric   // Skip over begin-of-block dbg_value instructions.
207d88c1a5aSDimitry Andric   return skipDebugInstructionsForward(begin(), end());
2083dac3a9bSDimitry Andric }
2093dac3a9bSDimitry Andric 
2102754fe60SDimitry Andric MachineBasicBlock::iterator MachineBasicBlock::getLastNonDebugInstr() {
211dff0c46cSDimitry Andric   // Skip over end-of-block dbg_value instructions.
212dff0c46cSDimitry Andric   instr_iterator B = instr_begin(), I = instr_end();
2132754fe60SDimitry Andric   while (I != B) {
2142754fe60SDimitry Andric     --I;
215dff0c46cSDimitry Andric     // Return instruction that starts a bundle.
216dff0c46cSDimitry Andric     if (I->isDebugValue() || I->isInsideBundle())
217dff0c46cSDimitry Andric       continue;
218dff0c46cSDimitry Andric     return I;
219dff0c46cSDimitry Andric   }
220dff0c46cSDimitry Andric   // The block is all debug values.
221dff0c46cSDimitry Andric   return end();
222dff0c46cSDimitry Andric }
223dff0c46cSDimitry Andric 
2247d523365SDimitry Andric bool MachineBasicBlock::hasEHPadSuccessor() const {
2257d523365SDimitry Andric   for (const_succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I)
2267d523365SDimitry Andric     if ((*I)->isEHPad())
2277d523365SDimitry Andric       return true;
2287d523365SDimitry Andric   return false;
2297d523365SDimitry Andric }
2307d523365SDimitry Andric 
2313861d79fSDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2323ca95b02SDimitry Andric LLVM_DUMP_METHOD void MachineBasicBlock::dump() const {
233f22ef01cSRoman Divacky   print(dbgs());
234f22ef01cSRoman Divacky }
2353861d79fSDimitry Andric #endif
236f22ef01cSRoman Divacky 
237edd7eaddSDimitry Andric bool MachineBasicBlock::isLegalToHoistInto() const {
238edd7eaddSDimitry Andric   if (isReturnBlock() || hasEHPadSuccessor())
239edd7eaddSDimitry Andric     return false;
240edd7eaddSDimitry Andric   return true;
241edd7eaddSDimitry Andric }
242edd7eaddSDimitry Andric 
243f22ef01cSRoman Divacky StringRef MachineBasicBlock::getName() const {
244f22ef01cSRoman Divacky   if (const BasicBlock *LBB = getBasicBlock())
245f22ef01cSRoman Divacky     return LBB->getName();
246f22ef01cSRoman Divacky   else
2477a7e6055SDimitry Andric     return StringRef("", 0);
248f22ef01cSRoman Divacky }
249f22ef01cSRoman Divacky 
250dff0c46cSDimitry Andric /// Return a hopefully unique identifier for this block.
251dff0c46cSDimitry Andric std::string MachineBasicBlock::getFullName() const {
252dff0c46cSDimitry Andric   std::string Name;
253dff0c46cSDimitry Andric   if (getParent())
2543861d79fSDimitry Andric     Name = (getParent()->getName() + ":").str();
255dff0c46cSDimitry Andric   if (getBasicBlock())
256dff0c46cSDimitry Andric     Name += getBasicBlock()->getName();
257dff0c46cSDimitry Andric   else
258ff0cc061SDimitry Andric     Name += ("BB" + Twine(getNumber())).str();
259dff0c46cSDimitry Andric   return Name;
260dff0c46cSDimitry Andric }
261dff0c46cSDimitry Andric 
2623ca95b02SDimitry Andric void MachineBasicBlock::print(raw_ostream &OS, const SlotIndexes *Indexes)
2633ca95b02SDimitry Andric     const {
264f22ef01cSRoman Divacky   const MachineFunction *MF = getParent();
265f22ef01cSRoman Divacky   if (!MF) {
266f22ef01cSRoman Divacky     OS << "Can't print out MachineBasicBlock because parent MachineFunction"
267f22ef01cSRoman Divacky        << " is null\n";
268f22ef01cSRoman Divacky     return;
269f22ef01cSRoman Divacky   }
2702cab237bSDimitry Andric   const Function &F = MF->getFunction();
2712cab237bSDimitry Andric   const Module *M = F.getParent();
2723dac3a9bSDimitry Andric   ModuleSlotTracker MST(M);
2733dac3a9bSDimitry Andric   print(OS, MST, Indexes);
2743dac3a9bSDimitry Andric }
2753dac3a9bSDimitry Andric 
2763dac3a9bSDimitry Andric void MachineBasicBlock::print(raw_ostream &OS, ModuleSlotTracker &MST,
2773ca95b02SDimitry Andric                               const SlotIndexes *Indexes) const {
2783dac3a9bSDimitry Andric   const MachineFunction *MF = getParent();
2793dac3a9bSDimitry Andric   if (!MF) {
2803dac3a9bSDimitry Andric     OS << "Can't print out MachineBasicBlock because parent MachineFunction"
2813dac3a9bSDimitry Andric        << " is null\n";
2823dac3a9bSDimitry Andric     return;
2833dac3a9bSDimitry Andric   }
284f22ef01cSRoman Divacky 
2852754fe60SDimitry Andric   if (Indexes)
2862754fe60SDimitry Andric     OS << Indexes->getMBBStartIdx(this) << '\t';
2872754fe60SDimitry Andric 
2882cab237bSDimitry Andric   OS << printMBBReference(*this) << ": ";
289f22ef01cSRoman Divacky 
290f22ef01cSRoman Divacky   const char *Comma = "";
291f22ef01cSRoman Divacky   if (const BasicBlock *LBB = getBasicBlock()) {
292f22ef01cSRoman Divacky     OS << Comma << "derived from LLVM BB ";
2933dac3a9bSDimitry Andric     LBB->printAsOperand(OS, /*PrintType=*/false, MST);
294f22ef01cSRoman Divacky     Comma = ", ";
295f22ef01cSRoman Divacky   }
2967d523365SDimitry Andric   if (isEHPad()) { OS << Comma << "EH LANDING PAD"; Comma = ", "; }
297f22ef01cSRoman Divacky   if (hasAddressTaken()) { OS << Comma << "ADDRESS TAKEN"; Comma = ", "; }
2987ae0e2c9SDimitry Andric   if (Alignment)
299dff0c46cSDimitry Andric     OS << Comma << "Align " << Alignment << " (" << (1u << Alignment)
300dff0c46cSDimitry Andric        << " bytes)";
301dff0c46cSDimitry Andric 
302f22ef01cSRoman Divacky   OS << '\n';
303f22ef01cSRoman Divacky 
30439d628a0SDimitry Andric   const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
305f22ef01cSRoman Divacky   if (!livein_empty()) {
3062754fe60SDimitry Andric     if (Indexes) OS << '\t';
307f22ef01cSRoman Divacky     OS << "    Live Ins:";
30895ec533aSDimitry Andric     for (const auto &LI : LiveIns) {
3092cab237bSDimitry Andric       OS << ' ' << printReg(LI.PhysReg, TRI);
310d88c1a5aSDimitry Andric       if (!LI.LaneMask.all())
3117d523365SDimitry Andric         OS << ':' << PrintLaneMask(LI.LaneMask);
3127d523365SDimitry Andric     }
313f22ef01cSRoman Divacky     OS << '\n';
314f22ef01cSRoman Divacky   }
315f22ef01cSRoman Divacky   // Print the preds of this block according to the CFG.
316f22ef01cSRoman Divacky   if (!pred_empty()) {
3172754fe60SDimitry Andric     if (Indexes) OS << '\t';
318f22ef01cSRoman Divacky     OS << "    Predecessors according to CFG:";
319f22ef01cSRoman Divacky     for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI)
3202cab237bSDimitry Andric       OS << " " << printMBBReference(*(*PI));
321f22ef01cSRoman Divacky     OS << '\n';
322f22ef01cSRoman Divacky   }
323f22ef01cSRoman Divacky 
3243ca95b02SDimitry Andric   for (auto &I : instrs()) {
3252754fe60SDimitry Andric     if (Indexes) {
3263ca95b02SDimitry Andric       if (Indexes->hasIndex(I))
3273ca95b02SDimitry Andric         OS << Indexes->getInstructionIndex(I);
3282754fe60SDimitry Andric       OS << '\t';
3292754fe60SDimitry Andric     }
330f22ef01cSRoman Divacky     OS << '\t';
3313ca95b02SDimitry Andric     if (I.isInsideBundle())
332dff0c46cSDimitry Andric       OS << "  * ";
3333ca95b02SDimitry Andric     I.print(OS, MST);
334f22ef01cSRoman Divacky   }
335f22ef01cSRoman Divacky 
336f22ef01cSRoman Divacky   // Print the successors of this block according to the CFG.
337f22ef01cSRoman Divacky   if (!succ_empty()) {
3382754fe60SDimitry Andric     if (Indexes) OS << '\t';
339f22ef01cSRoman Divacky     OS << "    Successors according to CFG:";
3407ae0e2c9SDimitry Andric     for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) {
3412cab237bSDimitry Andric       OS << " " << printMBBReference(*(*SI));
3427d523365SDimitry Andric       if (!Probs.empty())
3437d523365SDimitry Andric         OS << '(' << *getProbabilityIterator(SI) << ')';
3447ae0e2c9SDimitry Andric     }
345f22ef01cSRoman Divacky     OS << '\n';
346f22ef01cSRoman Divacky   }
3472cab237bSDimitry Andric   if (IrrLoopHeaderWeight) {
3482cab237bSDimitry Andric     if (Indexes) OS << '\t';
3492cab237bSDimitry Andric     OS << "    Irreducible loop header weight: "
3502cab237bSDimitry Andric        << IrrLoopHeaderWeight.getValue();
3512cab237bSDimitry Andric     OS << '\n';
3522cab237bSDimitry Andric   }
353f22ef01cSRoman Divacky }
354f22ef01cSRoman Divacky 
3557d523365SDimitry Andric void MachineBasicBlock::printAsOperand(raw_ostream &OS,
3567d523365SDimitry Andric                                        bool /*PrintType*/) const {
3572cab237bSDimitry Andric   OS << "%bb." << getNumber();
35891bc56edSDimitry Andric }
35991bc56edSDimitry Andric 
3607d523365SDimitry Andric void MachineBasicBlock::removeLiveIn(MCPhysReg Reg, LaneBitmask LaneMask) {
361d88c1a5aSDimitry Andric   LiveInVector::iterator I = find_if(
362d88c1a5aSDimitry Andric       LiveIns, [Reg](const RegisterMaskPair &LI) { return LI.PhysReg == Reg; });
3637d523365SDimitry Andric   if (I == LiveIns.end())
3647d523365SDimitry Andric     return;
3657d523365SDimitry Andric 
3667d523365SDimitry Andric   I->LaneMask &= ~LaneMask;
367d88c1a5aSDimitry Andric   if (I->LaneMask.none())
368f22ef01cSRoman Divacky     LiveIns.erase(I);
369f22ef01cSRoman Divacky }
370f22ef01cSRoman Divacky 
371f9448bf3SDimitry Andric MachineBasicBlock::livein_iterator
372f9448bf3SDimitry Andric MachineBasicBlock::removeLiveIn(MachineBasicBlock::livein_iterator I) {
373f9448bf3SDimitry Andric   // Get non-const version of iterator.
374f9448bf3SDimitry Andric   LiveInVector::iterator LI = LiveIns.begin() + (I - LiveIns.begin());
375f9448bf3SDimitry Andric   return LiveIns.erase(LI);
376f9448bf3SDimitry Andric }
377f9448bf3SDimitry Andric 
3787d523365SDimitry Andric bool MachineBasicBlock::isLiveIn(MCPhysReg Reg, LaneBitmask LaneMask) const {
379d88c1a5aSDimitry Andric   livein_iterator I = find_if(
380d88c1a5aSDimitry Andric       LiveIns, [Reg](const RegisterMaskPair &LI) { return LI.PhysReg == Reg; });
381d88c1a5aSDimitry Andric   return I != livein_end() && (I->LaneMask & LaneMask).any();
3827d523365SDimitry Andric }
3837d523365SDimitry Andric 
3847d523365SDimitry Andric void MachineBasicBlock::sortUniqueLiveIns() {
3857d523365SDimitry Andric   std::sort(LiveIns.begin(), LiveIns.end(),
3867d523365SDimitry Andric             [](const RegisterMaskPair &LI0, const RegisterMaskPair &LI1) {
3877d523365SDimitry Andric               return LI0.PhysReg < LI1.PhysReg;
3887d523365SDimitry Andric             });
3897d523365SDimitry Andric   // Liveins are sorted by physreg now we can merge their lanemasks.
3907d523365SDimitry Andric   LiveInVector::const_iterator I = LiveIns.begin();
3917d523365SDimitry Andric   LiveInVector::const_iterator J;
3927d523365SDimitry Andric   LiveInVector::iterator Out = LiveIns.begin();
3937d523365SDimitry Andric   for (; I != LiveIns.end(); ++Out, I = J) {
3947d523365SDimitry Andric     unsigned PhysReg = I->PhysReg;
3957d523365SDimitry Andric     LaneBitmask LaneMask = I->LaneMask;
3967d523365SDimitry Andric     for (J = std::next(I); J != LiveIns.end() && J->PhysReg == PhysReg; ++J)
3977d523365SDimitry Andric       LaneMask |= J->LaneMask;
3987d523365SDimitry Andric     Out->PhysReg = PhysReg;
3997d523365SDimitry Andric     Out->LaneMask = LaneMask;
4007d523365SDimitry Andric   }
4017d523365SDimitry Andric   LiveIns.erase(Out, LiveIns.end());
402f22ef01cSRoman Divacky }
403f22ef01cSRoman Divacky 
404*6beeb091SDimitry Andric unsigned
4057d523365SDimitry Andric MachineBasicBlock::addLiveIn(MCPhysReg PhysReg, const TargetRegisterClass *RC) {
406*6beeb091SDimitry Andric   assert(getParent() && "MBB must be inserted in function");
407*6beeb091SDimitry Andric   assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) && "Expected physreg");
408*6beeb091SDimitry Andric   assert(RC && "Register class is required");
4097d523365SDimitry Andric   assert((isEHPad() || this == &getParent()->front()) &&
410*6beeb091SDimitry Andric          "Only the entry block and landing pads can have physreg live ins");
411*6beeb091SDimitry Andric 
412*6beeb091SDimitry Andric   bool LiveIn = isLiveIn(PhysReg);
413*6beeb091SDimitry Andric   iterator I = SkipPHIsAndLabels(begin()), E = end();
414*6beeb091SDimitry Andric   MachineRegisterInfo &MRI = getParent()->getRegInfo();
41539d628a0SDimitry Andric   const TargetInstrInfo &TII = *getParent()->getSubtarget().getInstrInfo();
416*6beeb091SDimitry Andric 
417*6beeb091SDimitry Andric   // Look for an existing copy.
418*6beeb091SDimitry Andric   if (LiveIn)
419*6beeb091SDimitry Andric     for (;I != E && I->isCopy(); ++I)
420*6beeb091SDimitry Andric       if (I->getOperand(1).getReg() == PhysReg) {
421*6beeb091SDimitry Andric         unsigned VirtReg = I->getOperand(0).getReg();
422*6beeb091SDimitry Andric         if (!MRI.constrainRegClass(VirtReg, RC))
423*6beeb091SDimitry Andric           llvm_unreachable("Incompatible live-in register class.");
424*6beeb091SDimitry Andric         return VirtReg;
425*6beeb091SDimitry Andric       }
426*6beeb091SDimitry Andric 
427*6beeb091SDimitry Andric   // No luck, create a virtual register.
428*6beeb091SDimitry Andric   unsigned VirtReg = MRI.createVirtualRegister(RC);
429*6beeb091SDimitry Andric   BuildMI(*this, I, DebugLoc(), TII.get(TargetOpcode::COPY), VirtReg)
430*6beeb091SDimitry Andric     .addReg(PhysReg, RegState::Kill);
431*6beeb091SDimitry Andric   if (!LiveIn)
432*6beeb091SDimitry Andric     addLiveIn(PhysReg);
433*6beeb091SDimitry Andric   return VirtReg;
434*6beeb091SDimitry Andric }
435*6beeb091SDimitry Andric 
436f22ef01cSRoman Divacky void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
4377d523365SDimitry Andric   getParent()->splice(NewAfter->getIterator(), getIterator());
438f22ef01cSRoman Divacky }
439f22ef01cSRoman Divacky 
440f22ef01cSRoman Divacky void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) {
4417d523365SDimitry Andric   getParent()->splice(++NewBefore->getIterator(), getIterator());
442f22ef01cSRoman Divacky }
443f22ef01cSRoman Divacky 
444f22ef01cSRoman Divacky void MachineBasicBlock::updateTerminator() {
44539d628a0SDimitry Andric   const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo();
446f22ef01cSRoman Divacky   // A block with no successors has no concerns with fall-through edges.
4473ca95b02SDimitry Andric   if (this->succ_empty())
4483ca95b02SDimitry Andric     return;
449f22ef01cSRoman Divacky 
45091bc56edSDimitry Andric   MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
451f22ef01cSRoman Divacky   SmallVector<MachineOperand, 4> Cond;
4527a7e6055SDimitry Andric   DebugLoc DL = findBranchDebugLoc();
4533ca95b02SDimitry Andric   bool B = TII->analyzeBranch(*this, TBB, FBB, Cond);
454f22ef01cSRoman Divacky   (void) B;
455f22ef01cSRoman Divacky   assert(!B && "UpdateTerminators requires analyzable predecessors!");
456f22ef01cSRoman Divacky   if (Cond.empty()) {
457f22ef01cSRoman Divacky     if (TBB) {
4583ca95b02SDimitry Andric       // The block has an unconditional branch. If its successor is now its
4593ca95b02SDimitry Andric       // layout successor, delete the branch.
460f22ef01cSRoman Divacky       if (isLayoutSuccessor(TBB))
461d88c1a5aSDimitry Andric         TII->removeBranch(*this);
462f22ef01cSRoman Divacky     } else {
4633ca95b02SDimitry Andric       // The block has an unconditional fallthrough. If its successor is not its
4643ca95b02SDimitry Andric       // layout successor, insert a branch. First we have to locate the only
4653ca95b02SDimitry Andric       // non-landing-pad successor, as that is the fallthrough block.
466dff0c46cSDimitry Andric       for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) {
4677d523365SDimitry Andric         if ((*SI)->isEHPad())
468dff0c46cSDimitry Andric           continue;
469dff0c46cSDimitry Andric         assert(!TBB && "Found more than one non-landing-pad successor!");
470dff0c46cSDimitry Andric         TBB = *SI;
471dff0c46cSDimitry Andric       }
472dff0c46cSDimitry Andric 
4733ca95b02SDimitry Andric       // If there is no non-landing-pad successor, the block has no fall-through
4743ca95b02SDimitry Andric       // edges to be concerned with.
475dff0c46cSDimitry Andric       if (!TBB)
476dff0c46cSDimitry Andric         return;
477dff0c46cSDimitry Andric 
478dff0c46cSDimitry Andric       // Finally update the unconditional successor to be reached via a branch
479dff0c46cSDimitry Andric       // if it would not be reached by fallthrough.
480f22ef01cSRoman Divacky       if (!isLayoutSuccessor(TBB))
481d88c1a5aSDimitry Andric         TII->insertBranch(*this, TBB, nullptr, Cond, DL);
482f22ef01cSRoman Divacky     }
4833ca95b02SDimitry Andric     return;
4843ca95b02SDimitry Andric   }
4853ca95b02SDimitry Andric 
486f22ef01cSRoman Divacky   if (FBB) {
487f22ef01cSRoman Divacky     // The block has a non-fallthrough conditional branch. If one of its
488f22ef01cSRoman Divacky     // successors is its layout successor, rewrite it to a fallthrough
489f22ef01cSRoman Divacky     // conditional branch.
490f22ef01cSRoman Divacky     if (isLayoutSuccessor(TBB)) {
491d88c1a5aSDimitry Andric       if (TII->reverseBranchCondition(Cond))
492f22ef01cSRoman Divacky         return;
493d88c1a5aSDimitry Andric       TII->removeBranch(*this);
494d88c1a5aSDimitry Andric       TII->insertBranch(*this, FBB, nullptr, Cond, DL);
495f22ef01cSRoman Divacky     } else if (isLayoutSuccessor(FBB)) {
496d88c1a5aSDimitry Andric       TII->removeBranch(*this);
497d88c1a5aSDimitry Andric       TII->insertBranch(*this, TBB, nullptr, Cond, DL);
498f22ef01cSRoman Divacky     }
4993ca95b02SDimitry Andric     return;
5003ca95b02SDimitry Andric   }
5013ca95b02SDimitry Andric 
5023ca95b02SDimitry Andric   // Walk through the successors and find the successor which is not a landing
5033ca95b02SDimitry Andric   // pad and is not the conditional branch destination (in TBB) as the
5043ca95b02SDimitry Andric   // fallthrough successor.
50591bc56edSDimitry Andric   MachineBasicBlock *FallthroughBB = nullptr;
506cb4dff85SDimitry Andric   for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) {
5077d523365SDimitry Andric     if ((*SI)->isEHPad() || *SI == TBB)
508cb4dff85SDimitry Andric       continue;
509cb4dff85SDimitry Andric     assert(!FallthroughBB && "Found more than one fallthrough successor.");
510cb4dff85SDimitry Andric     FallthroughBB = *SI;
511cb4dff85SDimitry Andric   }
5123ca95b02SDimitry Andric 
5133ca95b02SDimitry Andric   if (!FallthroughBB) {
5143ca95b02SDimitry Andric     if (canFallThrough()) {
5153ca95b02SDimitry Andric       // We fallthrough to the same basic block as the conditional jump targets.
5163ca95b02SDimitry Andric       // Remove the conditional jump, leaving unconditional fallthrough.
5173ca95b02SDimitry Andric       // FIXME: This does not seem like a reasonable pattern to support, but it
5183ca95b02SDimitry Andric       // has been seen in the wild coming out of degenerate ARM test cases.
519d88c1a5aSDimitry Andric       TII->removeBranch(*this);
520cb4dff85SDimitry Andric 
5213ca95b02SDimitry Andric       // Finally update the unconditional successor to be reached via a branch if
5223ca95b02SDimitry Andric       // it would not be reached by fallthrough.
523cb4dff85SDimitry Andric       if (!isLayoutSuccessor(TBB))
524d88c1a5aSDimitry Andric         TII->insertBranch(*this, TBB, nullptr, Cond, DL);
525cb4dff85SDimitry Andric       return;
526cb4dff85SDimitry Andric     }
527cb4dff85SDimitry Andric 
5283ca95b02SDimitry Andric     // We enter here iff exactly one successor is TBB which cannot fallthrough
5293ca95b02SDimitry Andric     // and the rest successors if any are EHPads.  In this case, we need to
5303ca95b02SDimitry Andric     // change the conditional branch into unconditional branch.
531d88c1a5aSDimitry Andric     TII->removeBranch(*this);
5323ca95b02SDimitry Andric     Cond.clear();
533d88c1a5aSDimitry Andric     TII->insertBranch(*this, TBB, nullptr, Cond, DL);
5343ca95b02SDimitry Andric     return;
5353ca95b02SDimitry Andric   }
5363ca95b02SDimitry Andric 
537f22ef01cSRoman Divacky   // The block has a fallthrough conditional branch.
538f22ef01cSRoman Divacky   if (isLayoutSuccessor(TBB)) {
539d88c1a5aSDimitry Andric     if (TII->reverseBranchCondition(Cond)) {
540f22ef01cSRoman Divacky       // We can't reverse the condition, add an unconditional branch.
541f22ef01cSRoman Divacky       Cond.clear();
542d88c1a5aSDimitry Andric       TII->insertBranch(*this, FallthroughBB, nullptr, Cond, DL);
543f22ef01cSRoman Divacky       return;
544f22ef01cSRoman Divacky     }
545d88c1a5aSDimitry Andric     TII->removeBranch(*this);
546d88c1a5aSDimitry Andric     TII->insertBranch(*this, FallthroughBB, nullptr, Cond, DL);
547cb4dff85SDimitry Andric   } else if (!isLayoutSuccessor(FallthroughBB)) {
548d88c1a5aSDimitry Andric     TII->removeBranch(*this);
549d88c1a5aSDimitry Andric     TII->insertBranch(*this, TBB, FallthroughBB, Cond, DL);
550f22ef01cSRoman Divacky   }
551f22ef01cSRoman Divacky }
552f22ef01cSRoman Divacky 
5537d523365SDimitry Andric void MachineBasicBlock::validateSuccProbs() const {
5547d523365SDimitry Andric #ifndef NDEBUG
5557d523365SDimitry Andric   int64_t Sum = 0;
5567d523365SDimitry Andric   for (auto Prob : Probs)
5577d523365SDimitry Andric     Sum += Prob.getNumerator();
5587d523365SDimitry Andric   // Due to precision issue, we assume that the sum of probabilities is one if
5597d523365SDimitry Andric   // the difference between the sum of their numerators and the denominator is
5607d523365SDimitry Andric   // no greater than the number of successors.
5617d523365SDimitry Andric   assert((uint64_t)std::abs(Sum - BranchProbability::getDenominator()) <=
5627d523365SDimitry Andric              Probs.size() &&
5637d523365SDimitry Andric          "The sum of successors's probabilities exceeds one.");
5647d523365SDimitry Andric #endif // NDEBUG
565f22ef01cSRoman Divacky }
566f22ef01cSRoman Divacky 
5677d523365SDimitry Andric void MachineBasicBlock::addSuccessor(MachineBasicBlock *Succ,
5687d523365SDimitry Andric                                      BranchProbability Prob) {
5697d523365SDimitry Andric   // Probability list is either empty (if successor list isn't empty, this means
5707d523365SDimitry Andric   // disabled optimization) or has the same size as successor list.
5717d523365SDimitry Andric   if (!(Probs.empty() && !Successors.empty()))
5727d523365SDimitry Andric     Probs.push_back(Prob);
5737d523365SDimitry Andric   Successors.push_back(Succ);
5747d523365SDimitry Andric   Succ->addPredecessor(this);
57517a519f9SDimitry Andric }
57617a519f9SDimitry Andric 
5777d523365SDimitry Andric void MachineBasicBlock::addSuccessorWithoutProb(MachineBasicBlock *Succ) {
5787d523365SDimitry Andric   // We need to make sure probability list is either empty or has the same size
5797d523365SDimitry Andric   // of successor list. When this function is called, we can safely delete all
5807d523365SDimitry Andric   // probability in the list.
5817d523365SDimitry Andric   Probs.clear();
5827d523365SDimitry Andric   Successors.push_back(Succ);
5837d523365SDimitry Andric   Succ->addPredecessor(this);
5847d523365SDimitry Andric }
5857d523365SDimitry Andric 
5867d523365SDimitry Andric void MachineBasicBlock::removeSuccessor(MachineBasicBlock *Succ,
5877d523365SDimitry Andric                                         bool NormalizeSuccProbs) {
588d88c1a5aSDimitry Andric   succ_iterator I = find(Successors, Succ);
5897d523365SDimitry Andric   removeSuccessor(I, NormalizeSuccProbs);
590f22ef01cSRoman Divacky }
591f22ef01cSRoman Divacky 
592f22ef01cSRoman Divacky MachineBasicBlock::succ_iterator
5937d523365SDimitry Andric MachineBasicBlock::removeSuccessor(succ_iterator I, bool NormalizeSuccProbs) {
594f22ef01cSRoman Divacky   assert(I != Successors.end() && "Not a current successor!");
59517a519f9SDimitry Andric 
5967d523365SDimitry Andric   // If probability list is empty it means we don't use it (disabled
5977d523365SDimitry Andric   // optimization).
5987d523365SDimitry Andric   if (!Probs.empty()) {
5997d523365SDimitry Andric     probability_iterator WI = getProbabilityIterator(I);
6007d523365SDimitry Andric     Probs.erase(WI);
6017d523365SDimitry Andric     if (NormalizeSuccProbs)
6027d523365SDimitry Andric       normalizeSuccProbs();
60317a519f9SDimitry Andric   }
60417a519f9SDimitry Andric 
605f22ef01cSRoman Divacky   (*I)->removePredecessor(this);
606f22ef01cSRoman Divacky   return Successors.erase(I);
607f22ef01cSRoman Divacky }
608f22ef01cSRoman Divacky 
60917a519f9SDimitry Andric void MachineBasicBlock::replaceSuccessor(MachineBasicBlock *Old,
61017a519f9SDimitry Andric                                          MachineBasicBlock *New) {
6117ae0e2c9SDimitry Andric   if (Old == New)
6127ae0e2c9SDimitry Andric     return;
61317a519f9SDimitry Andric 
6147ae0e2c9SDimitry Andric   succ_iterator E = succ_end();
6157ae0e2c9SDimitry Andric   succ_iterator NewI = E;
6167ae0e2c9SDimitry Andric   succ_iterator OldI = E;
6177ae0e2c9SDimitry Andric   for (succ_iterator I = succ_begin(); I != E; ++I) {
6187ae0e2c9SDimitry Andric     if (*I == Old) {
6197ae0e2c9SDimitry Andric       OldI = I;
6207ae0e2c9SDimitry Andric       if (NewI != E)
6217ae0e2c9SDimitry Andric         break;
6227ae0e2c9SDimitry Andric     }
6237ae0e2c9SDimitry Andric     if (*I == New) {
6247ae0e2c9SDimitry Andric       NewI = I;
6257ae0e2c9SDimitry Andric       if (OldI != E)
6267ae0e2c9SDimitry Andric         break;
6277ae0e2c9SDimitry Andric     }
6287ae0e2c9SDimitry Andric   }
6297ae0e2c9SDimitry Andric   assert(OldI != E && "Old is not a successor of this block");
6307ae0e2c9SDimitry Andric 
6317ae0e2c9SDimitry Andric   // If New isn't already a successor, let it take Old's place.
6327ae0e2c9SDimitry Andric   if (NewI == E) {
6337d523365SDimitry Andric     Old->removePredecessor(this);
6347ae0e2c9SDimitry Andric     New->addPredecessor(this);
6357ae0e2c9SDimitry Andric     *OldI = New;
6367ae0e2c9SDimitry Andric     return;
63717a519f9SDimitry Andric   }
63817a519f9SDimitry Andric 
6397ae0e2c9SDimitry Andric   // New is already a successor.
6407d523365SDimitry Andric   // Update its probability instead of adding a duplicate edge.
6417d523365SDimitry Andric   if (!Probs.empty()) {
6427d523365SDimitry Andric     auto ProbIter = getProbabilityIterator(NewI);
6437d523365SDimitry Andric     if (!ProbIter->isUnknown())
6447d523365SDimitry Andric       *ProbIter += *getProbabilityIterator(OldI);
6457ae0e2c9SDimitry Andric   }
6467d523365SDimitry Andric   removeSuccessor(OldI);
64717a519f9SDimitry Andric }
64817a519f9SDimitry Andric 
6497d523365SDimitry Andric void MachineBasicBlock::addPredecessor(MachineBasicBlock *Pred) {
6507d523365SDimitry Andric   Predecessors.push_back(Pred);
651f22ef01cSRoman Divacky }
652f22ef01cSRoman Divacky 
6537d523365SDimitry Andric void MachineBasicBlock::removePredecessor(MachineBasicBlock *Pred) {
654d88c1a5aSDimitry Andric   pred_iterator I = find(Predecessors, Pred);
655f22ef01cSRoman Divacky   assert(I != Predecessors.end() && "Pred is not a predecessor of this block!");
656f22ef01cSRoman Divacky   Predecessors.erase(I);
657f22ef01cSRoman Divacky }
658f22ef01cSRoman Divacky 
6597d523365SDimitry Andric void MachineBasicBlock::transferSuccessors(MachineBasicBlock *FromMBB) {
6607d523365SDimitry Andric   if (this == FromMBB)
661f22ef01cSRoman Divacky     return;
662f22ef01cSRoman Divacky 
6637d523365SDimitry Andric   while (!FromMBB->succ_empty()) {
6647d523365SDimitry Andric     MachineBasicBlock *Succ = *FromMBB->succ_begin();
66517a519f9SDimitry Andric 
6667d523365SDimitry Andric     // If probability list is empty it means we don't use it (disabled optimization).
6677d523365SDimitry Andric     if (!FromMBB->Probs.empty()) {
6687d523365SDimitry Andric       auto Prob = *FromMBB->Probs.begin();
6697d523365SDimitry Andric       addSuccessor(Succ, Prob);
6707d523365SDimitry Andric     } else
6717d523365SDimitry Andric       addSuccessorWithoutProb(Succ);
67217a519f9SDimitry Andric 
6737d523365SDimitry Andric     FromMBB->removeSuccessor(Succ);
674ffd1746dSEd Schouten   }
675ffd1746dSEd Schouten }
676f22ef01cSRoman Divacky 
677ffd1746dSEd Schouten void
6787d523365SDimitry Andric MachineBasicBlock::transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB) {
6797d523365SDimitry Andric   if (this == FromMBB)
680ffd1746dSEd Schouten     return;
681ffd1746dSEd Schouten 
6827d523365SDimitry Andric   while (!FromMBB->succ_empty()) {
6837d523365SDimitry Andric     MachineBasicBlock *Succ = *FromMBB->succ_begin();
6847d523365SDimitry Andric     if (!FromMBB->Probs.empty()) {
6857d523365SDimitry Andric       auto Prob = *FromMBB->Probs.begin();
6867d523365SDimitry Andric       addSuccessor(Succ, Prob);
6877d523365SDimitry Andric     } else
6887d523365SDimitry Andric       addSuccessorWithoutProb(Succ);
6897d523365SDimitry Andric     FromMBB->removeSuccessor(Succ);
690ffd1746dSEd Schouten 
691ffd1746dSEd Schouten     // Fix up any PHI nodes in the successor.
692dff0c46cSDimitry Andric     for (MachineBasicBlock::instr_iterator MI = Succ->instr_begin(),
693dff0c46cSDimitry Andric            ME = Succ->instr_end(); MI != ME && MI->isPHI(); ++MI)
694ffd1746dSEd Schouten       for (unsigned i = 2, e = MI->getNumOperands()+1; i != e; i += 2) {
695ffd1746dSEd Schouten         MachineOperand &MO = MI->getOperand(i);
6967d523365SDimitry Andric         if (MO.getMBB() == FromMBB)
697ffd1746dSEd Schouten           MO.setMBB(this);
698ffd1746dSEd Schouten       }
699ffd1746dSEd Schouten   }
7007d523365SDimitry Andric   normalizeSuccProbs();
701f22ef01cSRoman Divacky }
702f22ef01cSRoman Divacky 
7037ae0e2c9SDimitry Andric bool MachineBasicBlock::isPredecessor(const MachineBasicBlock *MBB) const {
704d88c1a5aSDimitry Andric   return is_contained(predecessors(), MBB);
7057ae0e2c9SDimitry Andric }
7067ae0e2c9SDimitry Andric 
707f22ef01cSRoman Divacky bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const {
708d88c1a5aSDimitry Andric   return is_contained(successors(), MBB);
709f22ef01cSRoman Divacky }
710f22ef01cSRoman Divacky 
711f22ef01cSRoman Divacky bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const {
712f22ef01cSRoman Divacky   MachineFunction::const_iterator I(this);
71391bc56edSDimitry Andric   return std::next(I) == MachineFunction::const_iterator(MBB);
714f22ef01cSRoman Divacky }
715f22ef01cSRoman Divacky 
7167a7e6055SDimitry Andric MachineBasicBlock *MachineBasicBlock::getFallThrough() {
7177d523365SDimitry Andric   MachineFunction::iterator Fallthrough = getIterator();
718f22ef01cSRoman Divacky   ++Fallthrough;
719f22ef01cSRoman Divacky   // If FallthroughBlock is off the end of the function, it can't fall through.
720f22ef01cSRoman Divacky   if (Fallthrough == getParent()->end())
7217a7e6055SDimitry Andric     return nullptr;
722f22ef01cSRoman Divacky 
723f22ef01cSRoman Divacky   // If FallthroughBlock isn't a successor, no fallthrough is possible.
7247d523365SDimitry Andric   if (!isSuccessor(&*Fallthrough))
7257a7e6055SDimitry Andric     return nullptr;
726f22ef01cSRoman Divacky 
727f22ef01cSRoman Divacky   // Analyze the branches, if any, at the end of the block.
72891bc56edSDimitry Andric   MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
729f22ef01cSRoman Divacky   SmallVector<MachineOperand, 4> Cond;
73039d628a0SDimitry Andric   const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo();
7313ca95b02SDimitry Andric   if (TII->analyzeBranch(*this, TBB, FBB, Cond)) {
732f22ef01cSRoman Divacky     // If we couldn't analyze the branch, examine the last instruction.
733f22ef01cSRoman Divacky     // If the block doesn't end in a known control barrier, assume fallthrough
734dff0c46cSDimitry Andric     // is possible. The isPredicated check is needed because this code can be
735f22ef01cSRoman Divacky     // called during IfConversion, where an instruction which is normally a
736dff0c46cSDimitry Andric     // Barrier is predicated and thus no longer an actual control barrier.
7377a7e6055SDimitry Andric     return (empty() || !back().isBarrier() || TII->isPredicated(back()))
7387a7e6055SDimitry Andric                ? &*Fallthrough
7397a7e6055SDimitry Andric                : nullptr;
740f22ef01cSRoman Divacky   }
741f22ef01cSRoman Divacky 
742f22ef01cSRoman Divacky   // If there is no branch, control always falls through.
7437a7e6055SDimitry Andric   if (!TBB) return &*Fallthrough;
744f22ef01cSRoman Divacky 
745f22ef01cSRoman Divacky   // If there is some explicit branch to the fallthrough block, it can obviously
746f22ef01cSRoman Divacky   // reach, even though the branch should get folded to fall through implicitly.
747f22ef01cSRoman Divacky   if (MachineFunction::iterator(TBB) == Fallthrough ||
748f22ef01cSRoman Divacky       MachineFunction::iterator(FBB) == Fallthrough)
7497a7e6055SDimitry Andric     return &*Fallthrough;
750f22ef01cSRoman Divacky 
751f22ef01cSRoman Divacky   // If it's an unconditional branch to some block not the fall through, it
752f22ef01cSRoman Divacky   // doesn't fall through.
7537a7e6055SDimitry Andric   if (Cond.empty()) return nullptr;
754f22ef01cSRoman Divacky 
755f22ef01cSRoman Divacky   // Otherwise, if it is conditional and has no explicit false block, it falls
756f22ef01cSRoman Divacky   // through.
7577a7e6055SDimitry Andric   return (FBB == nullptr) ? &*Fallthrough : nullptr;
7587a7e6055SDimitry Andric }
7597a7e6055SDimitry Andric 
7607a7e6055SDimitry Andric bool MachineBasicBlock::canFallThrough() {
7617a7e6055SDimitry Andric   return getFallThrough() != nullptr;
762f22ef01cSRoman Divacky }
763f22ef01cSRoman Divacky 
7643ca95b02SDimitry Andric MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ,
7653ca95b02SDimitry Andric                                                         Pass &P) {
7663ca95b02SDimitry Andric   if (!canSplitCriticalEdge(Succ))
76791bc56edSDimitry Andric     return nullptr;
7687ae0e2c9SDimitry Andric 
769ffd1746dSEd Schouten   MachineFunction *MF = getParent();
7707d523365SDimitry Andric   DebugLoc DL;  // FIXME: this is nowhere
771ffd1746dSEd Schouten 
772ffd1746dSEd Schouten   MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock();
77391bc56edSDimitry Andric   MF->insert(std::next(MachineFunction::iterator(this)), NMBB);
7742cab237bSDimitry Andric   DEBUG(dbgs() << "Splitting critical edge: " << printMBBReference(*this)
7752cab237bSDimitry Andric                << " -- " << printMBBReference(*NMBB) << " -- "
7762cab237bSDimitry Andric                << printMBBReference(*Succ) << '\n');
777ffd1746dSEd Schouten 
7783ca95b02SDimitry Andric   LiveIntervals *LIS = P.getAnalysisIfAvailable<LiveIntervals>();
7793ca95b02SDimitry Andric   SlotIndexes *Indexes = P.getAnalysisIfAvailable<SlotIndexes>();
780139f7f9bSDimitry Andric   if (LIS)
781139f7f9bSDimitry Andric     LIS->insertMBBInMaps(NMBB);
782139f7f9bSDimitry Andric   else if (Indexes)
783139f7f9bSDimitry Andric     Indexes->insertMBBInMaps(NMBB);
784139f7f9bSDimitry Andric 
785bd5abe19SDimitry Andric   // On some targets like Mips, branches may kill virtual registers. Make sure
786bd5abe19SDimitry Andric   // that LiveVariables is properly updated after updateTerminator replaces the
787bd5abe19SDimitry Andric   // terminators.
7883ca95b02SDimitry Andric   LiveVariables *LV = P.getAnalysisIfAvailable<LiveVariables>();
789bd5abe19SDimitry Andric 
790bd5abe19SDimitry Andric   // Collect a list of virtual registers killed by the terminators.
791bd5abe19SDimitry Andric   SmallVector<unsigned, 4> KilledRegs;
792bd5abe19SDimitry Andric   if (LV)
793dff0c46cSDimitry Andric     for (instr_iterator I = getFirstInstrTerminator(), E = instr_end();
794dff0c46cSDimitry Andric          I != E; ++I) {
7957d523365SDimitry Andric       MachineInstr *MI = &*I;
796bd5abe19SDimitry Andric       for (MachineInstr::mop_iterator OI = MI->operands_begin(),
797bd5abe19SDimitry Andric            OE = MI->operands_end(); OI != OE; ++OI) {
798dff0c46cSDimitry Andric         if (!OI->isReg() || OI->getReg() == 0 ||
799dff0c46cSDimitry Andric             !OI->isUse() || !OI->isKill() || OI->isUndef())
800bd5abe19SDimitry Andric           continue;
801bd5abe19SDimitry Andric         unsigned Reg = OI->getReg();
802dff0c46cSDimitry Andric         if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
8033ca95b02SDimitry Andric             LV->getVarInfo(Reg).removeKill(*MI)) {
804bd5abe19SDimitry Andric           KilledRegs.push_back(Reg);
805bd5abe19SDimitry Andric           DEBUG(dbgs() << "Removing terminator kill: " << *MI);
806bd5abe19SDimitry Andric           OI->setIsKill(false);
807bd5abe19SDimitry Andric         }
808bd5abe19SDimitry Andric       }
809bd5abe19SDimitry Andric     }
810bd5abe19SDimitry Andric 
811139f7f9bSDimitry Andric   SmallVector<unsigned, 4> UsedRegs;
812139f7f9bSDimitry Andric   if (LIS) {
813139f7f9bSDimitry Andric     for (instr_iterator I = getFirstInstrTerminator(), E = instr_end();
814139f7f9bSDimitry Andric          I != E; ++I) {
8157d523365SDimitry Andric       MachineInstr *MI = &*I;
816139f7f9bSDimitry Andric 
817139f7f9bSDimitry Andric       for (MachineInstr::mop_iterator OI = MI->operands_begin(),
818139f7f9bSDimitry Andric            OE = MI->operands_end(); OI != OE; ++OI) {
819139f7f9bSDimitry Andric         if (!OI->isReg() || OI->getReg() == 0)
820139f7f9bSDimitry Andric           continue;
821139f7f9bSDimitry Andric 
822139f7f9bSDimitry Andric         unsigned Reg = OI->getReg();
823d88c1a5aSDimitry Andric         if (!is_contained(UsedRegs, Reg))
824139f7f9bSDimitry Andric           UsedRegs.push_back(Reg);
825139f7f9bSDimitry Andric       }
826139f7f9bSDimitry Andric     }
827139f7f9bSDimitry Andric   }
828139f7f9bSDimitry Andric 
829ffd1746dSEd Schouten   ReplaceUsesOfBlockWith(Succ, NMBB);
830139f7f9bSDimitry Andric 
831139f7f9bSDimitry Andric   // If updateTerminator() removes instructions, we need to remove them from
832139f7f9bSDimitry Andric   // SlotIndexes.
833139f7f9bSDimitry Andric   SmallVector<MachineInstr*, 4> Terminators;
834139f7f9bSDimitry Andric   if (Indexes) {
835139f7f9bSDimitry Andric     for (instr_iterator I = getFirstInstrTerminator(), E = instr_end();
836139f7f9bSDimitry Andric          I != E; ++I)
8377d523365SDimitry Andric       Terminators.push_back(&*I);
838139f7f9bSDimitry Andric   }
839139f7f9bSDimitry Andric 
840ffd1746dSEd Schouten   updateTerminator();
841ffd1746dSEd Schouten 
842139f7f9bSDimitry Andric   if (Indexes) {
843139f7f9bSDimitry Andric     SmallVector<MachineInstr*, 4> NewTerminators;
844139f7f9bSDimitry Andric     for (instr_iterator I = getFirstInstrTerminator(), E = instr_end();
845139f7f9bSDimitry Andric          I != E; ++I)
8467d523365SDimitry Andric       NewTerminators.push_back(&*I);
847139f7f9bSDimitry Andric 
848139f7f9bSDimitry Andric     for (SmallVectorImpl<MachineInstr*>::iterator I = Terminators.begin(),
849139f7f9bSDimitry Andric         E = Terminators.end(); I != E; ++I) {
850d88c1a5aSDimitry Andric       if (!is_contained(NewTerminators, *I))
8513ca95b02SDimitry Andric         Indexes->removeMachineInstrFromMaps(**I);
852139f7f9bSDimitry Andric     }
853139f7f9bSDimitry Andric   }
854139f7f9bSDimitry Andric 
855ffd1746dSEd Schouten   // Insert unconditional "jump Succ" instruction in NMBB if necessary.
856ffd1746dSEd Schouten   NMBB->addSuccessor(Succ);
857ffd1746dSEd Schouten   if (!NMBB->isLayoutSuccessor(Succ)) {
8583ca95b02SDimitry Andric     SmallVector<MachineOperand, 4> Cond;
8593ca95b02SDimitry Andric     const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo();
860d88c1a5aSDimitry Andric     TII->insertBranch(*NMBB, Succ, nullptr, Cond, DL);
861139f7f9bSDimitry Andric 
862139f7f9bSDimitry Andric     if (Indexes) {
8633ca95b02SDimitry Andric       for (MachineInstr &MI : NMBB->instrs()) {
864139f7f9bSDimitry Andric         // Some instructions may have been moved to NMBB by updateTerminator(),
865139f7f9bSDimitry Andric         // so we first remove any instruction that already has an index.
8663ca95b02SDimitry Andric         if (Indexes->hasIndex(MI))
8673ca95b02SDimitry Andric           Indexes->removeMachineInstrFromMaps(MI);
8683ca95b02SDimitry Andric         Indexes->insertMachineInstrInMaps(MI);
869139f7f9bSDimitry Andric       }
870139f7f9bSDimitry Andric     }
871ffd1746dSEd Schouten   }
872ffd1746dSEd Schouten 
873ffd1746dSEd Schouten   // Fix PHI nodes in Succ so they refer to NMBB instead of this
874dff0c46cSDimitry Andric   for (MachineBasicBlock::instr_iterator
875dff0c46cSDimitry Andric          i = Succ->instr_begin(),e = Succ->instr_end();
876ffd1746dSEd Schouten        i != e && i->isPHI(); ++i)
877ffd1746dSEd Schouten     for (unsigned ni = 1, ne = i->getNumOperands(); ni != ne; ni += 2)
878ffd1746dSEd Schouten       if (i->getOperand(ni+1).getMBB() == this)
879ffd1746dSEd Schouten         i->getOperand(ni+1).setMBB(NMBB);
880ffd1746dSEd Schouten 
8816122f3e6SDimitry Andric   // Inherit live-ins from the successor
8827d523365SDimitry Andric   for (const auto &LI : Succ->liveins())
8837d523365SDimitry Andric     NMBB->addLiveIn(LI);
8846122f3e6SDimitry Andric 
885bd5abe19SDimitry Andric   // Update LiveVariables.
88639d628a0SDimitry Andric   const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
887bd5abe19SDimitry Andric   if (LV) {
888bd5abe19SDimitry Andric     // Restore kills of virtual registers that were killed by the terminators.
889bd5abe19SDimitry Andric     while (!KilledRegs.empty()) {
890bd5abe19SDimitry Andric       unsigned Reg = KilledRegs.pop_back_val();
891dff0c46cSDimitry Andric       for (instr_iterator I = instr_end(), E = instr_begin(); I != E;) {
892dff0c46cSDimitry Andric         if (!(--I)->addRegisterKilled(Reg, TRI, /* addIfNotFound= */ false))
893bd5abe19SDimitry Andric           continue;
894dff0c46cSDimitry Andric         if (TargetRegisterInfo::isVirtualRegister(Reg))
8957d523365SDimitry Andric           LV->getVarInfo(Reg).Kills.push_back(&*I);
896bd5abe19SDimitry Andric         DEBUG(dbgs() << "Restored terminator kill: " << *I);
897bd5abe19SDimitry Andric         break;
898bd5abe19SDimitry Andric       }
899bd5abe19SDimitry Andric     }
900bd5abe19SDimitry Andric     // Update relevant live-through information.
901ffd1746dSEd Schouten     LV->addNewBlock(NMBB, this, Succ);
902bd5abe19SDimitry Andric   }
903ffd1746dSEd Schouten 
904139f7f9bSDimitry Andric   if (LIS) {
905139f7f9bSDimitry Andric     // After splitting the edge and updating SlotIndexes, live intervals may be
906139f7f9bSDimitry Andric     // in one of two situations, depending on whether this block was the last in
9077d523365SDimitry Andric     // the function. If the original block was the last in the function, all
9087d523365SDimitry Andric     // live intervals will end prior to the beginning of the new split block. If
9097d523365SDimitry Andric     // the original block was not at the end of the function, all live intervals
9107d523365SDimitry Andric     // will extend to the end of the new split block.
911139f7f9bSDimitry Andric 
912139f7f9bSDimitry Andric     bool isLastMBB =
91391bc56edSDimitry Andric       std::next(MachineFunction::iterator(NMBB)) == getParent()->end();
914139f7f9bSDimitry Andric 
915139f7f9bSDimitry Andric     SlotIndex StartIndex = Indexes->getMBBEndIdx(this);
916139f7f9bSDimitry Andric     SlotIndex PrevIndex = StartIndex.getPrevSlot();
917139f7f9bSDimitry Andric     SlotIndex EndIndex = Indexes->getMBBEndIdx(NMBB);
918139f7f9bSDimitry Andric 
919139f7f9bSDimitry Andric     // Find the registers used from NMBB in PHIs in Succ.
920139f7f9bSDimitry Andric     SmallSet<unsigned, 8> PHISrcRegs;
921139f7f9bSDimitry Andric     for (MachineBasicBlock::instr_iterator
922139f7f9bSDimitry Andric          I = Succ->instr_begin(), E = Succ->instr_end();
923139f7f9bSDimitry Andric          I != E && I->isPHI(); ++I) {
924139f7f9bSDimitry Andric       for (unsigned ni = 1, ne = I->getNumOperands(); ni != ne; ni += 2) {
925139f7f9bSDimitry Andric         if (I->getOperand(ni+1).getMBB() == NMBB) {
926139f7f9bSDimitry Andric           MachineOperand &MO = I->getOperand(ni);
927139f7f9bSDimitry Andric           unsigned Reg = MO.getReg();
928139f7f9bSDimitry Andric           PHISrcRegs.insert(Reg);
929139f7f9bSDimitry Andric           if (MO.isUndef())
930139f7f9bSDimitry Andric             continue;
931139f7f9bSDimitry Andric 
932139f7f9bSDimitry Andric           LiveInterval &LI = LIS->getInterval(Reg);
933139f7f9bSDimitry Andric           VNInfo *VNI = LI.getVNInfoAt(PrevIndex);
9347d523365SDimitry Andric           assert(VNI &&
9357d523365SDimitry Andric                  "PHI sources should be live out of their predecessors.");
936f785676fSDimitry Andric           LI.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI));
937139f7f9bSDimitry Andric         }
938139f7f9bSDimitry Andric       }
939139f7f9bSDimitry Andric     }
940139f7f9bSDimitry Andric 
941139f7f9bSDimitry Andric     MachineRegisterInfo *MRI = &getParent()->getRegInfo();
942139f7f9bSDimitry Andric     for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
943139f7f9bSDimitry Andric       unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
944139f7f9bSDimitry Andric       if (PHISrcRegs.count(Reg) || !LIS->hasInterval(Reg))
945139f7f9bSDimitry Andric         continue;
946139f7f9bSDimitry Andric 
947139f7f9bSDimitry Andric       LiveInterval &LI = LIS->getInterval(Reg);
948139f7f9bSDimitry Andric       if (!LI.liveAt(PrevIndex))
949139f7f9bSDimitry Andric         continue;
950139f7f9bSDimitry Andric 
951139f7f9bSDimitry Andric       bool isLiveOut = LI.liveAt(LIS->getMBBStartIdx(Succ));
952139f7f9bSDimitry Andric       if (isLiveOut && isLastMBB) {
953139f7f9bSDimitry Andric         VNInfo *VNI = LI.getVNInfoAt(PrevIndex);
954139f7f9bSDimitry Andric         assert(VNI && "LiveInterval should have VNInfo where it is live.");
955f785676fSDimitry Andric         LI.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI));
956139f7f9bSDimitry Andric       } else if (!isLiveOut && !isLastMBB) {
957f785676fSDimitry Andric         LI.removeSegment(StartIndex, EndIndex);
958139f7f9bSDimitry Andric       }
959139f7f9bSDimitry Andric     }
960139f7f9bSDimitry Andric 
961139f7f9bSDimitry Andric     // Update all intervals for registers whose uses may have been modified by
962139f7f9bSDimitry Andric     // updateTerminator().
963139f7f9bSDimitry Andric     LIS->repairIntervalsInRange(this, getFirstTerminator(), end(), UsedRegs);
964139f7f9bSDimitry Andric   }
965139f7f9bSDimitry Andric 
966ffd1746dSEd Schouten   if (MachineDominatorTree *MDT =
9673ca95b02SDimitry Andric           P.getAnalysisIfAvailable<MachineDominatorTree>())
96839d628a0SDimitry Andric     MDT->recordSplitCriticalEdge(this, Succ, NMBB);
969e580952dSDimitry Andric 
9703ca95b02SDimitry Andric   if (MachineLoopInfo *MLI = P.getAnalysisIfAvailable<MachineLoopInfo>())
971ffd1746dSEd Schouten     if (MachineLoop *TIL = MLI->getLoopFor(this)) {
972ffd1746dSEd Schouten       // If one or the other blocks were not in a loop, the new block is not
973ffd1746dSEd Schouten       // either, and thus LI doesn't need to be updated.
974ffd1746dSEd Schouten       if (MachineLoop *DestLoop = MLI->getLoopFor(Succ)) {
975ffd1746dSEd Schouten         if (TIL == DestLoop) {
976ffd1746dSEd Schouten           // Both in the same loop, the NMBB joins loop.
977ffd1746dSEd Schouten           DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase());
978ffd1746dSEd Schouten         } else if (TIL->contains(DestLoop)) {
979ffd1746dSEd Schouten           // Edge from an outer loop to an inner loop.  Add to the outer loop.
980ffd1746dSEd Schouten           TIL->addBasicBlockToLoop(NMBB, MLI->getBase());
981ffd1746dSEd Schouten         } else if (DestLoop->contains(TIL)) {
982ffd1746dSEd Schouten           // Edge from an inner loop to an outer loop.  Add to the outer loop.
983ffd1746dSEd Schouten           DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase());
984ffd1746dSEd Schouten         } else {
985ffd1746dSEd Schouten           // Edge from two loops with no containment relation.  Because these
986ffd1746dSEd Schouten           // are natural loops, we know that the destination block must be the
987ffd1746dSEd Schouten           // header of its loop (adding a branch into a loop elsewhere would
988ffd1746dSEd Schouten           // create an irreducible loop).
989ffd1746dSEd Schouten           assert(DestLoop->getHeader() == Succ &&
990ffd1746dSEd Schouten                  "Should not create irreducible loops!");
991ffd1746dSEd Schouten           if (MachineLoop *P = DestLoop->getParentLoop())
992ffd1746dSEd Schouten             P->addBasicBlockToLoop(NMBB, MLI->getBase());
993ffd1746dSEd Schouten         }
994ffd1746dSEd Schouten       }
995ffd1746dSEd Schouten     }
996ffd1746dSEd Schouten 
997ffd1746dSEd Schouten   return NMBB;
998ffd1746dSEd Schouten }
999ffd1746dSEd Schouten 
10003ca95b02SDimitry Andric bool MachineBasicBlock::canSplitCriticalEdge(
10013ca95b02SDimitry Andric     const MachineBasicBlock *Succ) const {
10023ca95b02SDimitry Andric   // Splitting the critical edge to a landing pad block is non-trivial. Don't do
10033ca95b02SDimitry Andric   // it in this generic function.
10043ca95b02SDimitry Andric   if (Succ->isEHPad())
10053ca95b02SDimitry Andric     return false;
10063ca95b02SDimitry Andric 
10073ca95b02SDimitry Andric   const MachineFunction *MF = getParent();
10083ca95b02SDimitry Andric 
10093ca95b02SDimitry Andric   // Performance might be harmed on HW that implements branching using exec mask
10103ca95b02SDimitry Andric   // where both sides of the branches are always executed.
10113ca95b02SDimitry Andric   if (MF->getTarget().requiresStructuredCFG())
10123ca95b02SDimitry Andric     return false;
10133ca95b02SDimitry Andric 
10143ca95b02SDimitry Andric   // We may need to update this's terminator, but we can't do that if
10153ca95b02SDimitry Andric   // AnalyzeBranch fails. If this uses a jump table, we won't touch it.
10163ca95b02SDimitry Andric   const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
10173ca95b02SDimitry Andric   MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
10183ca95b02SDimitry Andric   SmallVector<MachineOperand, 4> Cond;
10193ca95b02SDimitry Andric   // AnalyzeBanch should modify this, since we did not allow modification.
10203ca95b02SDimitry Andric   if (TII->analyzeBranch(*const_cast<MachineBasicBlock *>(this), TBB, FBB, Cond,
10213ca95b02SDimitry Andric                          /*AllowModify*/ false))
10223ca95b02SDimitry Andric     return false;
10233ca95b02SDimitry Andric 
10243ca95b02SDimitry Andric   // Avoid bugpoint weirdness: A block may end with a conditional branch but
10253ca95b02SDimitry Andric   // jumps to the same MBB is either case. We have duplicate CFG edges in that
10263ca95b02SDimitry Andric   // case that we can't handle. Since this never happens in properly optimized
10273ca95b02SDimitry Andric   // code, just skip those edges.
10283ca95b02SDimitry Andric   if (TBB && TBB == FBB) {
10292cab237bSDimitry Andric     DEBUG(dbgs() << "Won't split critical edge after degenerate "
10302cab237bSDimitry Andric                  << printMBBReference(*this) << '\n');
10313ca95b02SDimitry Andric     return false;
10323ca95b02SDimitry Andric   }
10333ca95b02SDimitry Andric   return true;
10343ca95b02SDimitry Andric }
10353ca95b02SDimitry Andric 
1036139f7f9bSDimitry Andric /// Prepare MI to be removed from its bundle. This fixes bundle flags on MI's
1037139f7f9bSDimitry Andric /// neighboring instructions so the bundle won't be broken by removing MI.
1038139f7f9bSDimitry Andric static void unbundleSingleMI(MachineInstr *MI) {
1039139f7f9bSDimitry Andric   // Removing the first instruction in a bundle.
1040139f7f9bSDimitry Andric   if (MI->isBundledWithSucc() && !MI->isBundledWithPred())
1041139f7f9bSDimitry Andric     MI->unbundleFromSucc();
1042139f7f9bSDimitry Andric   // Removing the last instruction in a bundle.
1043139f7f9bSDimitry Andric   if (MI->isBundledWithPred() && !MI->isBundledWithSucc())
1044139f7f9bSDimitry Andric     MI->unbundleFromPred();
1045139f7f9bSDimitry Andric   // If MI is not bundled, or if it is internal to a bundle, the neighbor flags
1046139f7f9bSDimitry Andric   // are already fine.
1047dff0c46cSDimitry Andric }
1048dff0c46cSDimitry Andric 
1049139f7f9bSDimitry Andric MachineBasicBlock::instr_iterator
1050139f7f9bSDimitry Andric MachineBasicBlock::erase(MachineBasicBlock::instr_iterator I) {
10517d523365SDimitry Andric   unbundleSingleMI(&*I);
1052139f7f9bSDimitry Andric   return Insts.erase(I);
1053dff0c46cSDimitry Andric }
1054dff0c46cSDimitry Andric 
1055139f7f9bSDimitry Andric MachineInstr *MachineBasicBlock::remove_instr(MachineInstr *MI) {
1056139f7f9bSDimitry Andric   unbundleSingleMI(MI);
1057139f7f9bSDimitry Andric   MI->clearFlag(MachineInstr::BundledPred);
1058139f7f9bSDimitry Andric   MI->clearFlag(MachineInstr::BundledSucc);
1059139f7f9bSDimitry Andric   return Insts.remove(MI);
1060dff0c46cSDimitry Andric }
1061dff0c46cSDimitry Andric 
1062139f7f9bSDimitry Andric MachineBasicBlock::instr_iterator
1063139f7f9bSDimitry Andric MachineBasicBlock::insert(instr_iterator I, MachineInstr *MI) {
1064139f7f9bSDimitry Andric   assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
1065139f7f9bSDimitry Andric          "Cannot insert instruction with bundle flags");
1066139f7f9bSDimitry Andric   // Set the bundle flags when inserting inside a bundle.
1067139f7f9bSDimitry Andric   if (I != instr_end() && I->isBundledWithPred()) {
1068139f7f9bSDimitry Andric     MI->setFlag(MachineInstr::BundledPred);
1069139f7f9bSDimitry Andric     MI->setFlag(MachineInstr::BundledSucc);
1070dff0c46cSDimitry Andric   }
1071139f7f9bSDimitry Andric   return Insts.insert(I, MI);
1072dff0c46cSDimitry Andric }
1073dff0c46cSDimitry Andric 
10747d523365SDimitry Andric /// This method unlinks 'this' from the containing function, and returns it, but
10757d523365SDimitry Andric /// does not delete it.
1076f22ef01cSRoman Divacky MachineBasicBlock *MachineBasicBlock::removeFromParent() {
1077f22ef01cSRoman Divacky   assert(getParent() && "Not embedded in a function!");
1078f22ef01cSRoman Divacky   getParent()->remove(this);
1079f22ef01cSRoman Divacky   return this;
1080f22ef01cSRoman Divacky }
1081f22ef01cSRoman Divacky 
10827d523365SDimitry Andric /// This method unlinks 'this' from the containing function, and deletes it.
1083f22ef01cSRoman Divacky void MachineBasicBlock::eraseFromParent() {
1084f22ef01cSRoman Divacky   assert(getParent() && "Not embedded in a function!");
1085f22ef01cSRoman Divacky   getParent()->erase(this);
1086f22ef01cSRoman Divacky }
1087f22ef01cSRoman Divacky 
10887d523365SDimitry Andric /// Given a machine basic block that branched to 'Old', change the code and CFG
10897d523365SDimitry Andric /// so that it branches to 'New' instead.
1090f22ef01cSRoman Divacky void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old,
1091f22ef01cSRoman Divacky                                                MachineBasicBlock *New) {
1092f22ef01cSRoman Divacky   assert(Old != New && "Cannot replace self with self!");
1093f22ef01cSRoman Divacky 
1094dff0c46cSDimitry Andric   MachineBasicBlock::instr_iterator I = instr_end();
1095dff0c46cSDimitry Andric   while (I != instr_begin()) {
1096f22ef01cSRoman Divacky     --I;
1097dff0c46cSDimitry Andric     if (!I->isTerminator()) break;
1098f22ef01cSRoman Divacky 
1099f22ef01cSRoman Divacky     // Scan the operands of this machine instruction, replacing any uses of Old
1100f22ef01cSRoman Divacky     // with New.
1101f22ef01cSRoman Divacky     for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1102f22ef01cSRoman Divacky       if (I->getOperand(i).isMBB() &&
1103f22ef01cSRoman Divacky           I->getOperand(i).getMBB() == Old)
1104f22ef01cSRoman Divacky         I->getOperand(i).setMBB(New);
1105f22ef01cSRoman Divacky   }
1106f22ef01cSRoman Divacky 
1107f22ef01cSRoman Divacky   // Update the successor information.
110817a519f9SDimitry Andric   replaceSuccessor(Old, New);
1109f22ef01cSRoman Divacky }
1110f22ef01cSRoman Divacky 
11117d523365SDimitry Andric /// Various pieces of code can cause excess edges in the CFG to be inserted.  If
11127d523365SDimitry Andric /// we have proven that MBB can only branch to DestA and DestB, remove any other
11137d523365SDimitry Andric /// MBB successors from the CFG.  DestA and DestB can be null.
1114f22ef01cSRoman Divacky ///
1115f22ef01cSRoman Divacky /// Besides DestA and DestB, retain other edges leading to LandingPads
1116f22ef01cSRoman Divacky /// (currently there can be only one; we don't check or require that here).
1117f22ef01cSRoman Divacky /// Note it is possible that DestA and/or DestB are LandingPads.
1118f22ef01cSRoman Divacky bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA,
1119f22ef01cSRoman Divacky                                              MachineBasicBlock *DestB,
11207d523365SDimitry Andric                                              bool IsCond) {
1121f22ef01cSRoman Divacky   // The values of DestA and DestB frequently come from a call to the
1122f22ef01cSRoman Divacky   // 'TargetInstrInfo::AnalyzeBranch' method. We take our meaning of the initial
1123f22ef01cSRoman Divacky   // values from there.
1124f22ef01cSRoman Divacky   //
1125f22ef01cSRoman Divacky   // 1. If both DestA and DestB are null, then the block ends with no branches
1126f22ef01cSRoman Divacky   //    (it falls through to its successor).
11277d523365SDimitry Andric   // 2. If DestA is set, DestB is null, and IsCond is false, then the block ends
1128f22ef01cSRoman Divacky   //    with only an unconditional branch.
11297d523365SDimitry Andric   // 3. If DestA is set, DestB is null, and IsCond is true, then the block ends
1130f22ef01cSRoman Divacky   //    with a conditional branch that falls through to a successor (DestB).
11317d523365SDimitry Andric   // 4. If DestA and DestB is set and IsCond is true, then the block ends with a
1132f22ef01cSRoman Divacky   //    conditional branch followed by an unconditional branch. DestA is the
1133f22ef01cSRoman Divacky   //    'true' destination and DestB is the 'false' destination.
1134f22ef01cSRoman Divacky 
1135f22ef01cSRoman Divacky   bool Changed = false;
1136f22ef01cSRoman Divacky 
1137d88c1a5aSDimitry Andric   MachineBasicBlock *FallThru = getNextNode();
1138f22ef01cSRoman Divacky 
113991bc56edSDimitry Andric   if (!DestA && !DestB) {
1140f22ef01cSRoman Divacky     // Block falls through to successor.
1141d88c1a5aSDimitry Andric     DestA = FallThru;
1142d88c1a5aSDimitry Andric     DestB = FallThru;
114391bc56edSDimitry Andric   } else if (DestA && !DestB) {
11447d523365SDimitry Andric     if (IsCond)
1145f22ef01cSRoman Divacky       // Block ends in conditional jump that falls through to successor.
1146d88c1a5aSDimitry Andric       DestB = FallThru;
1147f22ef01cSRoman Divacky   } else {
11487d523365SDimitry Andric     assert(DestA && DestB && IsCond &&
1149f22ef01cSRoman Divacky            "CFG in a bad state. Cannot correct CFG edges");
1150f22ef01cSRoman Divacky   }
1151f22ef01cSRoman Divacky 
1152f22ef01cSRoman Divacky   // Remove superfluous edges. I.e., those which aren't destinations of this
1153f22ef01cSRoman Divacky   // basic block, duplicate edges, or landing pads.
1154f22ef01cSRoman Divacky   SmallPtrSet<const MachineBasicBlock*, 8> SeenMBBs;
1155f22ef01cSRoman Divacky   MachineBasicBlock::succ_iterator SI = succ_begin();
1156f22ef01cSRoman Divacky   while (SI != succ_end()) {
1157f22ef01cSRoman Divacky     const MachineBasicBlock *MBB = *SI;
115839d628a0SDimitry Andric     if (!SeenMBBs.insert(MBB).second ||
11597d523365SDimitry Andric         (MBB != DestA && MBB != DestB && !MBB->isEHPad())) {
1160f22ef01cSRoman Divacky       // This is a superfluous edge, remove it.
1161f22ef01cSRoman Divacky       SI = removeSuccessor(SI);
1162f22ef01cSRoman Divacky       Changed = true;
1163f22ef01cSRoman Divacky     } else {
1164f22ef01cSRoman Divacky       ++SI;
1165f22ef01cSRoman Divacky     }
1166f22ef01cSRoman Divacky   }
1167f22ef01cSRoman Divacky 
11687d523365SDimitry Andric   if (Changed)
11697d523365SDimitry Andric     normalizeSuccProbs();
1170f22ef01cSRoman Divacky   return Changed;
1171f22ef01cSRoman Divacky }
1172f22ef01cSRoman Divacky 
11737d523365SDimitry Andric /// Find the next valid DebugLoc starting at MBBI, skipping any DBG_VALUE
11747d523365SDimitry Andric /// instructions.  Return UnknownLoc if there is none.
1175f22ef01cSRoman Divacky DebugLoc
1176dff0c46cSDimitry Andric MachineBasicBlock::findDebugLoc(instr_iterator MBBI) {
1177f22ef01cSRoman Divacky   // Skip debug declarations, we don't want a DebugLoc from them.
1178d88c1a5aSDimitry Andric   MBBI = skipDebugInstructionsForward(MBBI, instr_end());
1179d88c1a5aSDimitry Andric   if (MBBI != instr_end())
1180d88c1a5aSDimitry Andric     return MBBI->getDebugLoc();
1181d88c1a5aSDimitry Andric   return {};
1182f22ef01cSRoman Divacky }
1183f22ef01cSRoman Divacky 
11847a7e6055SDimitry Andric /// Find and return the merged DebugLoc of the branch instructions of the block.
11857a7e6055SDimitry Andric /// Return UnknownLoc if there is none.
11867a7e6055SDimitry Andric DebugLoc
11877a7e6055SDimitry Andric MachineBasicBlock::findBranchDebugLoc() {
11887a7e6055SDimitry Andric   DebugLoc DL;
11897a7e6055SDimitry Andric   auto TI = getFirstTerminator();
11907a7e6055SDimitry Andric   while (TI != end() && !TI->isBranch())
11917a7e6055SDimitry Andric     ++TI;
11927a7e6055SDimitry Andric 
11937a7e6055SDimitry Andric   if (TI != end()) {
11947a7e6055SDimitry Andric     DL = TI->getDebugLoc();
11957a7e6055SDimitry Andric     for (++TI ; TI != end() ; ++TI)
11967a7e6055SDimitry Andric       if (TI->isBranch())
11977a7e6055SDimitry Andric         DL = DILocation::getMergedLocation(DL, TI->getDebugLoc());
11987a7e6055SDimitry Andric   }
11997a7e6055SDimitry Andric   return DL;
12007a7e6055SDimitry Andric }
12017a7e6055SDimitry Andric 
12027d523365SDimitry Andric /// Return probability of the edge from this block to MBB.
12037d523365SDimitry Andric BranchProbability
12047d523365SDimitry Andric MachineBasicBlock::getSuccProbability(const_succ_iterator Succ) const {
12057d523365SDimitry Andric   if (Probs.empty())
12067d523365SDimitry Andric     return BranchProbability(1, succ_size());
120717a519f9SDimitry Andric 
12087d523365SDimitry Andric   const auto &Prob = *getProbabilityIterator(Succ);
12097d523365SDimitry Andric   if (Prob.isUnknown()) {
12107d523365SDimitry Andric     // For unknown probabilities, collect the sum of all known ones, and evenly
12117d523365SDimitry Andric     // ditribute the complemental of the sum to each unknown probability.
12127d523365SDimitry Andric     unsigned KnownProbNum = 0;
12137d523365SDimitry Andric     auto Sum = BranchProbability::getZero();
12147d523365SDimitry Andric     for (auto &P : Probs) {
12157d523365SDimitry Andric       if (!P.isUnknown()) {
12167d523365SDimitry Andric         Sum += P;
12177d523365SDimitry Andric         KnownProbNum++;
12187d523365SDimitry Andric       }
12197d523365SDimitry Andric     }
12207d523365SDimitry Andric     return Sum.getCompl() / (Probs.size() - KnownProbNum);
12217d523365SDimitry Andric   } else
12227d523365SDimitry Andric     return Prob;
122317a519f9SDimitry Andric }
122417a519f9SDimitry Andric 
12257d523365SDimitry Andric /// Set successor probability of a given iterator.
12267d523365SDimitry Andric void MachineBasicBlock::setSuccProbability(succ_iterator I,
12277d523365SDimitry Andric                                            BranchProbability Prob) {
12287d523365SDimitry Andric   assert(!Prob.isUnknown());
12297d523365SDimitry Andric   if (Probs.empty())
123091bc56edSDimitry Andric     return;
12317d523365SDimitry Andric   *getProbabilityIterator(I) = Prob;
123291bc56edSDimitry Andric }
123391bc56edSDimitry Andric 
12347d523365SDimitry Andric /// Return probability iterator corresonding to the I successor iterator
12357d523365SDimitry Andric MachineBasicBlock::const_probability_iterator
12367d523365SDimitry Andric MachineBasicBlock::getProbabilityIterator(
12377d523365SDimitry Andric     MachineBasicBlock::const_succ_iterator I) const {
12387d523365SDimitry Andric   assert(Probs.size() == Successors.size() && "Async probability list!");
1239dff0c46cSDimitry Andric   const size_t index = std::distance(Successors.begin(), I);
12407d523365SDimitry Andric   assert(index < Probs.size() && "Not a current successor!");
12417d523365SDimitry Andric   return Probs.begin() + index;
12427d523365SDimitry Andric }
12437d523365SDimitry Andric 
12447d523365SDimitry Andric /// Return probability iterator corresonding to the I successor iterator.
12457d523365SDimitry Andric MachineBasicBlock::probability_iterator
12467d523365SDimitry Andric MachineBasicBlock::getProbabilityIterator(MachineBasicBlock::succ_iterator I) {
12477d523365SDimitry Andric   assert(Probs.size() == Successors.size() && "Async probability list!");
12487d523365SDimitry Andric   const size_t index = std::distance(Successors.begin(), I);
12497d523365SDimitry Andric   assert(index < Probs.size() && "Not a current successor!");
12507d523365SDimitry Andric   return Probs.begin() + index;
1251dff0c46cSDimitry Andric }
1252dff0c46cSDimitry Andric 
12533861d79fSDimitry Andric /// Return whether (physical) register "Reg" has been <def>ined and not <kill>ed
12543861d79fSDimitry Andric /// as of just before "MI".
12553861d79fSDimitry Andric ///
12563861d79fSDimitry Andric /// Search is localised to a neighborhood of
12573861d79fSDimitry Andric /// Neighborhood instructions before (searching for defs or kills) and N
12583861d79fSDimitry Andric /// instructions after (searching just for defs) MI.
12593861d79fSDimitry Andric MachineBasicBlock::LivenessQueryResult
12603861d79fSDimitry Andric MachineBasicBlock::computeRegisterLiveness(const TargetRegisterInfo *TRI,
1261ff0cc061SDimitry Andric                                            unsigned Reg, const_iterator Before,
1262ff0cc061SDimitry Andric                                            unsigned Neighborhood) const {
12633861d79fSDimitry Andric   unsigned N = Neighborhood;
12643861d79fSDimitry Andric 
1265ff0cc061SDimitry Andric   // Start by searching backwards from Before, looking for kills, reads or defs.
1266ff0cc061SDimitry Andric   const_iterator I(Before);
12673861d79fSDimitry Andric   // If this is the first insn in the block, don't search backwards.
1268ff0cc061SDimitry Andric   if (I != begin()) {
12693861d79fSDimitry Andric     do {
12703861d79fSDimitry Andric       --I;
12713861d79fSDimitry Andric 
12727d523365SDimitry Andric       MachineOperandIteratorBase::PhysRegInfo Info =
12733ca95b02SDimitry Andric           ConstMIOperands(*I).analyzePhysReg(Reg, TRI);
12743861d79fSDimitry Andric 
12757d523365SDimitry Andric       // Defs happen after uses so they take precedence if both are present.
1276139f7f9bSDimitry Andric 
12777d523365SDimitry Andric       // Register is dead after a dead def of the full register.
12787d523365SDimitry Andric       if (Info.DeadDef)
12793861d79fSDimitry Andric         return LQR_Dead;
12807d523365SDimitry Andric       // Register is (at least partially) live after a def.
12813ca95b02SDimitry Andric       if (Info.Defined) {
12823ca95b02SDimitry Andric         if (!Info.PartialDeadDef)
12837d523365SDimitry Andric           return LQR_Live;
12843ca95b02SDimitry Andric         // As soon as we saw a partial definition (dead or not),
12853ca95b02SDimitry Andric         // we cannot tell if the value is partial live without
12863ca95b02SDimitry Andric         // tracking the lanemasks. We are not going to do this,
12873ca95b02SDimitry Andric         // so fall back on the remaining of the analysis.
12883ca95b02SDimitry Andric         break;
12893ca95b02SDimitry Andric       }
12907d523365SDimitry Andric       // Register is dead after a full kill or clobber and no def.
12917d523365SDimitry Andric       if (Info.Killed || Info.Clobbered)
12927d523365SDimitry Andric         return LQR_Dead;
12937d523365SDimitry Andric       // Register must be live if we read it.
12947d523365SDimitry Andric       if (Info.Read)
12957d523365SDimitry Andric         return LQR_Live;
1296ff0cc061SDimitry Andric     } while (I != begin() && --N > 0);
12973861d79fSDimitry Andric   }
12983861d79fSDimitry Andric 
12993861d79fSDimitry Andric   // Did we get to the start of the block?
1300ff0cc061SDimitry Andric   if (I == begin()) {
13013861d79fSDimitry Andric     // If so, the register's state is definitely defined by the live-in state.
13027d523365SDimitry Andric     for (MCRegAliasIterator RAI(Reg, TRI, /*IncludeSelf=*/true); RAI.isValid();
13037d523365SDimitry Andric          ++RAI)
1304ff0cc061SDimitry Andric       if (isLiveIn(*RAI))
13057d523365SDimitry Andric         return LQR_Live;
13063861d79fSDimitry Andric 
13073861d79fSDimitry Andric     return LQR_Dead;
13083861d79fSDimitry Andric   }
13093861d79fSDimitry Andric 
13103861d79fSDimitry Andric   N = Neighborhood;
13113861d79fSDimitry Andric 
1312ff0cc061SDimitry Andric   // Try searching forwards from Before, looking for reads or defs.
1313ff0cc061SDimitry Andric   I = const_iterator(Before);
13143861d79fSDimitry Andric   // If this is the last insn in the block, don't search forwards.
1315ff0cc061SDimitry Andric   if (I != end()) {
1316ff0cc061SDimitry Andric     for (++I; I != end() && N > 0; ++I, --N) {
13177d523365SDimitry Andric       MachineOperandIteratorBase::PhysRegInfo Info =
13183ca95b02SDimitry Andric           ConstMIOperands(*I).analyzePhysReg(Reg, TRI);
13193861d79fSDimitry Andric 
13207d523365SDimitry Andric       // Register is live when we read it here.
13217d523365SDimitry Andric       if (Info.Read)
13227d523365SDimitry Andric         return LQR_Live;
13237d523365SDimitry Andric       // Register is dead if we can fully overwrite or clobber it here.
13247d523365SDimitry Andric       if (Info.FullyDefined || Info.Clobbered)
13253861d79fSDimitry Andric         return LQR_Dead;
13263861d79fSDimitry Andric     }
13273861d79fSDimitry Andric   }
13283861d79fSDimitry Andric 
13293861d79fSDimitry Andric   // At this point we have no idea of the liveness of the register.
13303861d79fSDimitry Andric   return LQR_Unknown;
13313861d79fSDimitry Andric }
13327d523365SDimitry Andric 
13337d523365SDimitry Andric const uint32_t *
13347d523365SDimitry Andric MachineBasicBlock::getBeginClobberMask(const TargetRegisterInfo *TRI) const {
13357d523365SDimitry Andric   // EH funclet entry does not preserve any registers.
13367d523365SDimitry Andric   return isEHFuncletEntry() ? TRI->getNoPreservedMask() : nullptr;
13377d523365SDimitry Andric }
13387d523365SDimitry Andric 
13397d523365SDimitry Andric const uint32_t *
13407d523365SDimitry Andric MachineBasicBlock::getEndClobberMask(const TargetRegisterInfo *TRI) const {
13417d523365SDimitry Andric   // If we see a return block with successors, this must be a funclet return,
13427d523365SDimitry Andric   // which does not preserve any registers. If there are no successors, we don't
13437d523365SDimitry Andric   // care what kind of return it is, putting a mask after it is a no-op.
13447d523365SDimitry Andric   return isReturnBlock() && !succ_empty() ? TRI->getNoPreservedMask() : nullptr;
13457d523365SDimitry Andric }
1346d88c1a5aSDimitry Andric 
1347d88c1a5aSDimitry Andric void MachineBasicBlock::clearLiveIns() {
1348d88c1a5aSDimitry Andric   LiveIns.clear();
1349d88c1a5aSDimitry Andric }
135095ec533aSDimitry Andric 
135195ec533aSDimitry Andric MachineBasicBlock::livein_iterator MachineBasicBlock::livein_begin() const {
135295ec533aSDimitry Andric   assert(getParent()->getProperties().hasProperty(
135395ec533aSDimitry Andric       MachineFunctionProperties::Property::TracksLiveness) &&
135495ec533aSDimitry Andric       "Liveness information is accurate");
135595ec533aSDimitry Andric   return LiveIns.begin();
135695ec533aSDimitry Andric }
1357