TailDuplicator.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. //===- TailDuplicator.cpp - Duplicate blocks into predecessors' tails -----===//
  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. // This utility class duplicates basic blocks ending in unconditional branches
  10. // into the tails of their predecessors.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/TailDuplicator.h"
  14. #include "llvm/ADT/DenseMap.h"
  15. #include "llvm/ADT/DenseSet.h"
  16. #include "llvm/ADT/STLExtras.h"
  17. #include "llvm/ADT/SetVector.h"
  18. #include "llvm/ADT/SmallPtrSet.h"
  19. #include "llvm/ADT/SmallVector.h"
  20. #include "llvm/ADT/Statistic.h"
  21. #include "llvm/Analysis/ProfileSummaryInfo.h"
  22. #include "llvm/CodeGen/MachineBasicBlock.h"
  23. #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
  24. #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
  25. #include "llvm/CodeGen/MachineFunction.h"
  26. #include "llvm/CodeGen/MachineInstr.h"
  27. #include "llvm/CodeGen/MachineInstrBuilder.h"
  28. #include "llvm/CodeGen/MachineOperand.h"
  29. #include "llvm/CodeGen/MachineRegisterInfo.h"
  30. #include "llvm/CodeGen/MachineSizeOpts.h"
  31. #include "llvm/CodeGen/MachineSSAUpdater.h"
  32. #include "llvm/CodeGen/TargetInstrInfo.h"
  33. #include "llvm/CodeGen/TargetRegisterInfo.h"
  34. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  35. #include "llvm/IR/DebugLoc.h"
  36. #include "llvm/IR/Function.h"
  37. #include "llvm/Support/CommandLine.h"
  38. #include "llvm/Support/Debug.h"
  39. #include "llvm/Support/ErrorHandling.h"
  40. #include "llvm/Support/raw_ostream.h"
  41. #include "llvm/Target/TargetMachine.h"
  42. #include <algorithm>
  43. #include <cassert>
  44. #include <iterator>
  45. #include <utility>
  46. using namespace llvm;
  47. #define DEBUG_TYPE "tailduplication"
  48. STATISTIC(NumTails, "Number of tails duplicated");
  49. STATISTIC(NumTailDups, "Number of tail duplicated blocks");
  50. STATISTIC(NumTailDupAdded,
  51. "Number of instructions added due to tail duplication");
  52. STATISTIC(NumTailDupRemoved,
  53. "Number of instructions removed due to tail duplication");
  54. STATISTIC(NumDeadBlocks, "Number of dead blocks removed");
  55. STATISTIC(NumAddedPHIs, "Number of phis added");
  56. // Heuristic for tail duplication.
  57. static cl::opt<unsigned> TailDuplicateSize(
  58. "tail-dup-size",
  59. cl::desc("Maximum instructions to consider tail duplicating"), cl::init(2),
  60. cl::Hidden);
  61. static cl::opt<unsigned> TailDupIndirectBranchSize(
  62. "tail-dup-indirect-size",
  63. cl::desc("Maximum instructions to consider tail duplicating blocks that "
  64. "end with indirect branches."), cl::init(20),
  65. cl::Hidden);
  66. static cl::opt<bool>
  67. TailDupVerify("tail-dup-verify",
  68. cl::desc("Verify sanity of PHI instructions during taildup"),
  69. cl::init(false), cl::Hidden);
  70. static cl::opt<unsigned> TailDupLimit("tail-dup-limit", cl::init(~0U),
  71. cl::Hidden);
  72. void TailDuplicator::initMF(MachineFunction &MFin, bool PreRegAlloc,
  73. const MachineBranchProbabilityInfo *MBPIin,
  74. MBFIWrapper *MBFIin,
  75. ProfileSummaryInfo *PSIin,
  76. bool LayoutModeIn, unsigned TailDupSizeIn) {
  77. MF = &MFin;
  78. TII = MF->getSubtarget().getInstrInfo();
  79. TRI = MF->getSubtarget().getRegisterInfo();
  80. MRI = &MF->getRegInfo();
  81. MMI = &MF->getMMI();
  82. MBPI = MBPIin;
  83. MBFI = MBFIin;
  84. PSI = PSIin;
  85. TailDupSize = TailDupSizeIn;
  86. assert(MBPI != nullptr && "Machine Branch Probability Info required");
  87. LayoutMode = LayoutModeIn;
  88. this->PreRegAlloc = PreRegAlloc;
  89. }
  90. static void VerifyPHIs(MachineFunction &MF, bool CheckExtra) {
  91. for (MachineBasicBlock &MBB : llvm::drop_begin(MF)) {
  92. SmallSetVector<MachineBasicBlock *, 8> Preds(MBB.pred_begin(),
  93. MBB.pred_end());
  94. MachineBasicBlock::iterator MI = MBB.begin();
  95. while (MI != MBB.end()) {
  96. if (!MI->isPHI())
  97. break;
  98. for (MachineBasicBlock *PredBB : Preds) {
  99. bool Found = false;
  100. for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) {
  101. MachineBasicBlock *PHIBB = MI->getOperand(i + 1).getMBB();
  102. if (PHIBB == PredBB) {
  103. Found = true;
  104. break;
  105. }
  106. }
  107. if (!Found) {
  108. dbgs() << "Malformed PHI in " << printMBBReference(MBB) << ": "
  109. << *MI;
  110. dbgs() << " missing input from predecessor "
  111. << printMBBReference(*PredBB) << '\n';
  112. llvm_unreachable(nullptr);
  113. }
  114. }
  115. for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) {
  116. MachineBasicBlock *PHIBB = MI->getOperand(i + 1).getMBB();
  117. if (CheckExtra && !Preds.count(PHIBB)) {
  118. dbgs() << "Warning: malformed PHI in " << printMBBReference(MBB)
  119. << ": " << *MI;
  120. dbgs() << " extra input from predecessor "
  121. << printMBBReference(*PHIBB) << '\n';
  122. llvm_unreachable(nullptr);
  123. }
  124. if (PHIBB->getNumber() < 0) {
  125. dbgs() << "Malformed PHI in " << printMBBReference(MBB) << ": "
  126. << *MI;
  127. dbgs() << " non-existing " << printMBBReference(*PHIBB) << '\n';
  128. llvm_unreachable(nullptr);
  129. }
  130. }
  131. ++MI;
  132. }
  133. }
  134. }
  135. /// Tail duplicate the block and cleanup.
  136. /// \p IsSimple - return value of isSimpleBB
  137. /// \p MBB - block to be duplicated
  138. /// \p ForcedLayoutPred - If non-null, treat this block as the layout
  139. /// predecessor, instead of using the ordering in MF
  140. /// \p DuplicatedPreds - if non-null, \p DuplicatedPreds will contain a list of
  141. /// all Preds that received a copy of \p MBB.
  142. /// \p RemovalCallback - if non-null, called just before MBB is deleted.
  143. bool TailDuplicator::tailDuplicateAndUpdate(
  144. bool IsSimple, MachineBasicBlock *MBB,
  145. MachineBasicBlock *ForcedLayoutPred,
  146. SmallVectorImpl<MachineBasicBlock*> *DuplicatedPreds,
  147. function_ref<void(MachineBasicBlock *)> *RemovalCallback,
  148. SmallVectorImpl<MachineBasicBlock *> *CandidatePtr) {
  149. // Save the successors list.
  150. SmallSetVector<MachineBasicBlock *, 8> Succs(MBB->succ_begin(),
  151. MBB->succ_end());
  152. SmallVector<MachineBasicBlock *, 8> TDBBs;
  153. SmallVector<MachineInstr *, 16> Copies;
  154. if (!tailDuplicate(IsSimple, MBB, ForcedLayoutPred,
  155. TDBBs, Copies, CandidatePtr))
  156. return false;
  157. ++NumTails;
  158. SmallVector<MachineInstr *, 8> NewPHIs;
  159. MachineSSAUpdater SSAUpdate(*MF, &NewPHIs);
  160. // TailBB's immediate successors are now successors of those predecessors
  161. // which duplicated TailBB. Add the predecessors as sources to the PHI
  162. // instructions.
  163. bool isDead = MBB->pred_empty() && !MBB->hasAddressTaken();
  164. if (PreRegAlloc)
  165. updateSuccessorsPHIs(MBB, isDead, TDBBs, Succs);
  166. // If it is dead, remove it.
  167. if (isDead) {
  168. NumTailDupRemoved += MBB->size();
  169. removeDeadBlock(MBB, RemovalCallback);
  170. ++NumDeadBlocks;
  171. }
  172. // Update SSA form.
  173. if (!SSAUpdateVRs.empty()) {
  174. for (unsigned i = 0, e = SSAUpdateVRs.size(); i != e; ++i) {
  175. unsigned VReg = SSAUpdateVRs[i];
  176. SSAUpdate.Initialize(VReg);
  177. // If the original definition is still around, add it as an available
  178. // value.
  179. MachineInstr *DefMI = MRI->getVRegDef(VReg);
  180. MachineBasicBlock *DefBB = nullptr;
  181. if (DefMI) {
  182. DefBB = DefMI->getParent();
  183. SSAUpdate.AddAvailableValue(DefBB, VReg);
  184. }
  185. // Add the new vregs as available values.
  186. DenseMap<Register, AvailableValsTy>::iterator LI =
  187. SSAUpdateVals.find(VReg);
  188. for (std::pair<MachineBasicBlock *, Register> &J : LI->second) {
  189. MachineBasicBlock *SrcBB = J.first;
  190. Register SrcReg = J.second;
  191. SSAUpdate.AddAvailableValue(SrcBB, SrcReg);
  192. }
  193. SmallVector<MachineOperand *> DebugUses;
  194. // Rewrite uses that are outside of the original def's block.
  195. for (MachineOperand &UseMO :
  196. llvm::make_early_inc_range(MRI->use_operands(VReg))) {
  197. MachineInstr *UseMI = UseMO.getParent();
  198. // Rewrite debug uses last so that they can take advantage of any
  199. // register mappings introduced by other users in its BB, since we
  200. // cannot create new register definitions specifically for the debug
  201. // instruction (as debug instructions should not affect CodeGen).
  202. if (UseMI->isDebugValue()) {
  203. DebugUses.push_back(&UseMO);
  204. continue;
  205. }
  206. if (UseMI->getParent() == DefBB && !UseMI->isPHI())
  207. continue;
  208. SSAUpdate.RewriteUse(UseMO);
  209. }
  210. for (auto *UseMO : DebugUses) {
  211. MachineInstr *UseMI = UseMO->getParent();
  212. UseMO->setReg(
  213. SSAUpdate.GetValueInMiddleOfBlock(UseMI->getParent(), true));
  214. }
  215. }
  216. SSAUpdateVRs.clear();
  217. SSAUpdateVals.clear();
  218. }
  219. // Eliminate some of the copies inserted by tail duplication to maintain
  220. // SSA form.
  221. for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
  222. MachineInstr *Copy = Copies[i];
  223. if (!Copy->isCopy())
  224. continue;
  225. Register Dst = Copy->getOperand(0).getReg();
  226. Register Src = Copy->getOperand(1).getReg();
  227. if (MRI->hasOneNonDBGUse(Src) &&
  228. MRI->constrainRegClass(Src, MRI->getRegClass(Dst))) {
  229. // Copy is the only use. Do trivial copy propagation here.
  230. MRI->replaceRegWith(Dst, Src);
  231. Copy->eraseFromParent();
  232. }
  233. }
  234. if (NewPHIs.size())
  235. NumAddedPHIs += NewPHIs.size();
  236. if (DuplicatedPreds)
  237. *DuplicatedPreds = std::move(TDBBs);
  238. return true;
  239. }
  240. /// Look for small blocks that are unconditionally branched to and do not fall
  241. /// through. Tail-duplicate their instructions into their predecessors to
  242. /// eliminate (dynamic) branches.
  243. bool TailDuplicator::tailDuplicateBlocks() {
  244. bool MadeChange = false;
  245. if (PreRegAlloc && TailDupVerify) {
  246. LLVM_DEBUG(dbgs() << "\n*** Before tail-duplicating\n");
  247. VerifyPHIs(*MF, true);
  248. }
  249. for (MachineBasicBlock &MBB :
  250. llvm::make_early_inc_range(llvm::drop_begin(*MF))) {
  251. if (NumTails == TailDupLimit)
  252. break;
  253. bool IsSimple = isSimpleBB(&MBB);
  254. if (!shouldTailDuplicate(IsSimple, MBB))
  255. continue;
  256. MadeChange |= tailDuplicateAndUpdate(IsSimple, &MBB, nullptr);
  257. }
  258. if (PreRegAlloc && TailDupVerify)
  259. VerifyPHIs(*MF, false);
  260. return MadeChange;
  261. }
  262. static bool isDefLiveOut(Register Reg, MachineBasicBlock *BB,
  263. const MachineRegisterInfo *MRI) {
  264. for (MachineInstr &UseMI : MRI->use_instructions(Reg)) {
  265. if (UseMI.isDebugValue())
  266. continue;
  267. if (UseMI.getParent() != BB)
  268. return true;
  269. }
  270. return false;
  271. }
  272. static unsigned getPHISrcRegOpIdx(MachineInstr *MI, MachineBasicBlock *SrcBB) {
  273. for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2)
  274. if (MI->getOperand(i + 1).getMBB() == SrcBB)
  275. return i;
  276. return 0;
  277. }
  278. // Remember which registers are used by phis in this block. This is
  279. // used to determine which registers are liveout while modifying the
  280. // block (which is why we need to copy the information).
  281. static void getRegsUsedByPHIs(const MachineBasicBlock &BB,
  282. DenseSet<Register> *UsedByPhi) {
  283. for (const auto &MI : BB) {
  284. if (!MI.isPHI())
  285. break;
  286. for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) {
  287. Register SrcReg = MI.getOperand(i).getReg();
  288. UsedByPhi->insert(SrcReg);
  289. }
  290. }
  291. }
  292. /// Add a definition and source virtual registers pair for SSA update.
  293. void TailDuplicator::addSSAUpdateEntry(Register OrigReg, Register NewReg,
  294. MachineBasicBlock *BB) {
  295. DenseMap<Register, AvailableValsTy>::iterator LI =
  296. SSAUpdateVals.find(OrigReg);
  297. if (LI != SSAUpdateVals.end())
  298. LI->second.push_back(std::make_pair(BB, NewReg));
  299. else {
  300. AvailableValsTy Vals;
  301. Vals.push_back(std::make_pair(BB, NewReg));
  302. SSAUpdateVals.insert(std::make_pair(OrigReg, Vals));
  303. SSAUpdateVRs.push_back(OrigReg);
  304. }
  305. }
  306. /// Process PHI node in TailBB by turning it into a copy in PredBB. Remember the
  307. /// source register that's contributed by PredBB and update SSA update map.
  308. void TailDuplicator::processPHI(
  309. MachineInstr *MI, MachineBasicBlock *TailBB, MachineBasicBlock *PredBB,
  310. DenseMap<Register, RegSubRegPair> &LocalVRMap,
  311. SmallVectorImpl<std::pair<Register, RegSubRegPair>> &Copies,
  312. const DenseSet<Register> &RegsUsedByPhi, bool Remove) {
  313. Register DefReg = MI->getOperand(0).getReg();
  314. unsigned SrcOpIdx = getPHISrcRegOpIdx(MI, PredBB);
  315. assert(SrcOpIdx && "Unable to find matching PHI source?");
  316. Register SrcReg = MI->getOperand(SrcOpIdx).getReg();
  317. unsigned SrcSubReg = MI->getOperand(SrcOpIdx).getSubReg();
  318. const TargetRegisterClass *RC = MRI->getRegClass(DefReg);
  319. LocalVRMap.insert(std::make_pair(DefReg, RegSubRegPair(SrcReg, SrcSubReg)));
  320. // Insert a copy from source to the end of the block. The def register is the
  321. // available value liveout of the block.
  322. Register NewDef = MRI->createVirtualRegister(RC);
  323. Copies.push_back(std::make_pair(NewDef, RegSubRegPair(SrcReg, SrcSubReg)));
  324. if (isDefLiveOut(DefReg, TailBB, MRI) || RegsUsedByPhi.count(DefReg))
  325. addSSAUpdateEntry(DefReg, NewDef, PredBB);
  326. if (!Remove)
  327. return;
  328. // Remove PredBB from the PHI node.
  329. MI->RemoveOperand(SrcOpIdx + 1);
  330. MI->RemoveOperand(SrcOpIdx);
  331. if (MI->getNumOperands() == 1)
  332. MI->eraseFromParent();
  333. }
  334. /// Duplicate a TailBB instruction to PredBB and update
  335. /// the source operands due to earlier PHI translation.
  336. void TailDuplicator::duplicateInstruction(
  337. MachineInstr *MI, MachineBasicBlock *TailBB, MachineBasicBlock *PredBB,
  338. DenseMap<Register, RegSubRegPair> &LocalVRMap,
  339. const DenseSet<Register> &UsedByPhi) {
  340. // Allow duplication of CFI instructions.
  341. if (MI->isCFIInstruction()) {
  342. BuildMI(*PredBB, PredBB->end(), PredBB->findDebugLoc(PredBB->begin()),
  343. TII->get(TargetOpcode::CFI_INSTRUCTION)).addCFIIndex(
  344. MI->getOperand(0).getCFIIndex());
  345. return;
  346. }
  347. MachineInstr &NewMI = TII->duplicate(*PredBB, PredBB->end(), *MI);
  348. if (PreRegAlloc) {
  349. for (unsigned i = 0, e = NewMI.getNumOperands(); i != e; ++i) {
  350. MachineOperand &MO = NewMI.getOperand(i);
  351. if (!MO.isReg())
  352. continue;
  353. Register Reg = MO.getReg();
  354. if (!Register::isVirtualRegister(Reg))
  355. continue;
  356. if (MO.isDef()) {
  357. const TargetRegisterClass *RC = MRI->getRegClass(Reg);
  358. Register NewReg = MRI->createVirtualRegister(RC);
  359. MO.setReg(NewReg);
  360. LocalVRMap.insert(std::make_pair(Reg, RegSubRegPair(NewReg, 0)));
  361. if (isDefLiveOut(Reg, TailBB, MRI) || UsedByPhi.count(Reg))
  362. addSSAUpdateEntry(Reg, NewReg, PredBB);
  363. } else {
  364. auto VI = LocalVRMap.find(Reg);
  365. if (VI != LocalVRMap.end()) {
  366. // Need to make sure that the register class of the mapped register
  367. // will satisfy the constraints of the class of the register being
  368. // replaced.
  369. auto *OrigRC = MRI->getRegClass(Reg);
  370. auto *MappedRC = MRI->getRegClass(VI->second.Reg);
  371. const TargetRegisterClass *ConstrRC;
  372. if (VI->second.SubReg != 0) {
  373. ConstrRC = TRI->getMatchingSuperRegClass(MappedRC, OrigRC,
  374. VI->second.SubReg);
  375. if (ConstrRC) {
  376. // The actual constraining (as in "find appropriate new class")
  377. // is done by getMatchingSuperRegClass, so now we only need to
  378. // change the class of the mapped register.
  379. MRI->setRegClass(VI->second.Reg, ConstrRC);
  380. }
  381. } else {
  382. // For mapped registers that do not have sub-registers, simply
  383. // restrict their class to match the original one.
  384. ConstrRC = MRI->constrainRegClass(VI->second.Reg, OrigRC);
  385. }
  386. if (ConstrRC) {
  387. // If the class constraining succeeded, we can simply replace
  388. // the old register with the mapped one.
  389. MO.setReg(VI->second.Reg);
  390. // We have Reg -> VI.Reg:VI.SubReg, so if Reg is used with a
  391. // sub-register, we need to compose the sub-register indices.
  392. MO.setSubReg(TRI->composeSubRegIndices(MO.getSubReg(),
  393. VI->second.SubReg));
  394. } else {
  395. // The direct replacement is not possible, due to failing register
  396. // class constraints. An explicit COPY is necessary. Create one
  397. // that can be reused
  398. auto *NewRC = MI->getRegClassConstraint(i, TII, TRI);
  399. if (NewRC == nullptr)
  400. NewRC = OrigRC;
  401. Register NewReg = MRI->createVirtualRegister(NewRC);
  402. BuildMI(*PredBB, NewMI, NewMI.getDebugLoc(),
  403. TII->get(TargetOpcode::COPY), NewReg)
  404. .addReg(VI->second.Reg, 0, VI->second.SubReg);
  405. LocalVRMap.erase(VI);
  406. LocalVRMap.insert(std::make_pair(Reg, RegSubRegPair(NewReg, 0)));
  407. MO.setReg(NewReg);
  408. // The composed VI.Reg:VI.SubReg is replaced with NewReg, which
  409. // is equivalent to the whole register Reg. Hence, Reg:subreg
  410. // is same as NewReg:subreg, so keep the sub-register index
  411. // unchanged.
  412. }
  413. // Clear any kill flags from this operand. The new register could
  414. // have uses after this one, so kills are not valid here.
  415. MO.setIsKill(false);
  416. }
  417. }
  418. }
  419. }
  420. }
  421. /// After FromBB is tail duplicated into its predecessor blocks, the successors
  422. /// have gained new predecessors. Update the PHI instructions in them
  423. /// accordingly.
  424. void TailDuplicator::updateSuccessorsPHIs(
  425. MachineBasicBlock *FromBB, bool isDead,
  426. SmallVectorImpl<MachineBasicBlock *> &TDBBs,
  427. SmallSetVector<MachineBasicBlock *, 8> &Succs) {
  428. for (MachineBasicBlock *SuccBB : Succs) {
  429. for (MachineInstr &MI : *SuccBB) {
  430. if (!MI.isPHI())
  431. break;
  432. MachineInstrBuilder MIB(*FromBB->getParent(), MI);
  433. unsigned Idx = 0;
  434. for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) {
  435. MachineOperand &MO = MI.getOperand(i + 1);
  436. if (MO.getMBB() == FromBB) {
  437. Idx = i;
  438. break;
  439. }
  440. }
  441. assert(Idx != 0);
  442. MachineOperand &MO0 = MI.getOperand(Idx);
  443. Register Reg = MO0.getReg();
  444. if (isDead) {
  445. // Folded into the previous BB.
  446. // There could be duplicate phi source entries. FIXME: Should sdisel
  447. // or earlier pass fixed this?
  448. for (unsigned i = MI.getNumOperands() - 2; i != Idx; i -= 2) {
  449. MachineOperand &MO = MI.getOperand(i + 1);
  450. if (MO.getMBB() == FromBB) {
  451. MI.RemoveOperand(i + 1);
  452. MI.RemoveOperand(i);
  453. }
  454. }
  455. } else
  456. Idx = 0;
  457. // If Idx is set, the operands at Idx and Idx+1 must be removed.
  458. // We reuse the location to avoid expensive RemoveOperand calls.
  459. DenseMap<Register, AvailableValsTy>::iterator LI =
  460. SSAUpdateVals.find(Reg);
  461. if (LI != SSAUpdateVals.end()) {
  462. // This register is defined in the tail block.
  463. for (const std::pair<MachineBasicBlock *, Register> &J : LI->second) {
  464. MachineBasicBlock *SrcBB = J.first;
  465. // If we didn't duplicate a bb into a particular predecessor, we
  466. // might still have added an entry to SSAUpdateVals to correcly
  467. // recompute SSA. If that case, avoid adding a dummy extra argument
  468. // this PHI.
  469. if (!SrcBB->isSuccessor(SuccBB))
  470. continue;
  471. Register SrcReg = J.second;
  472. if (Idx != 0) {
  473. MI.getOperand(Idx).setReg(SrcReg);
  474. MI.getOperand(Idx + 1).setMBB(SrcBB);
  475. Idx = 0;
  476. } else {
  477. MIB.addReg(SrcReg).addMBB(SrcBB);
  478. }
  479. }
  480. } else {
  481. // Live in tail block, must also be live in predecessors.
  482. for (MachineBasicBlock *SrcBB : TDBBs) {
  483. if (Idx != 0) {
  484. MI.getOperand(Idx).setReg(Reg);
  485. MI.getOperand(Idx + 1).setMBB(SrcBB);
  486. Idx = 0;
  487. } else {
  488. MIB.addReg(Reg).addMBB(SrcBB);
  489. }
  490. }
  491. }
  492. if (Idx != 0) {
  493. MI.RemoveOperand(Idx + 1);
  494. MI.RemoveOperand(Idx);
  495. }
  496. }
  497. }
  498. }
  499. /// Determine if it is profitable to duplicate this block.
  500. bool TailDuplicator::shouldTailDuplicate(bool IsSimple,
  501. MachineBasicBlock &TailBB) {
  502. // When doing tail-duplication during layout, the block ordering is in flux,
  503. // so canFallThrough returns a result based on incorrect information and
  504. // should just be ignored.
  505. if (!LayoutMode && TailBB.canFallThrough())
  506. return false;
  507. // Don't try to tail-duplicate single-block loops.
  508. if (TailBB.isSuccessor(&TailBB))
  509. return false;
  510. // Set the limit on the cost to duplicate. When optimizing for size,
  511. // duplicate only one, because one branch instruction can be eliminated to
  512. // compensate for the duplication.
  513. unsigned MaxDuplicateCount;
  514. bool OptForSize = MF->getFunction().hasOptSize() ||
  515. llvm::shouldOptimizeForSize(&TailBB, PSI, MBFI);
  516. if (TailDupSize == 0)
  517. MaxDuplicateCount = TailDuplicateSize;
  518. else
  519. MaxDuplicateCount = TailDupSize;
  520. if (OptForSize)
  521. MaxDuplicateCount = 1;
  522. // If the block to be duplicated ends in an unanalyzable fallthrough, don't
  523. // duplicate it.
  524. // A similar check is necessary in MachineBlockPlacement to make sure pairs of
  525. // blocks with unanalyzable fallthrough get layed out contiguously.
  526. MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
  527. SmallVector<MachineOperand, 4> PredCond;
  528. if (TII->analyzeBranch(TailBB, PredTBB, PredFBB, PredCond) &&
  529. TailBB.canFallThrough())
  530. return false;
  531. // If the target has hardware branch prediction that can handle indirect
  532. // branches, duplicating them can often make them predictable when there
  533. // are common paths through the code. The limit needs to be high enough
  534. // to allow undoing the effects of tail merging and other optimizations
  535. // that rearrange the predecessors of the indirect branch.
  536. bool HasIndirectbr = false;
  537. if (!TailBB.empty())
  538. HasIndirectbr = TailBB.back().isIndirectBranch();
  539. if (HasIndirectbr && PreRegAlloc)
  540. MaxDuplicateCount = TailDupIndirectBranchSize;
  541. // Check the instructions in the block to determine whether tail-duplication
  542. // is invalid or unlikely to be profitable.
  543. unsigned InstrCount = 0;
  544. for (MachineInstr &MI : TailBB) {
  545. // Non-duplicable things shouldn't be tail-duplicated.
  546. // CFI instructions are marked as non-duplicable, because Darwin compact
  547. // unwind info emission can't handle multiple prologue setups. In case of
  548. // DWARF, allow them be duplicated, so that their existence doesn't prevent
  549. // tail duplication of some basic blocks, that would be duplicated otherwise.
  550. if (MI.isNotDuplicable() &&
  551. (TailBB.getParent()->getTarget().getTargetTriple().isOSDarwin() ||
  552. !MI.isCFIInstruction()))
  553. return false;
  554. // Convergent instructions can be duplicated only if doing so doesn't add
  555. // new control dependencies, which is what we're going to do here.
  556. if (MI.isConvergent())
  557. return false;
  558. // Do not duplicate 'return' instructions if this is a pre-regalloc run.
  559. // A return may expand into a lot more instructions (e.g. reload of callee
  560. // saved registers) after PEI.
  561. if (PreRegAlloc && MI.isReturn())
  562. return false;
  563. // Avoid duplicating calls before register allocation. Calls presents a
  564. // barrier to register allocation so duplicating them may end up increasing
  565. // spills.
  566. if (PreRegAlloc && MI.isCall())
  567. return false;
  568. // TailDuplicator::appendCopies will erroneously place COPYs after
  569. // INLINEASM_BR instructions after 4b0aa5724fea, which demonstrates the same
  570. // bug that was fixed in f7a53d82c090.
  571. // FIXME: Use findPHICopyInsertPoint() to find the correct insertion point
  572. // for the COPY when replacing PHIs.
  573. if (MI.getOpcode() == TargetOpcode::INLINEASM_BR)
  574. return false;
  575. if (MI.isBundle())
  576. InstrCount += MI.getBundleSize();
  577. else if (!MI.isPHI() && !MI.isMetaInstruction())
  578. InstrCount += 1;
  579. if (InstrCount > MaxDuplicateCount)
  580. return false;
  581. }
  582. // Check if any of the successors of TailBB has a PHI node in which the
  583. // value corresponding to TailBB uses a subregister.
  584. // If a phi node uses a register paired with a subregister, the actual
  585. // "value type" of the phi may differ from the type of the register without
  586. // any subregisters. Due to a bug, tail duplication may add a new operand
  587. // without a necessary subregister, producing an invalid code. This is
  588. // demonstrated by test/CodeGen/Hexagon/tail-dup-subreg-abort.ll.
  589. // Disable tail duplication for this case for now, until the problem is
  590. // fixed.
  591. for (auto SB : TailBB.successors()) {
  592. for (auto &I : *SB) {
  593. if (!I.isPHI())
  594. break;
  595. unsigned Idx = getPHISrcRegOpIdx(&I, &TailBB);
  596. assert(Idx != 0);
  597. MachineOperand &PU = I.getOperand(Idx);
  598. if (PU.getSubReg() != 0)
  599. return false;
  600. }
  601. }
  602. if (HasIndirectbr && PreRegAlloc)
  603. return true;
  604. if (IsSimple)
  605. return true;
  606. if (!PreRegAlloc)
  607. return true;
  608. return canCompletelyDuplicateBB(TailBB);
  609. }
  610. /// True if this BB has only one unconditional jump.
  611. bool TailDuplicator::isSimpleBB(MachineBasicBlock *TailBB) {
  612. if (TailBB->succ_size() != 1)
  613. return false;
  614. if (TailBB->pred_empty())
  615. return false;
  616. MachineBasicBlock::iterator I = TailBB->getFirstNonDebugInstr(true);
  617. if (I == TailBB->end())
  618. return true;
  619. return I->isUnconditionalBranch();
  620. }
  621. static bool bothUsedInPHI(const MachineBasicBlock &A,
  622. const SmallPtrSet<MachineBasicBlock *, 8> &SuccsB) {
  623. for (MachineBasicBlock *BB : A.successors())
  624. if (SuccsB.count(BB) && !BB->empty() && BB->begin()->isPHI())
  625. return true;
  626. return false;
  627. }
  628. bool TailDuplicator::canCompletelyDuplicateBB(MachineBasicBlock &BB) {
  629. for (MachineBasicBlock *PredBB : BB.predecessors()) {
  630. if (PredBB->succ_size() > 1)
  631. return false;
  632. MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
  633. SmallVector<MachineOperand, 4> PredCond;
  634. if (TII->analyzeBranch(*PredBB, PredTBB, PredFBB, PredCond))
  635. return false;
  636. if (!PredCond.empty())
  637. return false;
  638. }
  639. return true;
  640. }
  641. bool TailDuplicator::duplicateSimpleBB(
  642. MachineBasicBlock *TailBB, SmallVectorImpl<MachineBasicBlock *> &TDBBs,
  643. const DenseSet<Register> &UsedByPhi,
  644. SmallVectorImpl<MachineInstr *> &Copies) {
  645. SmallPtrSet<MachineBasicBlock *, 8> Succs(TailBB->succ_begin(),
  646. TailBB->succ_end());
  647. SmallVector<MachineBasicBlock *, 8> Preds(TailBB->predecessors());
  648. bool Changed = false;
  649. for (MachineBasicBlock *PredBB : Preds) {
  650. if (PredBB->hasEHPadSuccessor() || PredBB->mayHaveInlineAsmBr())
  651. continue;
  652. if (bothUsedInPHI(*PredBB, Succs))
  653. continue;
  654. MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
  655. SmallVector<MachineOperand, 4> PredCond;
  656. if (TII->analyzeBranch(*PredBB, PredTBB, PredFBB, PredCond))
  657. continue;
  658. Changed = true;
  659. LLVM_DEBUG(dbgs() << "\nTail-duplicating into PredBB: " << *PredBB
  660. << "From simple Succ: " << *TailBB);
  661. MachineBasicBlock *NewTarget = *TailBB->succ_begin();
  662. MachineBasicBlock *NextBB = PredBB->getNextNode();
  663. // Make PredFBB explicit.
  664. if (PredCond.empty())
  665. PredFBB = PredTBB;
  666. // Make fall through explicit.
  667. if (!PredTBB)
  668. PredTBB = NextBB;
  669. if (!PredFBB)
  670. PredFBB = NextBB;
  671. // Redirect
  672. if (PredFBB == TailBB)
  673. PredFBB = NewTarget;
  674. if (PredTBB == TailBB)
  675. PredTBB = NewTarget;
  676. // Make the branch unconditional if possible
  677. if (PredTBB == PredFBB) {
  678. PredCond.clear();
  679. PredFBB = nullptr;
  680. }
  681. // Avoid adding fall through branches.
  682. if (PredFBB == NextBB)
  683. PredFBB = nullptr;
  684. if (PredTBB == NextBB && PredFBB == nullptr)
  685. PredTBB = nullptr;
  686. auto DL = PredBB->findBranchDebugLoc();
  687. TII->removeBranch(*PredBB);
  688. if (!PredBB->isSuccessor(NewTarget))
  689. PredBB->replaceSuccessor(TailBB, NewTarget);
  690. else {
  691. PredBB->removeSuccessor(TailBB, true);
  692. assert(PredBB->succ_size() <= 1);
  693. }
  694. if (PredTBB)
  695. TII->insertBranch(*PredBB, PredTBB, PredFBB, PredCond, DL);
  696. TDBBs.push_back(PredBB);
  697. }
  698. return Changed;
  699. }
  700. bool TailDuplicator::canTailDuplicate(MachineBasicBlock *TailBB,
  701. MachineBasicBlock *PredBB) {
  702. // EH edges are ignored by analyzeBranch.
  703. if (PredBB->succ_size() > 1)
  704. return false;
  705. MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
  706. SmallVector<MachineOperand, 4> PredCond;
  707. if (TII->analyzeBranch(*PredBB, PredTBB, PredFBB, PredCond))
  708. return false;
  709. if (!PredCond.empty())
  710. return false;
  711. return true;
  712. }
  713. /// If it is profitable, duplicate TailBB's contents in each
  714. /// of its predecessors.
  715. /// \p IsSimple result of isSimpleBB
  716. /// \p TailBB Block to be duplicated.
  717. /// \p ForcedLayoutPred When non-null, use this block as the layout predecessor
  718. /// instead of the previous block in MF's order.
  719. /// \p TDBBs A vector to keep track of all blocks tail-duplicated
  720. /// into.
  721. /// \p Copies A vector of copy instructions inserted. Used later to
  722. /// walk all the inserted copies and remove redundant ones.
  723. bool TailDuplicator::tailDuplicate(bool IsSimple, MachineBasicBlock *TailBB,
  724. MachineBasicBlock *ForcedLayoutPred,
  725. SmallVectorImpl<MachineBasicBlock *> &TDBBs,
  726. SmallVectorImpl<MachineInstr *> &Copies,
  727. SmallVectorImpl<MachineBasicBlock *> *CandidatePtr) {
  728. LLVM_DEBUG(dbgs() << "\n*** Tail-duplicating " << printMBBReference(*TailBB)
  729. << '\n');
  730. bool ShouldUpdateTerminators = TailBB->canFallThrough();
  731. DenseSet<Register> UsedByPhi;
  732. getRegsUsedByPHIs(*TailBB, &UsedByPhi);
  733. if (IsSimple)
  734. return duplicateSimpleBB(TailBB, TDBBs, UsedByPhi, Copies);
  735. // Iterate through all the unique predecessors and tail-duplicate this
  736. // block into them, if possible. Copying the list ahead of time also
  737. // avoids trouble with the predecessor list reallocating.
  738. bool Changed = false;
  739. SmallSetVector<MachineBasicBlock *, 8> Preds;
  740. if (CandidatePtr)
  741. Preds.insert(CandidatePtr->begin(), CandidatePtr->end());
  742. else
  743. Preds.insert(TailBB->pred_begin(), TailBB->pred_end());
  744. for (MachineBasicBlock *PredBB : Preds) {
  745. assert(TailBB != PredBB &&
  746. "Single-block loop should have been rejected earlier!");
  747. if (!canTailDuplicate(TailBB, PredBB))
  748. continue;
  749. // Don't duplicate into a fall-through predecessor (at least for now).
  750. // If profile is available, findDuplicateCandidates can choose better
  751. // fall-through predecessor.
  752. if (!(MF->getFunction().hasProfileData() && LayoutMode)) {
  753. bool IsLayoutSuccessor = false;
  754. if (ForcedLayoutPred)
  755. IsLayoutSuccessor = (ForcedLayoutPred == PredBB);
  756. else if (PredBB->isLayoutSuccessor(TailBB) && PredBB->canFallThrough())
  757. IsLayoutSuccessor = true;
  758. if (IsLayoutSuccessor)
  759. continue;
  760. }
  761. LLVM_DEBUG(dbgs() << "\nTail-duplicating into PredBB: " << *PredBB
  762. << "From Succ: " << *TailBB);
  763. TDBBs.push_back(PredBB);
  764. // Remove PredBB's unconditional branch.
  765. TII->removeBranch(*PredBB);
  766. // Clone the contents of TailBB into PredBB.
  767. DenseMap<Register, RegSubRegPair> LocalVRMap;
  768. SmallVector<std::pair<Register, RegSubRegPair>, 4> CopyInfos;
  769. for (MachineInstr &MI : llvm::make_early_inc_range(*TailBB)) {
  770. if (MI.isPHI()) {
  771. // Replace the uses of the def of the PHI with the register coming
  772. // from PredBB.
  773. processPHI(&MI, TailBB, PredBB, LocalVRMap, CopyInfos, UsedByPhi, true);
  774. } else {
  775. // Replace def of virtual registers with new registers, and update
  776. // uses with PHI source register or the new registers.
  777. duplicateInstruction(&MI, TailBB, PredBB, LocalVRMap, UsedByPhi);
  778. }
  779. }
  780. appendCopies(PredBB, CopyInfos, Copies);
  781. NumTailDupAdded += TailBB->size() - 1; // subtract one for removed branch
  782. // Update the CFG.
  783. PredBB->removeSuccessor(PredBB->succ_begin());
  784. assert(PredBB->succ_empty() &&
  785. "TailDuplicate called on block with multiple successors!");
  786. for (MachineBasicBlock *Succ : TailBB->successors())
  787. PredBB->addSuccessor(Succ, MBPI->getEdgeProbability(TailBB, Succ));
  788. // Update branches in pred to jump to tail's layout successor if needed.
  789. if (ShouldUpdateTerminators)
  790. PredBB->updateTerminator(TailBB->getNextNode());
  791. Changed = true;
  792. ++NumTailDups;
  793. }
  794. // If TailBB was duplicated into all its predecessors except for the prior
  795. // block, which falls through unconditionally, move the contents of this
  796. // block into the prior block.
  797. MachineBasicBlock *PrevBB = ForcedLayoutPred;
  798. if (!PrevBB)
  799. PrevBB = &*std::prev(TailBB->getIterator());
  800. MachineBasicBlock *PriorTBB = nullptr, *PriorFBB = nullptr;
  801. SmallVector<MachineOperand, 4> PriorCond;
  802. // This has to check PrevBB->succ_size() because EH edges are ignored by
  803. // analyzeBranch.
  804. if (PrevBB->succ_size() == 1 &&
  805. // Layout preds are not always CFG preds. Check.
  806. *PrevBB->succ_begin() == TailBB &&
  807. !TII->analyzeBranch(*PrevBB, PriorTBB, PriorFBB, PriorCond) &&
  808. PriorCond.empty() &&
  809. (!PriorTBB || PriorTBB == TailBB) &&
  810. TailBB->pred_size() == 1 &&
  811. !TailBB->hasAddressTaken()) {
  812. LLVM_DEBUG(dbgs() << "\nMerging into block: " << *PrevBB
  813. << "From MBB: " << *TailBB);
  814. // There may be a branch to the layout successor. This is unlikely but it
  815. // happens. The correct thing to do is to remove the branch before
  816. // duplicating the instructions in all cases.
  817. bool RemovedBranches = TII->removeBranch(*PrevBB) != 0;
  818. // If there are still tail instructions, abort the merge
  819. if (PrevBB->getFirstTerminator() == PrevBB->end()) {
  820. if (PreRegAlloc) {
  821. DenseMap<Register, RegSubRegPair> LocalVRMap;
  822. SmallVector<std::pair<Register, RegSubRegPair>, 4> CopyInfos;
  823. MachineBasicBlock::iterator I = TailBB->begin();
  824. // Process PHI instructions first.
  825. while (I != TailBB->end() && I->isPHI()) {
  826. // Replace the uses of the def of the PHI with the register coming
  827. // from PredBB.
  828. MachineInstr *MI = &*I++;
  829. processPHI(MI, TailBB, PrevBB, LocalVRMap, CopyInfos, UsedByPhi,
  830. true);
  831. }
  832. // Now copy the non-PHI instructions.
  833. while (I != TailBB->end()) {
  834. // Replace def of virtual registers with new registers, and update
  835. // uses with PHI source register or the new registers.
  836. MachineInstr *MI = &*I++;
  837. assert(!MI->isBundle() && "Not expecting bundles before regalloc!");
  838. duplicateInstruction(MI, TailBB, PrevBB, LocalVRMap, UsedByPhi);
  839. MI->eraseFromParent();
  840. }
  841. appendCopies(PrevBB, CopyInfos, Copies);
  842. } else {
  843. TII->removeBranch(*PrevBB);
  844. // No PHIs to worry about, just splice the instructions over.
  845. PrevBB->splice(PrevBB->end(), TailBB, TailBB->begin(), TailBB->end());
  846. }
  847. PrevBB->removeSuccessor(PrevBB->succ_begin());
  848. assert(PrevBB->succ_empty());
  849. PrevBB->transferSuccessors(TailBB);
  850. // Update branches in PrevBB based on Tail's layout successor.
  851. if (ShouldUpdateTerminators)
  852. PrevBB->updateTerminator(TailBB->getNextNode());
  853. TDBBs.push_back(PrevBB);
  854. Changed = true;
  855. } else {
  856. LLVM_DEBUG(dbgs() << "Abort merging blocks, the predecessor still "
  857. "contains terminator instructions");
  858. // Return early if no changes were made
  859. if (!Changed)
  860. return RemovedBranches;
  861. }
  862. Changed |= RemovedBranches;
  863. }
  864. // If this is after register allocation, there are no phis to fix.
  865. if (!PreRegAlloc)
  866. return Changed;
  867. // If we made no changes so far, we are safe.
  868. if (!Changed)
  869. return Changed;
  870. // Handle the nasty case in that we duplicated a block that is part of a loop
  871. // into some but not all of its predecessors. For example:
  872. // 1 -> 2 <-> 3 |
  873. // \ |
  874. // \---> rest |
  875. // if we duplicate 2 into 1 but not into 3, we end up with
  876. // 12 -> 3 <-> 2 -> rest |
  877. // \ / |
  878. // \----->-----/ |
  879. // If there was a "var = phi(1, 3)" in 2, it has to be ultimately replaced
  880. // with a phi in 3 (which now dominates 2).
  881. // What we do here is introduce a copy in 3 of the register defined by the
  882. // phi, just like when we are duplicating 2 into 3, but we don't copy any
  883. // real instructions or remove the 3 -> 2 edge from the phi in 2.
  884. for (MachineBasicBlock *PredBB : Preds) {
  885. if (is_contained(TDBBs, PredBB))
  886. continue;
  887. // EH edges
  888. if (PredBB->succ_size() != 1)
  889. continue;
  890. DenseMap<Register, RegSubRegPair> LocalVRMap;
  891. SmallVector<std::pair<Register, RegSubRegPair>, 4> CopyInfos;
  892. MachineBasicBlock::iterator I = TailBB->begin();
  893. // Process PHI instructions first.
  894. while (I != TailBB->end() && I->isPHI()) {
  895. // Replace the uses of the def of the PHI with the register coming
  896. // from PredBB.
  897. MachineInstr *MI = &*I++;
  898. processPHI(MI, TailBB, PredBB, LocalVRMap, CopyInfos, UsedByPhi, false);
  899. }
  900. appendCopies(PredBB, CopyInfos, Copies);
  901. }
  902. return Changed;
  903. }
  904. /// At the end of the block \p MBB generate COPY instructions between registers
  905. /// described by \p CopyInfos. Append resulting instructions to \p Copies.
  906. void TailDuplicator::appendCopies(MachineBasicBlock *MBB,
  907. SmallVectorImpl<std::pair<Register, RegSubRegPair>> &CopyInfos,
  908. SmallVectorImpl<MachineInstr*> &Copies) {
  909. MachineBasicBlock::iterator Loc = MBB->getFirstTerminator();
  910. const MCInstrDesc &CopyD = TII->get(TargetOpcode::COPY);
  911. for (auto &CI : CopyInfos) {
  912. auto C = BuildMI(*MBB, Loc, DebugLoc(), CopyD, CI.first)
  913. .addReg(CI.second.Reg, 0, CI.second.SubReg);
  914. Copies.push_back(C);
  915. }
  916. }
  917. /// Remove the specified dead machine basic block from the function, updating
  918. /// the CFG.
  919. void TailDuplicator::removeDeadBlock(
  920. MachineBasicBlock *MBB,
  921. function_ref<void(MachineBasicBlock *)> *RemovalCallback) {
  922. assert(MBB->pred_empty() && "MBB must be dead!");
  923. LLVM_DEBUG(dbgs() << "\nRemoving MBB: " << *MBB);
  924. MachineFunction *MF = MBB->getParent();
  925. // Update the call site info.
  926. for (const MachineInstr &MI : *MBB)
  927. if (MI.shouldUpdateCallSiteInfo())
  928. MF->eraseCallSiteInfo(&MI);
  929. if (RemovalCallback)
  930. (*RemovalCallback)(MBB);
  931. // Remove all successors.
  932. while (!MBB->succ_empty())
  933. MBB->removeSuccessor(MBB->succ_end() - 1);
  934. // Remove the block.
  935. MBB->eraseFromParent();
  936. }