diff options
author | Rahman Lavaee <rahmanl@google.com> | 2023-08-21 19:55:24 +0000 |
---|---|---|
committer | Rahman Lavaee <rahmanl@google.com> | 2023-08-22 23:32:02 +0000 |
commit | ab53109166c0345a79cbd6939cf7bc764a982856 (patch) | |
tree | f4814b12b09c7072efb5c8c6f211cef12891ad75 /llvm/lib/CodeGen/BasicBlockSections.cpp | |
parent | ec5e947f97aeb3d30316a33313f8661e78318146 (diff) | |
download | llvm-ab53109166c0345a79cbd6939cf7bc764a982856.zip llvm-ab53109166c0345a79cbd6939cf7bc764a982856.tar.gz llvm-ab53109166c0345a79cbd6939cf7bc764a982856.tar.bz2 |
[BasicBlockSections] avoid insertting redundant branch to fall through blocks
Diffstat (limited to 'llvm/lib/CodeGen/BasicBlockSections.cpp')
-rw-r--r-- | llvm/lib/CodeGen/BasicBlockSections.cpp | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/llvm/lib/CodeGen/BasicBlockSections.cpp b/llvm/lib/CodeGen/BasicBlockSections.cpp index 33e70b1..4ebff65 100644 --- a/llvm/lib/CodeGen/BasicBlockSections.cpp +++ b/llvm/lib/CodeGen/BasicBlockSections.cpp @@ -134,6 +134,14 @@ INITIALIZE_PASS_END(BasicBlockSections, "bbsections-prepare", "into clusters of basic blocks.", false, false) +// Returns whether the given basic block has an unconditional branch. +bool hasUnconditionalBranch(const MachineBasicBlock &MBB) { + if (MBB.terminators().empty()) + return false; + const MachineInstr &Terminator = *(--MBB.terminators().end()); + return Terminator.isUnconditionalBranch(); +} + // This function updates and optimizes the branching instructions of every basic // block in a given function to account for changes in the layout. static void @@ -145,12 +153,14 @@ updateBranches(MachineFunction &MF, auto NextMBBI = std::next(MBB.getIterator()); auto *FTMBB = PreLayoutFallThroughs[MBB.getNumber()]; // If this block had a fallthrough before we need an explicit unconditional - // branch to that block if either + // branch to that block if either one of these two conditions hold and the + // block doesn't currently have an unconditional branch. // 1- the block ends a section, which means its next block may be // reorderd by the linker, or // 2- the fallthrough block is not adjacent to the block in the new // order. - if (FTMBB && (MBB.isEndSection() || &*NextMBBI != FTMBB)) + if (FTMBB && (MBB.isEndSection() || &*NextMBBI != FTMBB) && + !hasUnconditionalBranch(MBB)) TII->insertUnconditionalBranch(MBB, FTMBB, MBB.findBranchDebugLoc()); // We do not optimize branches for machine basic blocks ending sections, as |