<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/73614>73614</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[AVR] [missing opt] 16-bit operations cause unnecessary register shuffling
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
burgerindividual
</td>
</tr>
</table>
<pre>
**Problem**
All 16-bit pseudo instructions incorrectly assume that combined operations must be done on adjacent working registers. This is likely due the instruction set including 3 different 16-bit operations labeled as "word" operations, `ADIW`, `SBIW`, and `MOVW`. Only `MOVW` operates on all working registers, with `ADIW` and `SBIW` operating only on the pointer register pairs, r24:25...r30:r31.
An example of this can be seen with SUBW, a pseudo instruction for subtracting two 16-bit words (as seen [here](https://github.com/llvm/llvm-project/blob/f3d2a31d7d433f9c843a61caa7b025f3b7188ddf/llvm/lib/Target/AVR/AVRInstrInfo.td#L493)):
```
// SUBW Rd+1:Rd, Rr+1:Rr
// Subtracts two 16-bit values and places the result into Rd.
//
// Expands to:
// sub Rd, Rr
// sbc Rd+1, Rr+1
```
**Reproduction Example**
When doing single 16-bit operation, we can force the bytes to be out of order using a byte swap. This should be dealt with by shuffling the operands of the `sub` and `sbc` instructions.
LLVM-IR:
```
; Function Attrs: mustprogress nofree noinline nosync nounwind willreturn memory(none)
define dso_local noundef i16 @test(i16 %0, i16 %1) unnamed_addr addrspace(1) #1 {
start:
%2 = tail call addrspace(1) i16 @llvm.bswap.i16(i16 %0)
%3 = tail call addrspace(1) i16 @llvm.bswap.i16(i16 %1)
%_0 = sub i16 %2, %3
ret i16 %_0
}
```
AVR Assembly:
```
test:
movw r18, r22
mov r18, r19
clr r19
mov r23, r22
clr r22
or r22, r18
or r23, r19
movw r18, r24
mov r18, r19
clr r19
mov r25, r24
clr r24
or r24, r18
or r25, r19
sub r24, r22
sbc r25, r23
ret
```
</pre>
<img width="1px" height="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