[PATCH] D117677: [BOLT] Remove unreachable uncond branch after return

Vladislav Khmelevsky via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 19 06:50:56 PST 2022


yota9 created this revision.
yota9 added reviewers: maksfb, rafaelauler, Amir.
Herald added a reviewer: rafauler.
Herald added a subscriber: ayermolo.
yota9 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This patch fixes the removal of unreachable uncondtional branch located
after return instruction.

Vladislav Khmelevsky,
Advanced Software Technology Lab, Huawei


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D117677

Files:
  bolt/lib/Core/BinaryFunction.cpp
  bolt/test/AArch64/jmp-after-ret.s
  bolt/test/X86/jmp-after-ret.s


Index: bolt/test/X86/jmp-after-ret.s
===================================================================
--- /dev/null
+++ bolt/test/X86/jmp-after-ret.s
@@ -0,0 +1,24 @@
+## This test checks that the unreachable unconditional branch is removed
+## if it is located after return instruction.
+
+# REQUIRES: system-linux
+
+# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-unknown \
+# RUN:   %s -o %t.o
+# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q
+# RUN: llvm-bolt %t.exe -o %t.bolt | FileCheck %s
+
+# CHECK: UCE removed 1 blocks
+
+    .text
+    .globl main
+    .type main, %function
+    .size main, .Lend-main
+main:
+    je 1f
+    retq
+    jmp main
+1:
+    movl $0x2, %ebx
+    retq
+.Lend:
Index: bolt/test/AArch64/jmp-after-ret.s
===================================================================
--- /dev/null
+++ bolt/test/AArch64/jmp-after-ret.s
@@ -0,0 +1,24 @@
+## This test checks that the unreachable unconditional branch is removed
+## if it is located after return instruction.
+
+# REQUIRES: system-linux
+
+# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown \
+# RUN:   %s -o %t.o
+# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q
+# RUN: llvm-bolt %t.exe -o %t.bolt | FileCheck %s
+
+# CHECK: UCE removed 1 blocks
+
+  .text
+  .align 4
+  .global main
+  .type main, %function
+main:
+  b.eq 1f
+  ret
+  b main
+1:
+  mov x1, #1
+  ret
+  .size main, .-main
Index: bolt/lib/Core/BinaryFunction.cpp
===================================================================
--- bolt/lib/Core/BinaryFunction.cpp
+++ bolt/lib/Core/BinaryFunction.cpp
@@ -2033,7 +2033,8 @@
       assert(PrevInstr && "no previous instruction for a fall through");
       if (MIB->isUnconditionalBranch(Instr) &&
           !MIB->isUnconditionalBranch(*PrevInstr) &&
-          !MIB->getConditionalTailCall(*PrevInstr)) {
+          !MIB->getConditionalTailCall(*PrevInstr) &&
+          !MIB->isReturn(*PrevInstr)) {
         // Temporarily restore inserter basic block.
         InsertBB = PrevBB;
       } else {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117677.401222.patch
Type: text/x-patch
Size: 2022 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220119/ce3ae964/attachment.bin>


More information about the llvm-commits mailing list