| Issue |
242908
|
| Summary |
[AVR] Miscompile: `rol` of an undef byte is emitted as `adc rA, rB` with two different registers, so the carry-in bit is lost
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
haradama
|
### Summary
The AVR backend selects "rotate left through carry" as `ADCRdRr %x, %x`. When `%x` is an `IMPLICIT_DEF` (a byte of a wide shift whose upper bits are dead), the two uses are `undef` operands and the register allocator is free to give them different physical registers. The emitted instruction is then `adc r31, r24`, which is no longer `2*x + C`: bit 0 of the result is `r31.0 ^ r24.0 ^ C` instead of `C`. Bit 0 is the carry out of the byte below and is live, so the function returns a wrong value depending on what happens to be in those two registers.
### Reproducer
```llvm
target datalayout = "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8:16-a:8"
target triple = "avr-unknown-unknown"
define ptr addrspace(1) @f(i32 %0) addrspace(1) {
entry:
%1 = shl i32 %0, 9
%2 = add i32 %1, 262144
%3 = lshr i32 %2, 10
%4 = trunc i32 %3 to i16
%5 = getelementptr i16, ptr addrspace(1) null, i16 %4
ret ptr addrspace(1) %5
}
```
```
llc -O2 -mtriple=avr-unknown-unknown -mcpu=atmega328p repro.ll -o -
```
Output (LLVM 22.1.7):
```asm
f:
lsl r22
rol r23
adc r31, r24 ; <-- should be `rol rN` (adc rN, rN)
mov r30, r23
mov r25, r22
adiw r30, 4
lsr r31
ror r30
ror r25
lsr r31
ror r30
ror r25
mov r24, r25
mov r25, r30
lsl r24
rol r25
ret
```
### What goes wrong
After instruction selection (`-stop-after=finalize-isel`) the third byte of the shifted value comes from an `IMPLICIT_DEF`, which is fine by itself — only its bit 0 (the carry out of `rol r23`) is live:
```
%4:dregs = IMPLICIT_DEF
%10:gpr8 = ADDRdRr %0.sub_lo, %0.sub_lo, implicit-def $sreg
%11:gpr8 = ADCRdRr %0.sub_hi, %0.sub_hi, implicit-def $sreg, implicit $sreg
%12:gpr8 = ADCRdRr %4.sub_lo, %4.sub_lo, implicit-def $sreg, implicit $sreg
```
After register allocation (`-stop-after=virtregrewriter`) the two undef uses are different registers:
```
$r31 = ADCRdRr undef $r31, undef $r24, implicit-def dead $sreg, implicit killed $sreg
```
`x + x + C` has bit 0 equal to `C` for any `x`; `a + b + C` does not.
### Wrong result
`f(0x8000)` must return `0x8200` (`(((0x8000 << 9) + 0x40000) >> 10) * 2`, truncated to 16 bits). Stepping the emitted code by hand:
| argument | `r31` on entry | expected | emitted code returns |
| --- | --- | --- | --- |
| `0x00008000` | 0 | `0x8200` | `0x8200` |
| `0x00008000` | 1 | `0x8200` | `0x0200` |
| `0x00018000` | 0 | `0x8200` | `0x0200` |
| `0x00001234` | 1 | `0x1434` | `0x9434` |
(`r24` holds bits 16..23 of the argument, which the IR does not depend on, and `r31` is not an argument at all.)
### Version
LLVM 22.1.7 (`llc --version`: "LLVM version 22.1.7, Optimized build"). I have not tested trunk. Found in a larger function where the miscompiled value indexed a lookup table; the reproducer above is the `llvm-reduce` output.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs