<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/60761>60761</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Comparison in loop generates slightly worse code than GCC
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
firewave
</td>
</tr>
</table>
<pre>
Apologies in advance when I am on the wrong track here. I still (and probably always will) have issues with understanding byte code properly.
```cpp
int multiCompare1(const char *tok, const char *haystack)
{
const char *needle = tok;
const char *needlePointer = needle;
for (;;) {
if (*haystack == ' ' || *haystack == '\0') {
if (needlePointer == needle)
return 0;
break;
}
else {
do {
++haystack;
if (*haystack == ' ' || *haystack == '\0') { // <---
return -1;
}
if (*haystack == '|') {
break;
}
} while (true);
++haystack;
}
}
return -1;
}
```
If I replace the marked `if` with a `switch` it will generate one less branch. If I pull out the code and try to get a more reduced case it will generate the same code for both cases. If you remove the `if` after that check it will also generate the "expected" code.
Using the `switch` will match the code GCC generates for the `if` case. But with `switch` GCC will generate different code.
https://godbolt.org/z/9bbdfWejh
If I reduce the code too much GCC is also able to recognize that the loop won't do anything: https://godbolt.org/z/YMEz46hdE. That's probably a different ticket though.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVUFv6zYM_jXKhUjgyEmcHHxo0pehhwE7bBh2lCXa0ossGRLdLP31g-y2iV_Tvh2e0dY1TX78-NEiRYymcYglW-_Z-nEmetI-lLUJeBbPOKu8upQPnbe-MRjBOBDqWTiJcNbo4AlEC94BaYRz8K4BCkKeQGPABTxBJGMtML4VTkEXfCUqewFhz-IS4WysZXwHWjwjmBh7TDbS0DuFIZJwyrgGqgshSK8wAXQY7GXBskeWPbz-3WTjj-y60WIcQdtbMgffdiLgkvGt9C4SSC0CMP5A_sT4AaZGLS6RhDwxvntFLvbjPwDwg69DVBaB5Y-QsPKfOP7hjSMMg_9om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