<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/54649>54649</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[Aarch64] Improve "a MOD pow2cst" codegen
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ilinpv
</td>
</tr>
</table>
<pre>
Code generation for modulo operations with a power of two can be improved for aarch64.
```
int
mod2 (int a)
{
return a % 2;
}
int
mod16 (int a)
{
return a % 16;
}
```
At -O2 GCC produces shorter code
https://godbolt.org/z/v63WYvzna
Generally:
;; Expansion of signed mod by a power of 2 using CSNEG.
;; For x0 % n where n is a power of 2 produce:
;; negs x1, x0
;; and x0, x0, #(n - 1)
;; and x1, x1, #(n - 1)
;; csneg x0, x0, x1, mi
For the constant 2 the special case is
```
cmp x0, xzr
and x0, x0, 1
cneg x0, x0, lt.
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyNU01v2zAM_TXyhUhgybETH3zIR1vssPWww7CjLKu2BkUyJDlp--tHyykStwG2wLEpkXwkH8naNm_V3jYSWmmk40FZAy_WwdE2g7Zg-8ulh7MKHXDo7Vk6sC8QzhYEN1BLUMfe2ZNsoifnTnTFaknSA0m3pEgvTzwqEyYB8RkQtsEL4ISVF-v1bhIAnAyDMxiQsBwYyXYfFoeL8AWPFv8PSIs7iPNUp_c2wOKZwdN-D1hjMwjpwXfWBSRBIG-TVRdC70m2JewRn9Y2tdVhaV2Lp3f8n4rs1-_Tu-G30E-Rca3fRseb-zGzbAcPrz03fuwHku1Va5BfrBLqt9suMBi8Mi3sf_54eFrOAB6xGa9pLNfAuZNO4lf5ufelqGsKk6-RrQeAV0rYHkFmOm4aGHXpRYdvwjKk3sAC6JX6ufWERP9hLTxGnmNPPkc12Y1FhU4i98YHjr1m8eh7KRTXOJAex9HfbSjEnzj2N9m_u1vdncrozDcmN9Njmz8FS5oqa8qs5ElQQcuK5LvttBIkP8C3aVWQA8bh-_Nh7AUTPuA5zhOuYTI4XX2aKNy9oV4Ke8SD1qePzwLB_kiB3o_K-0F6FPJVsSqTrlpvijSXJc0amW9eKGe5pI0QsinytJCsTjSvpfZjfhjcyDNECJQxz0RVLGUszbKUrimjdFkWdSnWOUIKueargqxSeeRKL8c8xlFPXBVTqofWo1IrH_xVyX0c4UjHiM-HgFtUKa1Mf0pi5Cpm_hf7iUsv">