<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/58572>58572</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missed optimization with throw vs return conditions
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
tczajka
</td>
</tr>
</table>
<pre>
With this code, the compiler [is smart enough](https://godbolt.org/z/W7azWvqc5) to figure out that `a - 1 < b` is always true:
```cpp
unsigned foo(unsigned a, unsigned b) {
if (a > b) return 0;
if (a == 0) return 0;
return a - 1 < b ? 1234 : 5678;
}
```
```asm
foo(unsigned int, unsigned int): # @foo(unsigned int, unsigned int)
dec edi
xor ecx, ecx
cmp edi, esi
mov eax, 1234
cmovae eax, ecx
ret
```
However, if I replace the `return` statements with `throw` statements, it is [no longer able to](https://godbolt.org/z/M1MeE3qEW) figure it out:
```cpp
unsigned foo(unsigned a, unsigned b) {
if (a > b) throw 0;
if (a == 0) throw 0;
return a - 1 < b ? 1234 : 5678;
}
```
```asm
foo(unsigned int, unsigned int): # @foo(unsigned int, unsigned int)
push rax
cmp edi, esi
ja .LBB0_3
test edi, edi
je .LBB0_3
dec edi
cmp edi, esi
mov ecx, 1234
mov eax, 5678
cmovb eax, ecx
pop rcx
ret
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzNVU1vozAQ_TXmYjUCE0Jy4NA0jXal7TnHlTFDcNZgapukza_fMbTZhjZVKq1WaxHM-M2Hx35PyXXxnG2kq6irpKVCF0DYHRqA33UrFRhKkiVCtubGUWh0t61IsiJsXjnXWhLfErbGZ6uLXCs30WaL1hF_m5QfN_tHkRC2oE7TUm47A1R3DvNzR8ks5PSGRpTEdzRHi2IZrg782VJnOvCpwxUJbxEaHtG2w0rXWLltoKCl1riTk8n95k9W7guTdDnEUByypOjOseL9gBpwnWloSOIPvVb4IHjJ8WXxTRf4XtOIxVP8uKXJLJ2fAki6GrXzYp4vclsPK6PWZOPOmuvtha_y-SAspmQaXpft1JkfBYh-hkKeA0_aDIB48kn8dIaLuj0FetyO4mu9H3Dex_vjGifQew4nh3cF8Nw_Ocpv-gB7MD4Sb_I7ereKC-hJja7DpXm6Wccd1NA4Sw9eArjmKqMP51ifx3luohAaTZVutqgKnitMqa-TwkP0APfx4_3GU-lFB5gTpfAvSN43dQXHP_L7OxT__8jddrbq--NfJO-OD_Pkx3IZ_ozPQQfWvQ0eK2cHnwRf1NvVehIX9DQSXH9n7wSX08uCa_WwA3OtEgPIohkONovnLCiyuFjECx446RRkD9JavBbdOlnLI3dSN4MABwLu7SvrhG4K6WEbdEZlI5VhRJdP8G8KDaX2r9NNa_QOBF75Gut0gAJeJ_MkZUGV5SAEm5ZlKMp5AWUxLSDn6UwsFmEkch4FiuegbIZKR2EHMmMhY1HIplEynTE2EaxMWMR9L0WK0cg9qLlUE1_Y6z0wWb-HvNtaBJW0zv4Bue0ZCa_5eecqbTInjnz3iwf9drN-r78BAeoVCw">