<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/64090>64090</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[RISCV] Inefficient code generated for byte atomicrmw xchg when the input is a constant zero
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Amanieu
</td>
</tr>
</table>
<pre>
Noticed while working on https://github.com/rust-lang/rust/pull/114034. When the replacement value is known to be zero then we should lower to an `amoand` instead of a LL/SC loop.
### IR
```llvm
define zeroext i8 @swap_zero(ptr %0) {
%2 = atomicrmw xchg ptr %0, i8 0 monotonic
ret i8 %2
}
```
### Current output
```asm
swap_zero: # @swap_zero
andi a1, a0, -4
slli a0, a0, 3
li a2, 255
sllw a2, a2, a0
.LBB0_1: # =>This Inner Loop Header: Depth=1
lr.w a3, (a1)
li a4, 0
xor a4, a3, a4
and a4, a4, a2
xor a4, a3, a4
sc.w a4, a4, (a1)
bnez a4, .LBB0_1
srlw a0, a3, a0
andi a0, a0, 255
ret
```
### Ideal output
```asm
swap_zero: # @swap_zero
andi a1, a0, -4
slli a0, a0, 3
li a2, 255
sllw a2, a2, a0
not a2, a2
amoand a1, a2, (a1)
srlw a0, a1, a0
andi a0, a0, 255
ret
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVd-PozYQ_mvMy2gjY0MSHnjY3TRqpFUf7qr28WTwJLhnbGSbY-_--spAStgfvfYOIYM834y_zzOaEd6ri0EsSf5A8kMi-tBYV963wijsk8rKr-VvNqgaJQyN0giDdZ-VuYA10ITQecLvCTsSdryo0PTVprYtYUfX-3CnhbnM_4Qdu15rwo5pmlGebeDPBg2EBsFhp0WNLZoAX4TuEZSHz8YOBoKFCuEbOhuRBgYE39heS9B2QBftwgDZUtFaYSTZUlDGBxQS7BkEPD0Rdvz4CNrabkPogdD7eWV8euH0YbW_pdOr9Zd22pJ4VmYigc8B1B5IRv0guk9xi7B9FxwQllPCCiC7h8kL4hYDwg8ggm1V7doBnuvmAgv8MQaj0FpjgzWqvno6nI5hOZtZ7Q4v6L2t5bF3Lt6i7UPXhzd1CT_LWhTwe4jOK1Uzk-kRRqrxm0bOYmR-l80Yr_VkpIuRr_0nAIBg0cjyfG32Wg-LeV5nhZunhwf6KY0cv_OMEviB8F9-b5SHkzHo4MnaDn5FIdHFEAfsQkP4IX3Bz20mAjweTdg-Ci3e0ZBFDF0bn62bDVMIkb26wBvneWWvY9xg1nF8PTO8CfA2z8rgtwV5vb71fTs9XNPFby_7Vb5vUvoqaw7Df6rJk0Sh_39Ffj_XP1auS829W7U_Ua5XjLEBVpg1wbFZ_UOQvZ_NMVULzXR11k-lKZEllwUvRIJlui0oy3ZpQZOmPO85T1OZ7Vm1r1BUjHNGJdY51ryitEpUySjjdMdymtItpxtR7OR2n6fb835XSFGQjGIrlN7EHrqx7pIo73sstxktaKJFhdqP44YxgwOMRsJYnD6ujD53VX_xJKNa-eCXKEEFPc6pD6ePj3-Q_AAng-ezqlXsebWVCBc06ERACWfroPoa8GX3Ha5TR5muD3HSCKit8UGYMPb4pHe6_JfBNg6G6XPXOfsX1nG2jSI8YcdR5N8BAAD__7el78k">