llvm / llvm/llvm-project

[ARM] [Clang] Missed optimization for bool return val arithmetic operations

Open
#162,599 5 comments 0 reactions 0 assignees View on GitHub
backend:ARM llvm:codegen missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

```c
bool xxxx();
int kkk()
{
return xxxx() + 1;
}
```
https://godbolt.org/z/YoM1PYE1n

llvm:
```
kkk:
push {r11, lr}
bl xxxx
mov r1, #1
cmp r0, #0
movwne r1, #2
mov r0, r1
pop {r11, pc}
```
gcc:
```
kkk:
push {r4, lr}
bl xxxx
add r0, r0, #1
pop {r4, pc}
```

According to ARM's ABI (I'm not sure if there is documentation to support this, but both GCC and LLVM compilers behave this way), in the aforementioned use case, the return value of the bool type is stored in the r0 register, and the value of r0 is 0 (false) or 1 (true). This can be observed from the following use case: https://godbolt.org/z/Wj1Goq1Y4

However, the LLVM compiler seems to have missed some optimizations when performing arithmetic operations on return values.
No similar issues have been identified on other architectures at this time.

Contributor guide

Open the contributing guide

Research direction

Start with the C reproducer and the linked Godbolt compilations, then inspect the ARM Clang code path responsible for lowering arithmetic on boolean return values. Compare the generated ARM assembly with the shown GCC output; done means the redundant boolean normalization is avoided while preserving the stated ARM ABI behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
compilers, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.