llvm / llvm/llvm-project

Unnecessary `mov eax, eax` generated only if there is a conditional branch.

Open
#167,795 1 comment 1 reaction 0 assignees View on GitHub
llvm:optimizations missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

https://godbolt.org/z/48scbhEKo

Code
```c++
int eax();

unsigned long long x(int in) {
in |= eax();

if (in < 0) {
while (true) {}
}

return (unsigned long long)(unsigned)in;
}

unsigned long long y(int in) {
in |= eax();

return (unsigned long long)(unsigned)in;
}
```
Assembly
```asm
x(int):
push rbx
mov ebx, edi
call eax()@PLT
or eax, ebx
js .LBB0_1
mov eax, eax ; <---
pop rbx
ret
.LBB0_1:
jmp .LBB0_1

y(int):
push rbx
mov ebx, edi
call eax()@PLT
or eax, ebx
pop rbx
ret```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the example at https://godbolt.org/z/48scbhEKo and compare the generated assembly for x and y. Trace the LLVM code-generation path responsible for the conditional branch and integer conversion; done means the unnecessary mov eax, eax is no longer emitted while the branch behavior remains correct.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.