[PATCH] D136237: [BasicBlockSections] avoid insertting redundant branch to fall through blocks

Sinan Lin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 19 05:56:37 PDT 2022


sinan updated this revision to Diff 468887.
sinan added a comment.

simplify code


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D136237/new/

https://reviews.llvm.org/D136237

Files:
  llvm/lib/CodeGen/BasicBlockSections.cpp
  llvm/test/CodeGen/X86/basic-block-sections_2.ll


Index: llvm/test/CodeGen/X86/basic-block-sections_2.ll
===================================================================
--- llvm/test/CodeGen/X86/basic-block-sections_2.ll
+++ llvm/test/CodeGen/X86/basic-block-sections_2.ll
@@ -12,11 +12,18 @@
 ;;
 ; __cxx_global_var_init has multiple basic blocks which will produce many sections.
 ; RUN: llc < %s -mtriple=x86_64-pc-linux -function-sections -basic-block-sections=all -unique-basic-block-section-names | FileCheck %s
+; RUN: llc < %s -mtriple=x86_64-pc-linux -function-sections -basic-block-sections=all -O0 -unique-basic-block-section-names | FileCheck %s --check-prefix=REDUNDANT
 
 ; CHECK-LABEL: __cxx_global_var_init:
 ; CHECK-LABEL: __cxx_global_var_init.__part.1:
 ; CHECK-LABEL: __cxx_global_var_init.1:
 
+; REDUNDANT-LABEL: __cxx_global_var_init:
+; REDUNDANT: jmp __cxx_global_var_init.__part.2
+; REDUNDANT-NOT: jmp __cxx_global_var_init.__part.1
+; REDUNDANT-LABEL: __cxx_global_var_init.__part.1:
+; REDUNDANT-LABEL: __cxx_global_var_init.1:
+
 %class.A = type { i8 }
 
 $_ZN1AC2Eb = comdat any
Index: llvm/lib/CodeGen/BasicBlockSections.cpp
===================================================================
--- llvm/lib/CodeGen/BasicBlockSections.cpp
+++ llvm/lib/CodeGen/BasicBlockSections.cpp
@@ -128,6 +128,19 @@
                 "into clusters of basic blocks.",
                 false, false)
 
+/// Basic blocks with an explicit unconditional branch to its fallthrough block
+/// do not need an extra unconditional branch.
+static bool needInsertUncondBranch(const MachineBasicBlock &MBB) {
+  auto I = MBB.terminators().begin(), E = MBB.end();
+
+  while (I != E) {
+    if (I->isUnconditionalBranch())
+      return false;
+    I++;
+  }
+  return true;
+}
+
 // 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 updateBranches(
@@ -141,10 +154,13 @@
     // If this block had a fallthrough before we need an explicit unconditional
     // branch to that block if either
     //     1- the block ends a section, which means its next block may be
-    //        reorderd by the linker, or
+    //        reorderd by the linker,
     //     2- the fallthrough block is not adjacent to the block in the new
-    //        order.
-    if (FTMBB && (MBB.isEndSection() || &*NextMBBI != FTMBB))
+    //        order, or
+    //     3- the terminator of the block is an unconditional branch to its
+    //        fallthrough.
+    if (FTMBB && (MBB.isEndSection() || &*NextMBBI != FTMBB) &&
+        needInsertUncondBranch(MBB))
       TII->insertUnconditionalBranch(MBB, FTMBB, MBB.findBranchDebugLoc());
 
     // We do not optimize branches for machine basic blocks ending sections, as


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136237.468887.patch
Type: text/x-patch
Size: 2771 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221019/c359715a/attachment.bin>


More information about the llvm-commits mailing list