MachineBasicBlock.cpp 56 KB

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