<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/149413>149413</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[x86]: Failure to use LEA for base + index + displacement
</td>
</tr>
<tr>
<th>Labels</th>
<td>
backend:X86,
missed-optimization
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Kmeakin
</td>
</tr>
</table>
<pre>
https://godbolt.org/z/v89xz7bP8
```c++
#include <stdint.h>
using u64 = uint64_t;
u64 base_plus_index(u64 base, u64 index) { return base + index; }
u64 base_plus_displacement(u64 base) {
return base + 1;
}
u64 base_plus_index_plus_displacement(u64 base, u64 index) {
return base + index + 1;
}
u64 base_plus_index_times_scale_plus_displacement(u64 base, u64 index) {
return base + index * 2 + 1;
}
```
GCC produces
```asm
"base_plus_index(unsigned long, unsigned long)":
lea rax, [rdi+rsi]
ret
"base_plus_displacement(unsigned long)":
lea rax, [rdi+1]
ret
"base_plus_index_plus_displacement(unsigned long, unsigned long)":
lea rax, [rdi+1+rsi]
ret
"base_plus_index_times_scale_plus_displacement(unsigned long, unsigned long)":
lea rax, [rdi+1+rsi*2]
ret
```
but LLVM produces
```asm
base_plus_index(unsigned long, unsigned long):
lea rax, [rdi + rsi]
ret
base_plus_displacement(unsigned long):
lea rax, [rdi + 1]
ret
base_plus_index_plus_displacement(unsigned long, unsigned long):
lea rax, [rdi + rsi]
inc rax
ret
base_plus_index_times_scale_plus_displacement(unsigned long, unsigned long):
lea rax, [rdi + 2*rsi]
inc rax
ret
```
For `base_plus_index_plus_displacement` and `base_plus_index_times_scale_plus_displacement`, the `inc` could be folded into the `lea`
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJykVk1v6ygU_TXXG9TIxp8svLCTehbTkWY1ml2EDUl4xRAZqPL6659w3PeaxomrNkJKxIV7zr3noBtqjNgrzktIa0g3AXX2oIfy757TZ6GCVrOf5cHao4G4AtwAbvaatVralR72gJtXwM1LQU6veftvAWHlVxaeVwe49iusAMdCddIxjiBeG8uEsqsDxI_nG84ItUcuSxDEG-SEslmytRDXUzhLUEsN3x6lM1uhGD8BLt52Aa_Hq9M-QZDXaODWDWqMI8D1FIxrBPlmLikT5ihpx3uu7EXuMR2E1VVGFE385hOOgAu5r3l7IITm2Y-_PgNqRc_N1nRULhU3T-AWeoXwDIc3sc98_lqv0XHQzHXcvI9S0482wDM6qtGBDEmt9iOnyw0CGHvzhRWSnKKxP_TkD0JaD0wArgcjIN1MzZsa-BHuYxNugkyfW1jRhDQDcUfz79cYXdR5E31R_M8zWWzEmRCu8GzzL43ROouenv775447vmKNK6YXLEe73jLHe8Alc9yRZnoSF7Jc1fJFKZZgr0oTqvt99B6d77tk2SIjQwy4-iTLS780ekCQhct9zEJEFZs7e7_ILPRU7YH7q0J1PlGnnWSo5WinJeMMCWX12xHJKWRhwMqYkZjQgJdRnuK8KPI8Dw5lGBVFG7V5EodJl9KOsyLLSJYwRhKSkSgQJQ5xGuZRjqMki_IVJ2S3awlPW0KKIk4hCXlPhVxJ-dL76RoIYxwvo4QkURxI2nJpxjHtX333zBWDuPq_yPyLxWvAuBfGcPagj1b04pVaoZWPpZtgKH3Sh9btDSShFMaaPzBWWDnO_1OReaHiCjVUSDdwZDVyhqOnxwrt9DA3kN73NHCD_PhnQdiDa1ed7gE3HnH6ejgO-gfvLOBmrNIAbqZCX0r8KwAA__8DbqEg">