[llvm] Do not remove empty basic blocks which have address taken. (PR #67740)

via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 28 14:22:30 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-x86

<details>
<summary>Changes</summary>

This PR replaces `isMachineBlockAddressTaken` by `hasAddressTaken` to include blocks which have their IR address taken as well. These blocks are also not removable since their predecessors' terminators do not directly point to the block.

---
Full diff: https://github.com/llvm/llvm-project/pull/67740.diff


2 Files Affected:

- (modified) llvm/lib/CodeGen/GCEmptyBasicBlocks.cpp (+1-1) 
- (modified) llvm/test/CodeGen/X86/gc-empty-basic-blocks.ll (+29-5) 


``````````diff
diff --git a/llvm/lib/CodeGen/GCEmptyBasicBlocks.cpp b/llvm/lib/CodeGen/GCEmptyBasicBlocks.cpp
index e261531eb014052..598be26e40c8e9a 100644
--- a/llvm/lib/CodeGen/GCEmptyBasicBlocks.cpp
+++ b/llvm/lib/CodeGen/GCEmptyBasicBlocks.cpp
@@ -58,7 +58,7 @@ bool GCEmptyBasicBlocks::runOnMachineFunction(MachineFunction &MF) {
     // TODO If a block is an eh pad, or it has address taken, we don't remove
     // it. Removing such blocks is possible, but it probably requires a more
     // complex logic.
-    if (MBB->isEHPad() || MBB->isMachineBlockAddressTaken())
+    if (MBB->isEHPad() || MBB->hasAddressTaken())
       continue;
     // Skip blocks with real code.
     bool HasAnyRealCode = llvm::any_of(*MBB, [](const MachineInstr &MI) {
diff --git a/llvm/test/CodeGen/X86/gc-empty-basic-blocks.ll b/llvm/test/CodeGen/X86/gc-empty-basic-blocks.ll
index f737e43d3b816af..bacdad07a14a51e 100644
--- a/llvm/test/CodeGen/X86/gc-empty-basic-blocks.ll
+++ b/llvm/test/CodeGen/X86/gc-empty-basic-blocks.ll
@@ -1,6 +1,8 @@
-;; This test verifies that -gc-empty-basic-blocks removes empty blocks.
+;; This test verifies that -gc-empty-basic-blocks removes regular empty blocks
+;; but does not remove empty blocks which have their address taken.
 ; RUN: llc < %s -mtriple=x86_64 -O0 -gc-empty-basic-blocks | FileCheck %s
 
+;; This function has a regular empty block.
 define void @foo(i1 zeroext %0) nounwind {
   br i1 %0, label %2, label %empty_block
 
@@ -10,7 +12,7 @@ define void @foo(i1 zeroext %0) nounwind {
 ; CHECK-NEXT:    jmp .LBB0_3
 
 2:                                               ; preds = %1
-  %3 = call i32 @bar()
+  %3 = call i32 @baz()
   br label %4
 
 ; CHECK-LABEL: .LBB0_1:
@@ -19,8 +21,8 @@ define void @foo(i1 zeroext %0) nounwind {
 empty_block:                                     ; preds = %1
   unreachable
 
-; CHECK-NOT: %empty_block
-; CHECK-NOT: .LBB0_2
+; CHECK-NOT:     %empty_block
+; CHECK-NOT:   .LBB0_2
 
 4:                                               ; preds = %2, %empty_block
   ret void
@@ -30,4 +32,26 @@ empty_block:                                     ; preds = %1
 
 }
 
-declare i32 @bar()
+;; This function has an empty block which has its address taken.
+define void @bar(i1 zeroext %0) nounwind {
+entry:
+  %1 = select i1 %0, ptr blockaddress(@bar, %empty_block), ptr blockaddress(@bar, %bb2) ; <ptr> [#uses=1]
+  indirectbr ptr %1, [label %empty_block, label %bb2]
+
+; CHECK-LABEL: bar:
+; CHECK-LABEL: %bb.0:
+; CHECK:         jmpq *%rax
+
+empty_block:                                                ; preds = %entry
+  unreachable
+
+; CHECK-LABEL: .LBB1_1: # %empty_block
+
+bb2:                                                ; preds = %entry
+  %2 = call i32 @baz()
+  ret void
+; CHECK-LABEL: .LBB1_2:
+; CHECK-NEXT:    callq   baz
+}
+
+declare i32 @baz()

``````````

</details>


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


More information about the llvm-commits mailing list