[ARM] [Clang] Missed optimization for bool return val arithmetic operations
- 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
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