<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/158741>158741</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[AArch64] Unnecessary `addv` for `popcount(uint8_t)`
</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>
https://godbolt.org/z/e9qPT9z81
# C++ code
```c++
#include <cstdint>
auto popcount_u8(std::uint8_t x) { return __builtin_popcountg(x); }
```
# Assembly (LLVM)
```asm
popcount_u8(unsigned char):
and w8, w0, #0xff
fmov s0, w8
cnt v0.8b, v0.8b
addv b0, v0.8b
fmov w0, s0
ret
```
# Assembly (GCC)
```asm
popcount_u8(unsigned char):
and w0, w0, 255
fmov d31, x0
cnt v31.8b, v31.8b
smov w0, v31.b[0]
ret
```
As you can see from GCC's assembly, the `addv` is unecessary, because all the lanes except for the first will be zero.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysVMuO6zYM_Rp6I0wg0_Fr4UUeTRe9Bbpouw1kiU7UK0upJWVm8vWF7KSTOxigmxqCJfCQhzyEKOG9PlmiDsotlPtMxHB2U_fLSOK7tlnv1Ht3DuHiodgAHgAPJ6d6Z8LKTSfAww3wQO3fv_3e3poc-CYtLNgOcAu4ZdIpSqaKL0su9sVLW2miIgbFTvqgtA1Q_LRwiBgcu7iLdNGGY2wAGx9UqqHYRG1DcwzsDbBlUG_ZRCFOlh2PfdQmaHt8BJ4Am-QFxZZBvX8u5F7qUu3Gexp7884Am2_f_vw1hTz5Cj8C3_xYTbRz3xSTZzHNKRIZu3_Cqnl_bQB37JWnP2DB34YheQ2juybYz8Br8xQpbZj3K181fUKXwxO1UnNsz79CH8xLSs-foInCFw34pP7n3e7_E88_xGNZPgtXRZ6sb_wr5UX-kL6cPlz8D-oS3EO55VDu_0vnxrN3F5kUlnkiNkxuZLPW2jNx1584w5lYEq3UFSrOtGfRkiTvxTTjPUkRPTFhzOxrhCXP6E3SJbDBTbNx0JMP7FUbw3piN5rcKlNdodqiFRl1eV3WbZ2vsczOXVupum6Kdd8OleibSlZK5KiI6qoQZSky3SHHkrd5iTkW63LVVBUOTcEbymmo1i2sOY1Cm5Ux1zFNZaa9j9TlZVOv88yInoyfxxuxF_I72TRHm80kz9UaEAF3gDhq70m9uEvQo76JoJ1NWLnPpi4Rv_Tx5GHNjfbBf6QKOpj57XjwlXv2h_23Zc-9TN2Bij9uUrpGyyCnC1TxLE7m80ujwzn2K-lGwENKed9eLpP7i2QAPMxSPeDhrvba4T8BAAD__8qEYAE">