Skip to content

Commit ef232a7

Browse files
authored
[BOLT][AArch64] Remove nops in functions with defined control flow (#124705)
When a function has an indirect branch with unknown control flow, we preserve nops in order to keep all instruction offsets (from the start of the function) the same in case the indirect branch is used by a PC-relative jump table. However, when we know the control flow of the function, we should be able to safely remove nops.
1 parent 1d5ce61 commit ef232a7

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,6 @@ BinaryFunction::processIndirectBranch(MCInst &Instruction, unsigned Size,
795795

796796
auto Begin = Instructions.begin();
797797
if (BC.isAArch64()) {
798-
PreserveNops = BC.HasRelocations;
799798
// Start at the last label as an approximation of the current basic block.
800799
// This is a heuristic, since the full set of labels have yet to be
801800
// determined
@@ -2300,6 +2299,10 @@ Error BinaryFunction::buildCFG(MCPlusBuilder::AllocatorIdTy AllocatorId) {
23002299
BC.errs() << "BOLT-WARNING: failed to post-process indirect branches for "
23012300
<< *this << '\n';
23022301
}
2302+
2303+
if (BC.isAArch64())
2304+
PreserveNops = BC.HasRelocations;
2305+
23032306
// In relocation mode we want to keep processing the function but avoid
23042307
// optimizing it.
23052308
setSimple(false);

bolt/test/AArch64/remove-nops.s

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Verify that llvm-bolt removes nop instructions from functions with indirect
2+
## branches that have defined control flow.
3+
4+
# REQUIRES: system-linux
5+
6+
# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %s -o %t.o
7+
# RUN: %clang %cflags --target=aarch64-unknown-linux %t.o -o %t.exe -Wl,-q
8+
# RUN: llvm-bolt %t.exe -o %t.bolt --print-normalized 2>&1 | FileCheck %s
9+
# RUN: llvm-objdump -d --disassemble-symbols=_start %t.bolt \
10+
# RUN: | FileCheck %s --check-prefix=CHECK-OBJDUMP
11+
12+
# CHECK-OBJDUMP-LABEL: _start
13+
# CHECK-OBJDUMP-NOT: nop
14+
15+
.section .text
16+
.align 4
17+
.globl _start
18+
.type _start, %function
19+
_start:
20+
# CHECK-LABEL: Binary Function "_start"
21+
nop
22+
# CHECK-NOT: nop
23+
br x0
24+
# CHECK: br x0 # TAILCALL
25+
.size _start, .-_start
26+
27+
## Force relocation mode.
28+
.reloc 0, R_AARCH64_NONE

0 commit comments

Comments
 (0)