<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/62781>62781</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
LLVM/Clang does not optimize division by a power of 2 correctly on rv64gc
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ryao
</td>
</tr>
</table>
<pre>
```
int main (int argc, char ** argv)
{
return (argc / 4);
}
```
At -O2, that compiles to:
```
main: # @main
slli a1, a0, 1
srli a1, a1, 62
add a0, a0, a1
sraiw a0, a0, 2
ret
```
https://gcc.godbolt.org/z/Eza9Kv6dT
If you manually change it to use a bit shift, we get the optimal solution:
```
main: # @main
srai a0, a0, 2
ret
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0U8GOmzAQ_RpzGW1kBkLCgUN2t0hVt-ql6n2wHXBlcGQPibJfX5lktZus1J5qofGIeW_e8IwpRttPxjRi_SjWzxnNPPjQhDP5rPP63IhKXh_5LOTOTgwj2QkEblNOoVcCn0ANFEDgTuAuvTsKrC8EsXm8JrIOhuewUBMNBLZQJmDxBtk8X5Nb0UvcMTz8wCTGAzEoPx6sMxHYi2L3EXjHTuOKYgf_XgILEKVcCAsVonM2VShPuiRTzK-164rhBrLECm8xpPVll-9t6FMfsqc7zFubYPgvxgzMh5g8wFZg2yu16r3uvOOVD73A9lVg--WV6m_HSv_8SPy6h7OfYaRpJufO6RCn3oBlYA9zNEDQWYY42D2ncU4GesPAgwF_YDuSg-jdzNZP__EIAtl77-7s_WxPpptC10VNmWnyalsWRb3e5NnQFJRrrcoyr1Qt87JQKu_Wqt7KgnBTyjyzDUos5Drf5JWsZL2qao0lVXtNeyqx3opSmpGsWzl3HJPDmY1xNk2Fm22eOeqMi8ttQpzMCZaiQEyXKzSJ89DNfRSldDZyfO_Clp1pXl5-fRfYPjmaetDeRJg8X9y2rwa0Pdpo_QTdGQgO_mQC-D0gKB-CUezO4CcIx6rsVTYH19z9G5aHuVspPwpsk_J1ezgE_9soFtgu80aB7fI9fwIAAP__RuweUA">