<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/74577>74577</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missed optimization: duplicate address calculations
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
dvyukov
</td>
</tr>
</table>
<pre>
The code is:
```
struct X {
short a;
short b;
short c;
short d;
};
void foo(X* xx, long i, long j) {
X* x = &xx[i];
long q = (j & ~0xffff) | x->c;
long w = (j & ~0xffff) | x->d;
asm(
"movq %[q], %[x]; movq %[w], %[x];"
:: [q]"r"(q), [w]"r"(w), [x]"r"(x));
}
```
Clang generates:
```
foo(X*, long, long): # @foo(X*, long, long)
leaq (%rdi,%rsi,8), %rax
andq $-65536, %rdx # imm = 0xFFFF0000
movswq 4(%rdi,%rsi,8), %rcx
orq %rdx, %rcx
movswq 6(%rdi,%rsi,8), %rsi
orq %rdx, %rsi
movq %rcx, %rax
movq %rsi, %rax
retq
```
Since we already have `(%rdi,%rsi,8)` in `%rax`, `%rax` can be reused in subsequent instructions instead of using the complex addressing mode. I would expect clang to generate:
```
leaq (%rdi,%rsi,4), %rax
movswq 4(%rax), %rcx
movswq 6(%rax), %rsi
```
In #71306 @topperc mentioned that LEA with 3 arguments may be quite expensive on some CPUs. I would assume the same applies to any instruction with 3-argument address.
Simpler addressing mode may also result in smaller instructions.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVV1v6jgT_jXOzajIOB8kF7lo4SAd6X2llXZXOrdOPAF3nTixHUj3Yn_7yoYUaAtdhBJ75pkPz_NguLVy1yGWJH0h6Sbio9trU4rD2_iXPkSVFm_lH3uEWgsEaUn8TOiG0GeS0fM3bK0zY-3gF5DVy8kCYPfaOOAk_mCpPlnqTxbxbiGrzWUdngctBTRaE5b_IuwZpomwNSjd7UC-r14JK66bAThhgcQbICyb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