<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/141137>141137</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[AArch64] Need to adjust comparison with -1 to comparison with 0 when it is beneficial
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
AZero13
</td>
</tr>
</table>
<pre>
When I corrected an issue with getAArch64Cmp: the following changed:
; CHECK-SD-NEXT: ands w8, w8, w8, asr #31
; CHECK-SD-NEXT: csinv w8, w8, wzr, ge
to
; CHECK-SD-NEXT: and w8, w8, w8, asr #31
; CHECK-SD-NEXT: cmn w8, #1
; CHECK-SD-NEXT: csinv w8, w8, wzr, gt
Now, this is only one regression; the rest of the changes are improvements. However, it made me realize an issue: the compiler is peephole optimizing a compare with -1 but not because it is better, but because it thought -1 was not a legal immediate for a compare, which my patch corrects the compiler into realizing -1 is a valid cmp, so it changes it to a comparison with 0, which is beneficial in a tiny number of cases, like this one, but does more harm than good.
So the issue is not with this patch, but rather, we ought to peephole optimize a comparison with -1 to be a comparison with 0, if we know cse will do the job.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJycVM2O2zYQfhrpMrAhUdLaPujgeGOkKLCXFGjRG0WORW5IjkBSFrxPX5Cys02TtkAAw7JBznx_o-Eh6NEh9kX3oeieSz5HRb4__ome6qYcSN763xU6-AUEeY8iogTuQIcwIyw6KhgxHo9eqKf2ZKeiOUJUCBcyhhbtRhCKuxFl0RyLKn-aD3D69PH06-bz8-bl4x-_pRIA4E4GWPYFO33zzYOHgjVN_R-lImh3_bb2zafHiCtmpP_F_llo6-63C_aTHOPK7YWW9DcqHUAHIGduQA7B4-gxBE0utU7eegwR6JJ_r-4G4B5B28nTFS26GLbwiRa8YobQESyXCDbVcqPf8GuEj7wE2Ukb9Al6QpwUGQSaorb6LaXI842EkjPf1DDMERxFGFDwOWAC0QEGjHEFTed_O4uK5lHFVLnwkCs5GBy5AW0tSs1jmhr_jpQ9UloosDeYeBTqMYLhH5RdpLuwRHVTJyIcrtxoCcJOqVGgROLhVuJDX5F0ILfKqt4xsxaHFy10ouiAQ9TuBm62A_pkv-ABQyow-guuuZHDh3RJGMCSR1DcW4iKOxiJ5HZN-zNlDetrpFc_MoXcJ6t9dPI8qtXSBWE1MdJ3IeEP5GzqdHP40VFWqi-p5RdHC4iQkjUG5ErslYY701L2jTw0B15iX-_a3aF92jNWqr658L1kOEjGdsNedpcntq9E_VRVouN73pW6ZxXrqo4xVrW7im2ZqHdN21StkLyuxaFoK7Rcm60xV7slP5bZj75u67rZlYYPaELeTIw5XO4Ty1haVL5PRZthHkPRVkaHGN7bRB1NXmn3xVR0z_CCKHPq8nUO8V-8-s4mWNLye8z2Yx7K2ZtexTiFtNbYuWDnUUc1D1tBtmDnROT-2EyeXlHEgp0z_VCw813ftWd_BQAA__--HMV5">