BranchRelaxation.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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/CodeGen/Passes.h"
  10. #include "llvm/ADT/SmallVector.h"
  11. #include "llvm/ADT/Statistic.h"
  12. #include "llvm/CodeGen/MachineFunctionPass.h"
  13. #include "llvm/Target/TargetInstrInfo.h"
  14. #include "llvm/Target/TargetSubtargetInfo.h"
  15. #include "llvm/Support/Debug.h"
  16. #include "llvm/Support/Format.h"
  17. #include "llvm/Support/raw_ostream.h"
  18. using namespace llvm;
  19. #define DEBUG_TYPE "branch-relaxation"
  20. STATISTIC(NumSplit, "Number of basic blocks split");
  21. STATISTIC(NumConditionalRelaxed, "Number of conditional branches relaxed");
  22. #define BRANCH_RELAX_NAME "Branch relaxation pass"
  23. namespace {
  24. class BranchRelaxation : public MachineFunctionPass {
  25. /// BasicBlockInfo - Information about the offset and size of a single
  26. /// basic block.
  27. struct BasicBlockInfo {
  28. /// Offset - Distance from the beginning of the function to the beginning
  29. /// of this basic block.
  30. ///
  31. /// The offset is always aligned as required by the basic block.
  32. unsigned Offset;
  33. /// Size - Size of the basic block in bytes. If the block contains
  34. /// inline assembly, this is a worst case estimate.
  35. ///
  36. /// The size does not include any alignment padding whether from the
  37. /// beginning of the block, or from an aligned jump table at the end.
  38. unsigned Size;
  39. BasicBlockInfo() : Offset(0), Size(0) {}
  40. /// Compute the offset immediately following this block. \p MBB is the next
  41. /// block.
  42. unsigned postOffset(const MachineBasicBlock &MBB) const {
  43. unsigned PO = Offset + Size;
  44. unsigned Align = MBB.getAlignment();
  45. if (Align == 0)
  46. return PO;
  47. unsigned AlignAmt = 1 << Align;
  48. unsigned ParentAlign = MBB.getParent()->getAlignment();
  49. if (Align <= ParentAlign)
  50. return PO + OffsetToAlignment(PO, AlignAmt);
  51. // The alignment of this MBB is larger than the function's alignment, so we
  52. // can't tell whether or not it will insert nops. Assume that it will.
  53. return PO + AlignAmt + OffsetToAlignment(PO, AlignAmt);
  54. }
  55. };
  56. SmallVector<BasicBlockInfo, 16> BlockInfo;
  57. MachineFunction *MF;
  58. const TargetInstrInfo *TII;
  59. bool relaxBranchInstructions();
  60. void scanFunction();
  61. MachineBasicBlock *splitBlockBeforeInstr(MachineInstr &MI);
  62. void adjustBlockOffsets(MachineBasicBlock &MBB);
  63. bool isBlockInRange(const MachineInstr &MI, const MachineBasicBlock &BB) const;
  64. bool fixupConditionalBranch(MachineInstr &MI);
  65. uint64_t computeBlockSize(const MachineBasicBlock &MBB) const;
  66. unsigned getInstrOffset(const MachineInstr &MI) const;
  67. void dumpBBs();
  68. void verify();
  69. public:
  70. static char ID;
  71. BranchRelaxation() : MachineFunctionPass(ID) { }
  72. bool runOnMachineFunction(MachineFunction &MF) override;
  73. StringRef getPassName() const override {
  74. return BRANCH_RELAX_NAME;
  75. }
  76. };
  77. }
  78. char BranchRelaxation::ID = 0;
  79. char &llvm::BranchRelaxationPassID = BranchRelaxation::ID;
  80. INITIALIZE_PASS(BranchRelaxation, DEBUG_TYPE, BRANCH_RELAX_NAME, false, false)
  81. /// verify - check BBOffsets, BBSizes, alignment of islands
  82. void BranchRelaxation::verify() {
  83. #ifndef NDEBUG
  84. unsigned PrevNum = MF->begin()->getNumber();
  85. for (MachineBasicBlock &MBB : *MF) {
  86. unsigned Align = MBB.getAlignment();
  87. unsigned Num = MBB.getNumber();
  88. assert(BlockInfo[Num].Offset % (1u << Align) == 0);
  89. assert(!Num || BlockInfo[PrevNum].postOffset(MBB) <= BlockInfo[Num].Offset);
  90. PrevNum = Num;
  91. }
  92. #endif
  93. }
  94. /// print block size and offset information - debugging
  95. void BranchRelaxation::dumpBBs() {
  96. for (auto &MBB : *MF) {
  97. const BasicBlockInfo &BBI = BlockInfo[MBB.getNumber()];
  98. dbgs() << format("BB#%u\toffset=%08x\t", MBB.getNumber(), BBI.Offset)
  99. << format("size=%#x\n", BBI.Size);
  100. }
  101. }
  102. /// scanFunction - Do the initial scan of the function, building up
  103. /// information about each block.
  104. void BranchRelaxation::scanFunction() {
  105. BlockInfo.clear();
  106. BlockInfo.resize(MF->getNumBlockIDs());
  107. // First thing, compute the size of all basic blocks, and see if the function
  108. // has any inline assembly in it. If so, we have to be conservative about
  109. // alignment assumptions, as we don't know for sure the size of any
  110. // instructions in the inline assembly.
  111. for (MachineBasicBlock &MBB : *MF)
  112. BlockInfo[MBB.getNumber()].Size = computeBlockSize(MBB);
  113. // Compute block offsets and known bits.
  114. adjustBlockOffsets(*MF->begin());
  115. }
  116. /// computeBlockSize - Compute the size for MBB.
  117. uint64_t BranchRelaxation::computeBlockSize(const MachineBasicBlock &MBB) const {
  118. uint64_t Size = 0;
  119. for (const MachineInstr &MI : MBB)
  120. Size += TII->getInstSizeInBytes(MI);
  121. return Size;
  122. }
  123. /// getInstrOffset - Return the current offset of the specified machine
  124. /// instruction from the start of the function. This offset changes as stuff is
  125. /// moved around inside the function.
  126. unsigned BranchRelaxation::getInstrOffset(const MachineInstr &MI) const {
  127. const MachineBasicBlock *MBB = MI.getParent();
  128. // The offset is composed of two things: the sum of the sizes of all MBB's
  129. // before this instruction's block, and the offset from the start of the block
  130. // it is in.
  131. unsigned Offset = BlockInfo[MBB->getNumber()].Offset;
  132. // Sum instructions before MI in MBB.
  133. for (MachineBasicBlock::const_iterator I = MBB->begin(); &*I != &MI; ++I) {
  134. assert(I != MBB->end() && "Didn't find MI in its own basic block?");
  135. Offset += TII->getInstSizeInBytes(*I);
  136. }
  137. return Offset;
  138. }
  139. void BranchRelaxation::adjustBlockOffsets(MachineBasicBlock &Start) {
  140. unsigned PrevNum = Start.getNumber();
  141. for (auto &MBB : make_range(MachineFunction::iterator(Start), MF->end())) {
  142. unsigned Num = MBB.getNumber();
  143. if (!Num) // block zero is never changed from offset zero.
  144. continue;
  145. // Get the offset and known bits at the end of the layout predecessor.
  146. // Include the alignment of the current block.
  147. BlockInfo[Num].Offset = BlockInfo[PrevNum].postOffset(MBB);
  148. PrevNum = Num;
  149. }
  150. }
  151. /// Split the basic block containing MI into two blocks, which are joined by
  152. /// an unconditional branch. Update data structures and renumber blocks to
  153. /// account for this change and returns the newly created block.
  154. /// NOTE: Successor list of the original BB is out of date after this function,
  155. /// and must be updated by the caller! Other transforms follow using this
  156. /// utility function, so no point updating now rather than waiting.
  157. MachineBasicBlock *BranchRelaxation::splitBlockBeforeInstr(MachineInstr &MI) {
  158. MachineBasicBlock *OrigBB = MI.getParent();
  159. // Create a new MBB for the code after the OrigBB.
  160. MachineBasicBlock *NewBB =
  161. MF->CreateMachineBasicBlock(OrigBB->getBasicBlock());
  162. MF->insert(++OrigBB->getIterator(), NewBB);
  163. // Splice the instructions starting with MI over to NewBB.
  164. NewBB->splice(NewBB->end(), OrigBB, MI.getIterator(), OrigBB->end());
  165. // Add an unconditional branch from OrigBB to NewBB.
  166. // Note the new unconditional branch is not being recorded.
  167. // There doesn't seem to be meaningful DebugInfo available; this doesn't
  168. // correspond to anything in the source.
  169. TII->insertUnconditionalBranch(*OrigBB, NewBB, DebugLoc());
  170. // Insert an entry into BlockInfo to align it properly with the block numbers.
  171. BlockInfo.insert(BlockInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
  172. // Figure out how large the OrigBB is. As the first half of the original
  173. // block, it cannot contain a tablejump. The size includes
  174. // the new jump we added. (It should be possible to do this without
  175. // recounting everything, but it's very confusing, and this is rarely
  176. // executed.)
  177. BlockInfo[OrigBB->getNumber()].Size = computeBlockSize(*OrigBB);
  178. // Figure out how large the NewMBB is. As the second half of the original
  179. // block, it may contain a tablejump.
  180. BlockInfo[NewBB->getNumber()].Size = computeBlockSize(*NewBB);
  181. // All BBOffsets following these blocks must be modified.
  182. adjustBlockOffsets(*OrigBB);
  183. ++NumSplit;
  184. return NewBB;
  185. }
  186. /// isBlockInRange - Returns true if the distance between specific MI and
  187. /// specific BB can fit in MI's displacement field.
  188. bool BranchRelaxation::isBlockInRange(
  189. const MachineInstr &MI, const MachineBasicBlock &DestBB) const {
  190. int64_t BrOffset = getInstrOffset(MI);
  191. int64_t DestOffset = BlockInfo[DestBB.getNumber()].Offset;
  192. if (TII->isBranchOffsetInRange(MI.getOpcode(), DestOffset - BrOffset))
  193. return true;
  194. DEBUG(
  195. dbgs() << "Out of range branch to destination BB#" << DestBB.getNumber()
  196. << " from BB#" << MI.getParent()->getNumber()
  197. << " to " << DestOffset
  198. << " offset " << DestOffset - BrOffset
  199. << '\t' << MI
  200. );
  201. return false;
  202. }
  203. /// fixupConditionalBranch - Fix up a conditional branch whose destination is
  204. /// too far away to fit in its displacement field. It is converted to an inverse
  205. /// conditional branch + an unconditional branch to the destination.
  206. bool BranchRelaxation::fixupConditionalBranch(MachineInstr &MI) {
  207. DebugLoc DL = MI.getDebugLoc();
  208. MachineBasicBlock *MBB = MI.getParent();
  209. MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
  210. SmallVector<MachineOperand, 4> Cond;
  211. bool Fail = TII->analyzeBranch(*MBB, TBB, FBB, Cond);
  212. assert(!Fail && "branches to be relaxed must be analyzable");
  213. (void)Fail;
  214. // Add an unconditional branch to the destination and invert the branch
  215. // condition to jump over it:
  216. // tbz L1
  217. // =>
  218. // tbnz L2
  219. // b L1
  220. // L2:
  221. if (FBB && isBlockInRange(MI, *FBB)) {
  222. // Last MI in the BB is an unconditional branch. We can simply invert the
  223. // condition and swap destinations:
  224. // beq L1
  225. // b L2
  226. // =>
  227. // bne L2
  228. // b L1
  229. DEBUG(dbgs() << " Invert condition and swap "
  230. "its destination with " << MBB->back());
  231. TII->reverseBranchCondition(Cond);
  232. int OldSize = 0, NewSize = 0;
  233. TII->removeBranch(*MBB, &OldSize);
  234. TII->insertBranch(*MBB, FBB, TBB, Cond, DL, &NewSize);
  235. BlockInfo[MBB->getNumber()].Size += (NewSize - OldSize);
  236. return true;
  237. } else if (FBB) {
  238. // We need to split the basic block here to obtain two long-range
  239. // unconditional branches.
  240. auto &NewBB = *MF->CreateMachineBasicBlock(MBB->getBasicBlock());
  241. MF->insert(++MBB->getIterator(), &NewBB);
  242. // Insert an entry into BlockInfo to align it properly with the block
  243. // numbers.
  244. BlockInfo.insert(BlockInfo.begin() + NewBB.getNumber(), BasicBlockInfo());
  245. unsigned &NewBBSize = BlockInfo[NewBB.getNumber()].Size;
  246. int NewBrSize;
  247. TII->insertUnconditionalBranch(NewBB, FBB, DL, &NewBrSize);
  248. NewBBSize += NewBrSize;
  249. // Update the successor lists according to the transformation to follow.
  250. // Do it here since if there's no split, no update is needed.
  251. MBB->replaceSuccessor(FBB, &NewBB);
  252. NewBB.addSuccessor(FBB);
  253. }
  254. // We now have an appropriate fall-through block in place (either naturally or
  255. // just created), so we can invert the condition.
  256. MachineBasicBlock &NextBB = *std::next(MachineFunction::iterator(MBB));
  257. DEBUG(dbgs() << " Insert B to BB#" << TBB->getNumber()
  258. << ", invert condition and change dest. to BB#"
  259. << NextBB.getNumber() << '\n');
  260. unsigned &MBBSize = BlockInfo[MBB->getNumber()].Size;
  261. // Insert a new conditional branch and a new unconditional branch.
  262. int RemovedSize = 0;
  263. TII->reverseBranchCondition(Cond);
  264. TII->removeBranch(*MBB, &RemovedSize);
  265. MBBSize -= RemovedSize;
  266. int AddedSize = 0;
  267. TII->insertBranch(*MBB, &NextBB, TBB, Cond, DL, &AddedSize);
  268. MBBSize += AddedSize;
  269. // Finally, keep the block offsets up to date.
  270. adjustBlockOffsets(*MBB);
  271. return true;
  272. }
  273. bool BranchRelaxation::relaxBranchInstructions() {
  274. bool Changed = false;
  275. // Relaxing branches involves creating new basic blocks, so re-eval
  276. // end() for termination.
  277. for (MachineFunction::iterator I = MF->begin(); I != MF->end(); ++I) {
  278. MachineBasicBlock &MBB = *I;
  279. MachineBasicBlock::iterator J = MBB.getFirstTerminator();
  280. if (J == MBB.end())
  281. continue;
  282. MachineBasicBlock::iterator Next;
  283. for (MachineBasicBlock::iterator J = MBB.getFirstTerminator();
  284. J != MBB.end(); J = Next) {
  285. Next = std::next(J);
  286. MachineInstr &MI = *J;
  287. if (MI.isConditionalBranch()) {
  288. MachineBasicBlock *DestBB = TII->getBranchDestBlock(MI);
  289. if (!isBlockInRange(MI, *DestBB)) {
  290. if (Next != MBB.end() && Next->isConditionalBranch()) {
  291. // If there are multiple conditional branches, this isn't an
  292. // analyzable block. Split later terminators into a new block so
  293. // each one will be analyzable.
  294. MachineBasicBlock *NewBB = splitBlockBeforeInstr(*Next);
  295. NewBB->transferSuccessors(&MBB);
  296. MBB.addSuccessor(NewBB);
  297. MBB.addSuccessor(DestBB);
  298. // Cleanup potential unconditional branch to successor block.
  299. NewBB->updateTerminator();
  300. MBB.updateTerminator();
  301. } else {
  302. fixupConditionalBranch(MI);
  303. ++NumConditionalRelaxed;
  304. }
  305. Changed = true;
  306. // This may have modified all of the terminators, so start over.
  307. Next = MBB.getFirstTerminator();
  308. }
  309. }
  310. }
  311. }
  312. return Changed;
  313. }
  314. bool BranchRelaxation::runOnMachineFunction(MachineFunction &mf) {
  315. MF = &mf;
  316. DEBUG(dbgs() << "***** BranchRelaxation *****\n");
  317. TII = MF->getSubtarget().getInstrInfo();
  318. // Renumber all of the machine basic blocks in the function, guaranteeing that
  319. // the numbers agree with the position of the block in the function.
  320. MF->RenumberBlocks();
  321. // Do the initial scan of the function, building up information about the
  322. // sizes of each block.
  323. scanFunction();
  324. DEBUG(dbgs() << " Basic blocks before relaxation\n"; dumpBBs(););
  325. bool MadeChange = false;
  326. while (relaxBranchInstructions())
  327. MadeChange = true;
  328. // After a while, this might be made debug-only, but it is not expensive.
  329. verify();
  330. DEBUG(dbgs() << " Basic blocks after relaxation\n\n"; dumpBBs());
  331. BlockInfo.clear();
  332. return MadeChange;
  333. }