[openmp] [lld] [lldb] [clang-tools-extra] [compiler-rt] [libunwind] [llvm] [flang] [mlir] [clang] [BranchFolding] Fix missing predecessors of landing-pad (PR #77608)

via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 10 18:15:31 PST 2024


================
@@ -1624,6 +1632,15 @@ bool BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
             } else {
               DidChange = true;
               PMBB->ReplaceUsesOfBlockWith(MBB, CurTBB);
+              // Add rest successors of MBB to successors of CurTBB. Those
+              // successors are not directly reachable via MBB, so it should be
+              // landing-pad.
+              for (auto SI = MBB->succ_begin(), SE = MBB->succ_end(); SI != SE;
+                   ++SI)
+                if (*SI != CurTBB && !CurTBB->isSuccessor(*SI)) {
+                  assert((*SI)->isEHPad() && "Bad CFG");
+                  CurTBB->copySuccessor(MBB, SI);
----------------
HaohaiWen wrote:

Before this code:
```
bb.0:
  successors: %bb.6(0x7ffff800), %bb.5(0x00000800); %bb.6(100.00%), %bb.5(0.00%)

  JMP_1 %bb.6

bb.3:
  successors: %bb.6(0x80000000), %bb.4(0x00000800); %bb.6(100.00%), %bb.4(0.00%)


bb.4 (machine-block-address-taken, landing-pad, ehfunclet-entry):
; predecessors: %bb.3
  successors: %bb.5(0x80000000); %bb.5(100.00%)

  CLEANUPRET

bb.5 (landing-pad, ehfunclet-entry):
; predecessors: %bb.0, %bb.4

  CLEANUPRET

bb.6:
; predecessors: %bb.3, %bb.0

  RET 0
```
After:
```
bb.0:
  successors: %bb.6(0x7ffff800), %bb.5(0x00000800); %bb.6(100.00%), %bb.5(0.00%)

  JMP_1 %bb.6

bb.3:
  successors: %bb.6(0x80000000), %bb.4(0x00000800); %bb.6(100.00%), %bb.4(0.00%)


bb.4 (machine-block-address-taken, landing-pad, ehfunclet-entry):
; predecessors: %bb.3, %bb.6
  successors: %bb.5(0x80000000); %bb.5(100.00%)

  CLEANUPRET

bb.5 (landing-pad, ehfunclet-entry):
; predecessors: %bb.0, %bb.4

  CLEANUPRET

bb.6:
; predecessors: %bb.3, %bb.0
  successors: %bb.4(0x00000800); %bb.4(0.00%)

  RET 0
```


https://github.com/llvm/llvm-project/pull/77608


More information about the cfe-commits mailing list