<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/72100>72100</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missed optimization: 2 field loads instead of 1
</td>
</tr>
<tr>
<th>Labels</th>
<td>
llvm:optimizations,
missed-optimization
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
dvyukov
</td>
</tr>
</table>
<pre>
The code is:
```
struct alignas(8) Y {
int c;
int d;
};
bool bar(Y* y) {
return (y->c | y->d) == 0;
}
```
clang generates:
```
bar(Y*):
movl 4(%rdi), %eax
orl (%rdi), %eax
sete %al
retq
```
Unless I am missing something this can be a single `cmpq` with memory.
The real world use case is a container empty() method that needs to check 2 int fields for 0.
I added `alignas(8)` just to ensure that the alignment is somehow in the way. Probably should be cmpq even w/o alignment at least on x86.
My real code actually contains atomic variables, I would assume cmpq can be done for the following as well:
```
bool foo(X* x) {
return (x->a.load(std::memory_order_relaxed) |
x->b.load(std::memory_order_relaxed)) == 0;
}
```
Also the following should probably be compiled to a single cmpq:
```
bool baz(Y* y) {
return y->c == 0 && y->d == 0;
}
```
Or, are there any concerns with the second accesses validity? These are both fields of the same base object, so I would assume if the first valid, the second is valid as well.
This last snippet is probably more frequent in real life.
https://godbolt.org/z/Kv6rMcehd
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVV1v4jgU_TXm5aooOBDggQemLFK1qnYfZqWdp9GNfUM849jU14Gmv35lh35QbWerRSix5ft9znGQ2Rwc0UYsvojFboJ9bH3Y6NPQ__SnSe31sPnaEiivCQyLciuKnSi2oiou_7zlGHoVAa05OGQhVysh1_ANxPLLaABgXAQlyqu9ftmL5e51nZ-19xZqDEKuvgm5hSFFfBMPAsU-OBByNdyI8jcFYnkLeamzabkT5Q6KqxT_Vvv4VBbdAQ7kKGCkDxt9LUjI9YsRXH6dP1mYC7kSchG0SSbyFoRcED5eW_pg8_s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