<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/76460>76460</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [AArch64] Suboptimal comparison against certain immediates
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:AArch64,
            missed-optimization
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Kmeakin
      </td>
    </tr>
</table>

<pre>
    LLVM canonicalizes non-strict comparisons against constants to strict comparisons (eg `ule x, c` => `ult x, c+1`. This results in less optimal AArch64 assembly if `c` can be encoded in a single `mov` instruction but `c+1` or `c-1` takes a `mov+movk` pair to encode:


[godbolt](https://godbolt.org/z/KxoWeYK3d)
```c
#include <stdbool.h>
#include <stdint.h>

#define u32 uint32_t
#define s32 int32_t

bool ule_11111111(u32 x) { return x <= 0x11111111; }
bool ule_22222222(u32 x) { return x <= 0x22222222; }
bool ule_33333333(u32 x) { return x <= 0x33333333; }
bool ule_44444444(u32 x) { return x <= 0x44444444; }
bool ule_55555555(u32 x) { return x <= 0x55555555; }
bool ule_66666666(u32 x) { return x <= 0x66666666; }
bool ule_77777777(u32 x) { return x <= 0x77777777; }
bool ule_88888888(u32 x) { return x <= 0x88888888; }
bool ule_99999999(u32 x) { return x <= 0x99999999; }

bool uge_11111111(u32 x) { return x >= 0x11111111; }
bool uge_22222222(u32 x) { return x >= 0x22222222; }
bool uge_33333333(u32 x) { return x >= 0x33333333; }
bool uge_44444444(u32 x) { return x >= 0x44444444; }
bool uge_55555555(u32 x) { return x >= 0x55555555; }
bool uge_66666666(u32 x) { return x >= 0x66666666; }
bool uge_77777777(u32 x) { return x >= 0x77777777; }
bool uge_88888888(u32 x) { return x >= 0x88888888; }
bool uge_99999999(u32 x) { return x >= 0x99999999; }

bool sle_11111111(s32 x) { return x <= 0x11111111; }
bool sle_22222222(s32 x) { return x <= 0x22222222; }
bool sle_33333333(s32 x) { return x <= 0x33333333; }
bool sle_44444444(s32 x) { return x <= 0x44444444; }
bool sle_55555555(s32 x) { return x <= 0x55555555; }
bool sle_66666666(s32 x) { return x <= 0x66666666; }
bool sle_77777777(s32 x) { return x <= 0x77777777; }
bool sle_88888888(s32 x) { return x <= 0x88888888; }
bool sle_99999999(s32 x) { return x <= 0x99999999; }

bool sge_11111111(s32 x) { return x >= 0x11111111; }
bool sge_22222222(s32 x) { return x >= 0x22222222; }
bool sge_33333333(s32 x) { return x >= 0x33333333; }
bool sge_44444444(s32 x) { return x >= 0x44444444; }
bool sge_55555555(s32 x) { return x >= 0x55555555; }
bool sge_66666666(s32 x) { return x >= 0x66666666; }
bool sge_77777777(s32 x) { return x >= 0x77777777; }
bool sge_88888888(s32 x) { return x >= 0x88888888; }
bool sge_99999999(s32 x) { return x >= 0x99999999; }

```


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycl0FvqzgQxz-NuYwaETuE5MChacilb0-72tWenoxxHG-NHTGmyuunX0EILVUiW29UpcIefgbpp78YjqiVlbIg2Y5k-4R3_uTa4rWR_E3bpHL1r-LHj7__AMGts1pwoz8kgnX2CX2rhQfhmjNvNTqLwBXXFvs1i55bj-Ad3OkjdCMVkHXaGQkXQl9AkHUKhO0JK6_r_rZOd0uyThfw10kjtBI74xG0BSMRwZ29briB5-dWnNYr4Iiyqcwv0MceM1AFt1BJkFa4Wtb9rRxQW2Vk39K4976pf-y2E147C1XnrzdfjwbXDpdPw4XnbxKB326lu8a9v_UbZ67b_nWv5xD2TNI9See_2U65unLGk2xP6Obk_Rn7Tnog9DBuLVyrCD18EHp4vbh_5L-vrCZ0OxLW6fVPjNeUaStMV0sg7AV9XTlnFifCygf72vqv27emWh61ldAxCp22ntGf_vseMgrzreG3PxA6I38uxyJ002MuhG6B5Dtope9aC5f-AQjbQ3qZOtkOSL7_xqFjhTlT510OGyvMmTrvclZjhTlT511ONlaYM3Xe5azHCnOmzrucfK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