<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/112063>112063</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missed optimization: Imperfect optimization of straightforward std::find
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Alcaro
</td>
</tr>
</table>
<pre>
```c++
static bool any_of_impl(int* first, int* last, int num)
{
for (; first != last; ++first) {
if (*first == num)
return true;
}
return false;
}
bool square(int num) {
int x[] = {1,2,3};
return any_of_impl(x, x+3, num);
}
bool cube(int num) {
return num==1 || num==2 || num==3;
}
```
-O2 or -O3
Expected: Same asm for both
Actual:
```
square(int):
cmp edi, 1
sete cl
add edi, -2
test edi, -2
sete al
or al, cl
ret
cube(int):
dec edi
cmp edi, 3
setb al
ret
```
The expected result shows up on pretty much any simplification of the input, including moving the +3 to the callee, or changing both functions' return type to int. Feels like some optimizer passes are running in wrong order and confusing each other.
https://godbolt.org/z/88938fP3d
Originally found via a std::ranges::any_of call, hence the function name. It turned into std::find when I reduced it. https://godbolt.org/z/GjsE3vEK4
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyEVctu6zYQ_Rp6M4ghk_JroYVjx0VQFCnQ7i8ocijxliJVPuz4fn1BWbHlm7Q1BIjkPM6cM0OLh6Abi1iR5TNZHmY8xdb5amcE925WO3mpyKq4PoLQ5_wUB1LsQuRRC6idM8Dt5ZtT33TXG0I32kZCd6C0D5HQPYx7w29bsKkjdHtNRNZjRgAA5TwQuiHs-RoPhC4IO1yD2TNcKxhTb-Eeq9UQR3djGDvksCkOTH4eY_IWok9I2ASerA_3zeikuAl3r5vHQDz8nbjHK-cRDB745PP3q7S5pmxcELqnhO5ZTjUFH_EexXzPkr0T-szyYuTzqZhJSSLV_1XQCJJNg0QLIOs9We_vJ_TTCfsC8GMmpvhPbxSch6c3Nj19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