Skip to content

Commit bb8e7eb

Browse files
committed
[BOLT] Remove unreachable uncond branch after return
This patch fixes the removal of unreachable uncondtional branch located after return instruction. Vladislav Khmelevsky, Advanced Software Technology Lab, Huawei Reviewed By: Amir Differential Revision: https://reviews.llvm.org/D117677
1 parent e67430c commit bb8e7eb

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2031,7 +2031,8 @@ bool BinaryFunction::buildCFG(MCPlusBuilder::AllocatorIdTy AllocatorId) {
20312031
assert(PrevInstr && "no previous instruction for a fall through");
20322032
if (MIB->isUnconditionalBranch(Instr) &&
20332033
!MIB->isUnconditionalBranch(*PrevInstr) &&
2034-
!MIB->getConditionalTailCall(*PrevInstr)) {
2034+
!MIB->getConditionalTailCall(*PrevInstr) &&
2035+
!MIB->isReturn(*PrevInstr)) {
20352036
// Temporarily restore inserter basic block.
20362037
InsertBB = PrevBB;
20372038
} else {

bolt/test/AArch64/jmp-after-ret.s

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## This test checks that the unreachable unconditional branch is removed
2+
## if it is located after return instruction.
3+
4+
# REQUIRES: system-linux
5+
6+
# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown \
7+
# RUN: %s -o %t.o
8+
# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q
9+
# RUN: llvm-bolt %t.exe -o %t.bolt | FileCheck %s
10+
11+
# CHECK: UCE removed 1 blocks
12+
13+
.text
14+
.align 4
15+
.global main
16+
.type main, %function
17+
main:
18+
b.eq 1f
19+
ret
20+
b main
21+
1:
22+
mov x1, #1
23+
ret
24+
.size main, .-main

bolt/test/X86/jmp-after-ret.s

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## This test checks that the unreachable unconditional branch is removed
2+
## if it is located after return instruction.
3+
4+
# REQUIRES: system-linux
5+
6+
# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-unknown \
7+
# RUN: %s -o %t.o
8+
# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q
9+
# RUN: llvm-bolt %t.exe -o %t.bolt | FileCheck %s
10+
11+
# CHECK: UCE removed 1 blocks
12+
13+
.text
14+
.globl main
15+
.type main, %function
16+
.size main, .Lend-main
17+
main:
18+
je 1f
19+
retq
20+
jmp main
21+
1:
22+
movl $0x2, %ebx
23+
retq
24+
.Lend:

0 commit comments

Comments
 (0)