BranchRelaxation.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. //===- BranchRelaxation.cpp -----------------------------------------------===//
  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. #include "llvm/ADT/SmallVector.h"
  9. #include "llvm/ADT/Statistic.h"
  10. #include "llvm/CodeGen/LivePhysRegs.h"
  11. #include "llvm/CodeGen/MachineBasicBlock.h"
  12. #include "llvm/CodeGen/MachineFunction.h"
  13. #include "llvm/CodeGen/MachineFunctionPass.h"
  14. #include "llvm/CodeGen/MachineInstr.h"
  15. #include "llvm/CodeGen/RegisterScavenging.h"
  16. #include "llvm/CodeGen/TargetInstrInfo.h"
  17. #include "llvm/CodeGen/TargetRegisterInfo.h"
  18. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  19. #include "llvm/Config/llvm-config.h"
  20. #include "llvm/IR/DebugLoc.h"
  21. #include "llvm/Pass.h"
  22. #include "llvm/Support/Compiler.h"
  23. #include "llvm/Support/Debug.h"
  24. #include "llvm/Support/Format.h"
  25. #include "llvm/Support/MathExtras.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. #include <cassert>
  28. #include <cstdint>
  29. #include <iterator>
  30. #include <memory>
  31. using namespace llvm;
  32. #define DEBUG_TYPE "branch-relaxation"
  33. STATISTIC(NumSplit, "Number of basic blocks split");
  34. STATISTIC(NumConditionalRelaxed, "Number of conditional branches relaxed");
  35. STATISTIC(NumUnconditionalRelaxed, "Number of unconditional branches relaxed");
  36. #define BRANCH_RELAX_NAME "Branch relaxation pass"
  37. namespace {
  38. class BranchRelaxation : public MachineFunctionPass {
  39. /// BasicBlockInfo - Information about the offset and size of a single
  40. /// basic block.
  41. struct BasicBlockInfo {
  42. /// Offset - Distance from the beginning of the function to the beginning
  43. /// of this basic block.
  44. ///
  45. /// The offset is always aligned as required by the basic block.
  46. unsigned Offset = 0;
  47. /// Size - Size of the basic block in bytes. If the block contains
  48. /// inline assembly, this is a worst case estimate.
  49. ///
  50. /// The size does not include any alignment padding whether from the
  51. /// beginning of the block, or from an aligned jump table at the end.
  52. unsigned Size = 0;
  53. BasicBlockInfo() = default;
  54. /// Compute the offset immediately following this block. \p MBB is the next
  55. /// block.
  56. unsigned postOffset(const MachineBasicBlock &MBB) const {
  57. unsigned PO = Offset + Size;
  58. unsigned LogAlign = MBB.getLogAlignment();
  59. if (LogAlign == 0)
  60. return PO;
  61. unsigned AlignAmt = 1 << LogAlign;
  62. unsigned ParentLogAlign = MBB.getParent()->getLogAlignment();
  63. if (LogAlign <= ParentLogAlign)
  64. return PO + OffsetToAlignment(PO, AlignAmt);
  65. // The alignment of this MBB is larger than the function's alignment, so we
  66. // can't tell whether or not it will insert nops. Assume that it will.
  67. return PO + AlignAmt + OffsetToAlignment(PO, AlignAmt);
  68. }
  69. };
  70. SmallVector<BasicBlockInfo, 16> BlockInfo;
  71. std::unique_ptr<RegScavenger> RS;
  72. LivePhysRegs LiveRegs;
  73. MachineFunction *MF;
  74. const TargetRegisterInfo *TRI;
  75. const TargetInstrInfo *TII;
  76. bool relaxBranchInstructions();
  77. void scanFunction();
  78. MachineBasicBlock *createNewBlockAfter(MachineBasicBlock &BB);
  79. MachineBasicBlock *splitBlockBeforeInstr(MachineInstr &MI,
  80. MachineBasicBlock *DestBB);
  81. void adjustBlockOffsets(MachineBasicBlock &Start);
  82. bool isBlockInRange(const MachineInstr &MI, const MachineBasicBlock &BB) const;
  83. bool fixupConditionalBranch(MachineInstr &MI);
  84. bool fixupUnconditionalBranch(MachineInstr &MI);
  85. uint64_t computeBlockSize(const MachineBasicBlock &MBB) const;
  86. unsigned getInstrOffset(const MachineInstr &MI) const;
  87. void dumpBBs();
  88. void verify();
  89. public:
  90. static char ID;
  91. BranchRelaxation() : MachineFunctionPass(ID) {}
  92. bool runOnMachineFunction(MachineFunction &MF) override;
  93. StringRef getPassName() const override { return BRANCH_RELAX_NAME; }
  94. };
  95. } // end anonymous namespace
  96. char BranchRelaxation::ID = 0;
  97. char &llvm::BranchRelaxationPassID = BranchRelaxation::ID;
  98. INITIALIZE_PASS(BranchRelaxation, DEBUG_TYPE, BRANCH_RELAX_NAME, false, false)
  99. /// verify - check BBOffsets, BBSizes, alignment of islands
  100. void BranchRelaxation::verify() {
  101. #ifndef NDEBUG
  102. unsigned PrevNum = MF->begin()->getNumber();
  103. for (MachineBasicBlock &MBB : *MF) {
  104. unsigned LogAlign = MBB.getLogAlignment();
  105. unsigned Num = MBB.getNumber();
  106. assert(BlockInfo[Num].Offset % (1u << LogAlign) == 0);
  107. assert(!Num || BlockInfo[PrevNum].postOffset(MBB) <= BlockInfo[Num].Offset);
  108. assert(BlockInfo[Num].Size == computeBlockSize(MBB));
  109. PrevNum = Num;
  110. }
  111. #endif
  112. }
  113. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  114. /// print block size and offset information - debugging
  115. LLVM_DUMP_METHOD void BranchRelaxation::dumpBBs() {
  116. for (auto &MBB : *MF) {
  117. const BasicBlockInfo &BBI = BlockInfo[MBB.getNumber()];
  118. dbgs() << format("%%bb.%u\toffset=%08x\t", MBB.getNumber(), BBI.Offset)
  119. << format("size=%#x\n", BBI.Size);
  120. }
  121. }
  122. #endif
  123. /// scanFunction - Do the initial scan of the function, building up
  124. /// information about each block.
  125. void BranchRelaxation::scanFunction() {
  126. BlockInfo.clear();
  127. BlockInfo.resize(MF->getNumBlockIDs());
  128. // First thing, compute the size of all basic blocks, and see if the function
  129. // has any inline assembly in it. If so, we have to be conservative about
  130. // alignment assumptions, as we don't know for sure the size of any
  131. // instructions in the inline assembly.
  132. for (MachineBasicBlock &MBB : *MF)
  133. BlockInfo[MBB.getNumber()].Size = computeBlockSize(MBB);
  134. // Compute block offsets and known bits.
  135. adjustBlockOffsets(*MF->begin());
  136. }
  137. /// computeBlockSize - Compute the size for MBB.
  138. uint64_t BranchRelaxation::computeBlockSize(const MachineBasicBlock &MBB) const {
  139. uint64_t Size = 0;
  140. for (const MachineInstr &MI : MBB)
  141. Size += TII->getInstSizeInBytes(MI);
  142. return Size;
  143. }
  144. /// getInstrOffset - Return the current offset of the specified machine
  145. /// instruction from the start of the function. This offset changes as stuff is
  146. /// moved around inside the function.
  147. unsigned BranchRelaxation::getInstrOffset(const MachineInstr &MI) const {
  148. const MachineBasicBlock *MBB = MI.getParent();
  149. // The offset is composed of two things: the sum of the sizes of all MBB's
  150. // before this instruction's block, and the offset from the start of the block
  151. // it is in.
  152. unsigned Offset = BlockInfo[MBB->getNumber()].Offset;
  153. // Sum instructions before MI in MBB.
  154. for (MachineBasicBlock::const_iterator I = MBB->begin(); &*I != &MI; ++I) {
  155. assert(I != MBB->end() && "Didn't find MI in its own basic block?");
  156. Offset += TII->getInstSizeInBytes(*I);
  157. }
  158. return Offset;
  159. }
  160. void BranchRelaxation::adjustBlockOffsets(MachineBasicBlock &Start) {
  161. unsigned PrevNum = Start.getNumber();
  162. for (auto &MBB : make_range(MachineFunction::iterator(Start), MF->end())) {
  163. unsigned Num = MBB.getNumber();
  164. if (!Num) // block zero is never changed from offset zero.
  165. continue;
  166. // Get the offset and known bits at the end of the layout predecessor.
  167. // Include the alignment of the current block.
  168. BlockInfo[Num].Offset = BlockInfo[PrevNum].postOffset(MBB);
  169. PrevNum = Num;
  170. }
  171. }
  172. /// Insert a new empty basic block and insert it after \BB
  173. MachineBasicBlock *BranchRelaxation::createNewBlockAfter(MachineBasicBlock &BB) {
  174. // Create a new MBB for the code after the OrigBB.
  175. MachineBasicBlock *NewBB =
  176. MF->CreateMachineBasicBlock(BB.getBasicBlock());
  177. MF->insert(++BB.getIterator(), NewBB);
  178. // Insert an entry into BlockInfo to align it properly with the block numbers.
  179. BlockInfo.insert(BlockInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
  180. return NewBB;
  181. }
  182. /// Split the basic block containing MI into two blocks, which are joined by
  183. /// an unconditional branch. Update data structures and renumber blocks to
  184. /// account for this change and returns the newly created block.
  185. MachineBasicBlock *BranchRelaxation::splitBlockBeforeInstr(MachineInstr &MI,
  186. MachineBasicBlock *DestBB) {
  187. MachineBasicBlock *OrigBB = MI.getParent();
  188. // Create a new MBB for the code after the OrigBB.
  189. MachineBasicBlock *NewBB =
  190. MF->CreateMachineBasicBlock(OrigBB->getBasicBlock());
  191. MF->insert(++OrigBB->getIterator(), NewBB);
  192. // Splice the instructions starting with MI over to NewBB.
  193. NewBB->splice(NewBB->end(), OrigBB, MI.getIterator(), OrigBB->end());
  194. // Add an unconditional branch from OrigBB to NewBB.
  195. // Note the new unconditional branch is not being recorded.
  196. // There doesn't seem to be meaningful DebugInfo available; this doesn't
  197. // correspond to anything in the source.
  198. TII->insertUnconditionalBranch(*OrigBB, NewBB, DebugLoc());
  199. // Insert an entry into BlockInfo to align it properly with the block numbers.
  200. BlockInfo.insert(BlockInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
  201. NewBB->transferSuccessors(OrigBB);
  202. OrigBB->addSuccessor(NewBB);
  203. OrigBB->addSuccessor(DestBB);
  204. // Cleanup potential unconditional branch to successor block.
  205. // Note that updateTerminator may change the size of the blocks.
  206. NewBB->updateTerminator();
  207. OrigBB->updateTerminator();
  208. // Figure out how large the OrigBB is. As the first half of the original
  209. // block, it cannot contain a tablejump. The size includes
  210. // the new jump we added. (It should be possible to do this without
  211. // recounting everything, but it's very confusing, and this is rarely
  212. // executed.)
  213. BlockInfo[OrigBB->getNumber()].Size = computeBlockSize(*OrigBB);
  214. // Figure out how large the NewMBB is. As the second half of the original
  215. // block, it may contain a tablejump.
  216. BlockInfo[NewBB->getNumber()].Size = computeBlockSize(*NewBB);
  217. // All BBOffsets following these blocks must be modified.
  218. adjustBlockOffsets(*OrigBB);
  219. // Need to fix live-in lists if we track liveness.
  220. if (TRI->trackLivenessAfterRegAlloc(*MF))
  221. computeAndAddLiveIns(LiveRegs, *NewBB);
  222. ++NumSplit;
  223. return NewBB;
  224. }
  225. /// isBlockInRange - Returns true if the distance between specific MI and
  226. /// specific BB can fit in MI's displacement field.
  227. bool BranchRelaxation::isBlockInRange(
  228. const MachineInstr &MI, const MachineBasicBlock &DestBB) const {
  229. int64_t BrOffset = getInstrOffset(MI);
  230. int64_t DestOffset = BlockInfo[DestBB.getNumber()].Offset;
  231. if (TII->isBranchOffsetInRange(MI.getOpcode(), DestOffset - BrOffset))
  232. return true;
  233. LLVM_DEBUG(dbgs() << "Out of range branch to destination "
  234. << printMBBReference(DestBB) << " from "
  235. << printMBBReference(*MI.getParent()) << " to "
  236. << DestOffset << " offset " << DestOffset - BrOffset << '\t'
  237. << MI);
  238. return false;
  239. }
  240. /// fixupConditionalBranch - Fix up a conditional branch whose destination is
  241. /// too far away to fit in its displacement field. It is converted to an inverse
  242. /// conditional branch + an unconditional branch to the destination.
  243. bool BranchRelaxation::fixupConditionalBranch(MachineInstr &MI) {
  244. DebugLoc DL = MI.getDebugLoc();
  245. MachineBasicBlock *MBB = MI.getParent();
  246. MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
  247. MachineBasicBlock *NewBB = nullptr;
  248. SmallVector<MachineOperand, 4> Cond;
  249. auto insertUncondBranch = [&](MachineBasicBlock *MBB,
  250. MachineBasicBlock *DestBB) {
  251. unsigned &BBSize = BlockInfo[MBB->getNumber()].Size;
  252. int NewBrSize = 0;
  253. TII->insertUnconditionalBranch(*MBB, DestBB, DL, &NewBrSize);
  254. BBSize += NewBrSize;
  255. };
  256. auto insertBranch = [&](MachineBasicBlock *MBB, MachineBasicBlock *TBB,
  257. MachineBasicBlock *FBB,
  258. SmallVectorImpl<MachineOperand>& Cond) {
  259. unsigned &BBSize = BlockInfo[MBB->getNumber()].Size;
  260. int NewBrSize = 0;
  261. TII->insertBranch(*MBB, TBB, FBB, Cond, DL, &NewBrSize);
  262. BBSize += NewBrSize;
  263. };
  264. auto removeBranch = [&](MachineBasicBlock *MBB) {
  265. unsigned &BBSize = BlockInfo[MBB->getNumber()].Size;
  266. int RemovedSize = 0;
  267. TII->removeBranch(*MBB, &RemovedSize);
  268. BBSize -= RemovedSize;
  269. };
  270. auto finalizeBlockChanges = [&](MachineBasicBlock *MBB,
  271. MachineBasicBlock *NewBB) {
  272. // Keep the block offsets up to date.
  273. adjustBlockOffsets(*MBB);
  274. // Need to fix live-in lists if we track liveness.
  275. if (NewBB && TRI->trackLivenessAfterRegAlloc(*MF))
  276. computeAndAddLiveIns(LiveRegs, *NewBB);
  277. };
  278. bool Fail = TII->analyzeBranch(*MBB, TBB, FBB, Cond);
  279. assert(!Fail && "branches to be relaxed must be analyzable");
  280. (void)Fail;
  281. // Add an unconditional branch to the destination and invert the branch
  282. // condition to jump over it:
  283. // tbz L1
  284. // =>
  285. // tbnz L2
  286. // b L1
  287. // L2:
  288. bool ReversedCond = !TII->reverseBranchCondition(Cond);
  289. if (ReversedCond) {
  290. if (FBB && isBlockInRange(MI, *FBB)) {
  291. // Last MI in the BB is an unconditional branch. We can simply invert the
  292. // condition and swap destinations:
  293. // beq L1
  294. // b L2
  295. // =>
  296. // bne L2
  297. // b L1
  298. LLVM_DEBUG(dbgs() << " Invert condition and swap "
  299. "its destination with "
  300. << MBB->back());
  301. removeBranch(MBB);
  302. insertBranch(MBB, FBB, TBB, Cond);
  303. finalizeBlockChanges(MBB, nullptr);
  304. return true;
  305. }
  306. if (FBB) {
  307. // We need to split the basic block here to obtain two long-range
  308. // unconditional branches.
  309. NewBB = createNewBlockAfter(*MBB);
  310. insertUncondBranch(NewBB, FBB);
  311. // Update the succesor lists according to the transformation to follow.
  312. // Do it here since if there's no split, no update is needed.
  313. MBB->replaceSuccessor(FBB, NewBB);
  314. NewBB->addSuccessor(FBB);
  315. }
  316. // We now have an appropriate fall-through block in place (either naturally or
  317. // just created), so we can use the inverted the condition.
  318. MachineBasicBlock &NextBB = *std::next(MachineFunction::iterator(MBB));
  319. LLVM_DEBUG(dbgs() << " Insert B to " << printMBBReference(*TBB)
  320. << ", invert condition and change dest. to "
  321. << printMBBReference(NextBB) << '\n');
  322. removeBranch(MBB);
  323. // Insert a new conditional branch and a new unconditional branch.
  324. insertBranch(MBB, &NextBB, TBB, Cond);
  325. finalizeBlockChanges(MBB, NewBB);
  326. return true;
  327. }
  328. // Branch cond can't be inverted.
  329. // In this case we always add a block after the MBB.
  330. LLVM_DEBUG(dbgs() << " The branch condition can't be inverted. "
  331. << " Insert a new BB after " << MBB->back());
  332. if (!FBB)
  333. FBB = &(*std::next(MachineFunction::iterator(MBB)));
  334. // This is the block with cond. branch and the distance to TBB is too long.
  335. // beq L1
  336. // L2:
  337. // We do the following transformation:
  338. // beq NewBB
  339. // b L2
  340. // NewBB:
  341. // b L1
  342. // L2:
  343. NewBB = createNewBlockAfter(*MBB);
  344. insertUncondBranch(NewBB, TBB);
  345. LLVM_DEBUG(dbgs() << " Insert cond B to the new BB "
  346. << printMBBReference(*NewBB)
  347. << " Keep the exiting condition.\n"
  348. << " Insert B to " << printMBBReference(*FBB) << ".\n"
  349. << " In the new BB: Insert B to "
  350. << printMBBReference(*TBB) << ".\n");
  351. // Update the successor lists according to the transformation to follow.
  352. MBB->replaceSuccessor(TBB, NewBB);
  353. NewBB->addSuccessor(TBB);
  354. // Replace branch in the current (MBB) block.
  355. removeBranch(MBB);
  356. insertBranch(MBB, NewBB, FBB, Cond);
  357. finalizeBlockChanges(MBB, NewBB);
  358. return true;
  359. }
  360. bool BranchRelaxation::fixupUnconditionalBranch(MachineInstr &MI) {
  361. MachineBasicBlock *MBB = MI.getParent();
  362. unsigned OldBrSize = TII->getInstSizeInBytes(MI);
  363. MachineBasicBlock *DestBB = TII->getBranchDestBlock(MI);
  364. int64_t DestOffset = BlockInfo[DestBB->getNumber()].Offset;
  365. int64_t SrcOffset = getInstrOffset(MI);
  366. assert(!TII->isBranchOffsetInRange(MI.getOpcode(), DestOffset - SrcOffset));
  367. BlockInfo[MBB->getNumber()].Size -= OldBrSize;
  368. MachineBasicBlock *BranchBB = MBB;
  369. // If this was an expanded conditional branch, there is already a single
  370. // unconditional branch in a block.
  371. if (!MBB->empty()) {
  372. BranchBB = createNewBlockAfter(*MBB);
  373. // Add live outs.
  374. for (const MachineBasicBlock *Succ : MBB->successors()) {
  375. for (const MachineBasicBlock::RegisterMaskPair &LiveIn : Succ->liveins())
  376. BranchBB->addLiveIn(LiveIn);
  377. }
  378. BranchBB->sortUniqueLiveIns();
  379. BranchBB->addSuccessor(DestBB);
  380. MBB->replaceSuccessor(DestBB, BranchBB);
  381. }
  382. DebugLoc DL = MI.getDebugLoc();
  383. MI.eraseFromParent();
  384. BlockInfo[BranchBB->getNumber()].Size += TII->insertIndirectBranch(
  385. *BranchBB, *DestBB, DL, DestOffset - SrcOffset, RS.get());
  386. adjustBlockOffsets(*MBB);
  387. return true;
  388. }
  389. bool BranchRelaxation::relaxBranchInstructions() {
  390. bool Changed = false;
  391. // Relaxing branches involves creating new basic blocks, so re-eval
  392. // end() for termination.
  393. for (MachineFunction::iterator I = MF->begin(); I != MF->end(); ++I) {
  394. MachineBasicBlock &MBB = *I;
  395. // Empty block?
  396. MachineBasicBlock::iterator Last = MBB.getLastNonDebugInstr();
  397. if (Last == MBB.end())
  398. continue;
  399. // Expand the unconditional branch first if necessary. If there is a
  400. // conditional branch, this will end up changing the branch destination of
  401. // it to be over the newly inserted indirect branch block, which may avoid
  402. // the need to try expanding the conditional branch first, saving an extra
  403. // jump.
  404. if (Last->isUnconditionalBranch()) {
  405. // Unconditional branch destination might be unanalyzable, assume these
  406. // are OK.
  407. if (MachineBasicBlock *DestBB = TII->getBranchDestBlock(*Last)) {
  408. if (!isBlockInRange(*Last, *DestBB)) {
  409. fixupUnconditionalBranch(*Last);
  410. ++NumUnconditionalRelaxed;
  411. Changed = true;
  412. }
  413. }
  414. }
  415. // Loop over the conditional branches.
  416. MachineBasicBlock::iterator Next;
  417. for (MachineBasicBlock::iterator J = MBB.getFirstTerminator();
  418. J != MBB.end(); J = Next) {
  419. Next = std::next(J);
  420. MachineInstr &MI = *J;
  421. if (MI.isConditionalBranch()) {
  422. MachineBasicBlock *DestBB = TII->getBranchDestBlock(MI);
  423. if (!isBlockInRange(MI, *DestBB)) {
  424. if (Next != MBB.end() && Next->isConditionalBranch()) {
  425. // If there are multiple conditional branches, this isn't an
  426. // analyzable block. Split later terminators into a new block so
  427. // each one will be analyzable.
  428. splitBlockBeforeInstr(*Next, DestBB);
  429. } else {
  430. fixupConditionalBranch(MI);
  431. ++NumConditionalRelaxed;
  432. }
  433. Changed = true;
  434. // This may have modified all of the terminators, so start over.
  435. Next = MBB.getFirstTerminator();
  436. }
  437. }
  438. }
  439. }
  440. return Changed;
  441. }
  442. bool BranchRelaxation::runOnMachineFunction(MachineFunction &mf) {
  443. MF = &mf;
  444. LLVM_DEBUG(dbgs() << "***** BranchRelaxation *****\n");
  445. const TargetSubtargetInfo &ST = MF->getSubtarget();
  446. TII = ST.getInstrInfo();
  447. TRI = ST.getRegisterInfo();
  448. if (TRI->trackLivenessAfterRegAlloc(*MF))
  449. RS.reset(new RegScavenger());
  450. // Renumber all of the machine basic blocks in the function, guaranteeing that
  451. // the numbers agree with the position of the block in the function.
  452. MF->RenumberBlocks();
  453. // Do the initial scan of the function, building up information about the
  454. // sizes of each block.
  455. scanFunction();
  456. LLVM_DEBUG(dbgs() << " Basic blocks before relaxation\n"; dumpBBs(););
  457. bool MadeChange = false;
  458. while (relaxBranchInstructions())
  459. MadeChange = true;
  460. // After a while, this might be made debug-only, but it is not expensive.
  461. verify();
  462. LLVM_DEBUG(dbgs() << " Basic blocks after relaxation\n\n"; dumpBBs());
  463. BlockInfo.clear();
  464. return MadeChange;
  465. }