<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/56564>56564</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
clang x86 missed optimization combining `add` and `mov` to `lea`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
AMS21
</td>
</tr>
</table>
<pre>
Given the following source code [godbolt](https://godbolt.org/z/1f383Pefa)
```cpp
[[noreturn]] void f();
int g(int a) {
if (a == 0) {
f();
}
return a + 21;
}
```
compiling with `-O3` or `-Os` outputs
```asm
g(int): # @g(int)
test edi, edi
je .LBB0_2
add edi, 21
mov eax, edi
ret
.LBB0_2:
push rax
call f()@PLT
```
while gcc gives
```asm
g(int):
test edi, edi
je .L7
lea eax, [rdi+21]
ret
.L7:
push rax
call f()
```
llvm-mca gives for gcc
```
Iterations: 100
Instructions: 600
Total Cycles: 447
Total uOps: 700
Dispatch Width: 4
uOps Per Cycle: 1.57
IPC: 1.34
Block RThroughput: 1.8
```
while for clang it gives
```
Iterations: 100
Instructions: 700
Total Cycles: 734
Total uOps: 1400
Dispatch Width: 6
uOps Per Cycle: 1.91
IPC: 0.95
Block RThroughput: 2.3
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJydVU1vozAQ_TXkYhWB-T5wCI26qtRVq91Ke1wZ2wnuGoywSdr99TsG0kKaZqtaDs74zTwPD49dKvaSfxN73iBTcbRVUqqDaHZIq76jHFHFOHKiYqdYqaRxoo2D08qYVjvB2sE30CfIVd0OrL_w87dBGjzwLXFw5ngbx1s7sTd22rbTTFRAb1THTd81ljfaoL0SDG1hBRsYFJPn8BSNQcCf2tHyIieZcARNbBFgBDnBBjry3jnYdkps55xkM1_FTo0ZISDDBcL-Wx6vrseXmUdSVbdCWuUOwlQI0Kv7AJ5IdaOhB6M3bW_0CQ_R9TgzveCQ5GvaHzcHB8gJvVnU4n0N18aOnAkHXw_DAn_i4-jeFYX3Gy9BwhiaBYMOC7hW-xEmz2e5QcRx4kgerJcOba-rwREIFgAlUs6_Vug93D1eEP5QCcnRjlK0g238OWm_qlOyBCQncxFgP3eWogCxos2HeiRf1-JjFaTc11c1JaMIUMedleRswK3hHTFCNXq2yXzviDbadD1d4vERfVSGSHT9QiV_Q8MwmaP9fTtnTrxFohuhW2JohX4JZqrJLxwxG4keeDcuMGG-G030tw_X78vCd4MpupCK_kE_HqtO9bsKysw6-276371jxaKSQOkKc3YTfVm35KJuyTHzs7r54SeEiy8Kl_kXhPPcLLogHHaDExVWLA9YFmRkZYSRPB8le05jVAutOUOqNaIWfweN4OKoS9HY89BWIWP29CMNsxacHdYyyhpQQ5a772R-cq3AMdqXLvCAYbf3NFy1nXriFCr5BpbtQU98E8VRHK6qnEUp9mkaJ5h6WUY5o6UXJnEaZEEalXGwkqTkUuf28sG44Qc0UMB_KNiVyLGHsZf4se9HYRS42yzy0yxOSz_ZYs62cBbxmgjp2jzsfbfq8iGlst9pAKXQRr-BRGuxazgflgN-0ptKdfn6-0_sr4aF8yHxf6My9X0">