<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/97175>97175</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[AArch64] Unnecessary sign-extend that then has to be undone by `and`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Kmeakin
</td>
</tr>
</table>
<pre>
https://godbolt.org/z/nrsT16Es7
This c code
```c
#include <stdint.h>
#define likely(x) __builtin_expect(!!(x), 1)
typedef uint8_t u8;
typedef uint32_t u32;
u32 src(u8* bytes) {
u8 b0 = bytes[0];
if (likely(b0 <= 0x7F)) {
return b0;
}
u8 b1 = bytes[1];
u32 c = ((b0 & 0x1f) << 6) | ((b1 & 0x3f) << 0);
return c;
}
```
produces this assembly:
```asm
src: // @src
ldrsb w9, [x0]
mov x8, x0
and w0, w9, #0xff
tbnz w9, #31, .LBB0_2
ret
.LBB0_2:
ldrb w8, [x8, #1]
and w8, w8, #0x3f
bfi w8, w0, #6, #5
mov w0, w8
ret
```
GCC generates a zero-extending load instead and so is able to lead out the `and w0, w9, #0xff`.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx0lM9y4ygQxp8GXbriQiDJ6KCD_4z3sHucPadAtCx2MHIJlMh5-i2w7NjJjEslLPqj-fHR1dJ7c3SIDSm3pNxncgr9MDZ_n1D-Mi5Tg740fQhnT_iGsANhh-Og1WDDahiPhB0-CDu40f_Mqx9-Teie0M31_bM3HlpoB42P06Si16ddvhk3rrWTRiB854M2Lqx6wn88LWJcY2ccgjW_0F4IEzNhNby-qsnYYNwrzmdsA2GCsDw9SUDYDvI4PuQKlzNq7GAyLojXAJMgfPs9xFmMcXYPPr4nzsCPLWFiEoRtQF0C-ghE1osaAGASoCgQvl_i5ZaScn9PGCWmA8LE_VBJvosr6Lw-pAN8yRl_I4ZpdKDoUyqy3j8i3gDyJ4D8K0A8SZskybtEwCqgc96lvSPNDqorx-4myhcRfxTRyHvPvUC2n_7d-W4V8Ih7Hgc9teghxLKR3uNJ2Uusuec10p-uM9F-voFrSQIpaJx4ssnq0SsAeE9lQMrtnOy_ak7DWxLNIgZn-rxUOp3GdxqjSwLG6dx1z8Kg3Ad87sE4z-Of1T_bLX1l367tOnGL8s03YJWSiRuwWLLmn-B3tBR7F3c0_gVNdeZRSBdhtYzlszr6sZxW_AH7t_f2124HR3Q4yoAeJHzgOLzgHNBp445gB6nBOB9Q6oTuB4j3qyxCGMDG6WEKEHqEeLtO_9bziq4y3XBd81pm2OTrvK5EtaZl1jcFo7RumaZCVqLoKsFaqlVNi0JXqiqqzDSMsoJWrM5FUdJyRStsu7JGxXhd17kkBcWTNHZl7dsptrXMeD9hU6_zdZlZqdD61B4Zc_gOKUgYi91ybOKaFzUdPSmoNT74zyzBBJv66mYztn1VkHIP_zqHLXovxwvExrt4BaGXyQUHvfTRGoUwOT04BHVZrCEVzabRfm3HJvSTWrXDibBD3HsZXs7j8F_qiYdE7Ak7XE_01rD_AwAA__8wn5eG">