BranchRelaxation.cpp 20 KB

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