Issue 242909
Summary [AVR] `ldi` emitted on a register below r16 after the register allocator rematerializes `LDIRdK` into a `gpr8` virtual register (silently mis-encoded in object output)
Labels new issue
Assignees
Reporter haradama
    ### Summary

`LDI` only encodes `r16`–`r31` (its def is `LD8`). In the attached function the greedy register allocator creates a new virtual register of class `gpr8` whose definition is `LDIRdK -1`, and assigns it `r9`. The assembly printer then prints `ldi r9, -1`, which is not a valid instruction — and with `-filetype=obj` the integrated assembler path does not diagnose it: the 4-bit register field wraps and the object contains `ldi r25, 0xff`, i.e. the constant is written to a different register and `r9` is left uninitialized.

### Reproducer

`repro.ll` is attached below (86 lines; `llvm-reduce` output, inline-asm bodies replaced by `nop` — only their register constraints matter).

```
llc -O3 -mtriple=avr-unknown-unknown -mcpu=atmega328p repro.ll -o - | grep -nP '^\tldi\tr([0-9]|1[0-5]),'
```

Output (LLVM 22.1.7):

```
	ldi	r9, -1
```

Feeding that text back to the assembler is rejected, as it should be:

```
$ llc -O3 -mtriple=avr-unknown-unknown -mcpu=atmega328p repro.ll -o repro.s
$ llvm-mc -triple=avr-unknown-unknown -mcpu=atmega328p -filetype=obj repro.s -o /dev/null
repro.s:259:6: error: invalid operand for instruction
        ldi     r9, -1
                ^
```

but direct object emission succeeds and encodes another register:

```
$ llc -O3 -mtriple=avr-unknown-unknown -mcpu=atmega328p repro.ll -filetype=obj -o repro.o
$ llvm-objdump -d repro.o
     ...
     19a: 81 2d        	mov	r24, r1
     19c: 9f ef        	ldi	r25, 0xff      ; printed as `ldi r9, -1` in the .s
     19e: 80 30        	cpi	r24, 0x0
```

### Where the register class is lost

The constant starts out correctly constrained and stays so until register allocation:

```
-stop-after=finalize-isel        %181:ld8  = LDIRdK -1
-stop-after=register-coalescer   %181:ld8  = LDIRdK -1
-stop-after=greedy               %291:ld8  = LDIRdK -1
                                 %318:gpr8 = LDIRdK -1     <-- new vreg, class gpr8
                                 ...
                                 CPCRdRr %311.sub_hi, %318, implicit-def $sreg, implicit killed $sreg
-stop-after=virtregrewriter      $r9 = LDIRdK -1
```

My reading (not verified in the allocator's source): a live range split of `%181` produced an interval whose only remaining use (`CPCRdRr`) allows `gpr8`, the interval's class was widened to `gpr8`, and the `LDIRdK` was then rematerialized as that interval's def without constraining the class back to the instruction's own (`ld8`).

### Version

LLVM 22.1.7. I have not tested trunk. In the original (larger) function this appeared only when the module carried branch weights and `optsize`; the same function without them allocated differently and was fine.

<details>
<summary>repro.ll</summary>

```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 void @f(ptr %0, ptr %1, i16 %2, ptr addrspace(1) %3, { ptr addrspace(1), ptr, i16, i16, i8, i8, i8, i8, i8, i8 } %4, i16 %5, i32 %6, ptr addrspace(1) %7, i64 %8, { ptr addrspace(1), ptr, i16, i16, i8, i8, i8, i8, i8, i8 } %9, i64 %10) addrspace(1) {
.preheader8.lr.ph:
  %11 = inttoptr i16 1 to ptr
  br label %.preheader8

.preheader8:                                      ; preds = %31, %.preheader8.lr.ph
  %12 = phi i16 [ 0, %.preheader8.lr.ph ], [ %5, %31 ]
  %13 = shl i16 %12, 0
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.preheader8
  %14 = tail call addrspace(0) { ptr addrspace(1), ptr, i16, i16, i8, i8, i8, i8, i8, i8 } asm sideeffect "nop", "=z,=x,=r,=r,=r,=&a,=&a,=&a,=&a,=&r,0,1,2,3,4,~{r0},~{r1},~{memory}"(ptr addrspace(1) null, ptr null, i16 0, i16 0, i8 0)
  %exitcond.not = icmp eq i16 0, 1
  br i1 %exitcond.not, label %.preheader7, label %.lr.ph

.preheader7:                                      ; preds = %.preheader7, %.lr.ph
  %15 = phi i16 [ %19, %.preheader7 ], [ 0, %.lr.ph ]
  %16 = getelementptr i16, ptr %1, i16 %15
  %17 = tail call addrspace(1) i16 @llvm.smax.i16(i16 %2, i16 -24218)
  %18 = add i16 %17, 1
  store i16 %18, ptr null, align 2
  %19 = or i16 %15, 1
  %exitcond15.not = icmp eq i16 1, 0
  br i1 %exitcond15.not, label %.preheader, label %.preheader7

.preheader:                                       ; preds = %.preheader7
  %20 = tail call addrspace(0) { ptr addrspace(1), ptr, i16, i16, i8, i8, i8, i8, i8, i8 } asm sideeffect "nop", "=z,=x,=r,=r,=r,=&a,=&a,=&a,=&a,=&r,0,1,2,3,4,~{r0},~{r1},~{memory}"(ptr addrspace(1) %3, ptr null, i16 0, i16 0, i8 1)
  %21 = extractvalue { ptr addrspace(1), ptr, i16, i16, i8, i8, i8, i8, i8, i8 } %4, 3
  %22 = tail call addrspace(0) { ptr addrspace(1), ptr, i16, i16, i8, i8, i8, i8, i8, i8 } asm sideeffect "nop", "=z,=x,=r,=r,=r,=&a,=&a,=&a,=&a,=&r,0,1,2,3,4,~{r0},~{r1},~{memory}"(ptr addrspace(1) %7, ptr %11, i16 0, i16 0, i8 2)
  br label %23

23:                                               ; preds = %23, %.preheader
  %24 = phi i64 [ 0, %.preheader ], [ %8, %23 ]
  %25 = phi i16 [ 0, %.preheader ], [ %30, %23 ]
  %26 = inttoptr i16 %25 to ptr
  %27 = tail call addrspace(0) { ptr addrspace(1), ptr, i16, i16, i8, i8, i8, i8, i8, i8 } asm sideeffect "nop", "=z,=x,=r,=r,=r,=&a,=&a,=&a,=&a,=&r,0,1,2,3,4,~{r0},~{r1},~{memory}"(ptr addrspace(1) null, ptr %26, i16 0, i16 0, i8 1)
  %28 = extractvalue { ptr addrspace(1), ptr, i16, i16, i8, i8, i8, i8, i8, i8 } %9, 2
  %29 = add i64 %24, 1
  %30 = or i16 %2, 1
  %exitcond17.not = icmp eq i16 0, 0
  br i1 %exitcond17.not, label %31, label %23

31:                                               ; preds = %23
  %32 = or i32 0, %6
  %33 = or i64 %10, 0
  %34 = extractvalue { ptr addrspace(1), ptr, i16, i16, i8, i8, i8, i8, i8, i8 } %22, 2
  %35 = zext i16 %34 to i32
  %36 = sext i32 %35 to i64
  %37 = add i64 %8, %36
  %38 = tail call addrspace(1) i64 @llvm.smax.i64(i64 %37, i64 -2147483648)
  %39 = tail call addrspace(0) { ptr addrspace(1), ptr, i16, i16, i8, i8, i8, i8, i8, i8 } asm sideeffect "nop", "=z,=x,=r,=r,=r,=&a,=&a,=&a,=&a,=&r,0,1,2,3,4,~{r0},~{r1},~{memory}"(ptr addrspace(1) null, ptr %1, i16 0, i16 0, i8 1)
  %40 = tail call addrspace(1) i64 @llvm.smin.i64(i64 %38, i64 2147483647)
  %41 = trunc i64 %40 to i32
  %42 = tail call addrspace(1) i32 @llvm.smin.i32(i32 %41, i32 0)
  %43 = trunc i32 %42 to i16
  %44 = getelementptr i8, ptr %0, i16 %13
  store i16 %43, ptr %44, align 2
  %45 = tail call addrspace(1) i64 @llvm.smax.i64(i64 %29, i64 -2147483648)
  %46 = tail call addrspace(1) i64 @llvm.smin.i64(i64 %45, i64 2147483647)
  %47 = trunc i64 %46 to i32
  %48 = trunc i32 %47 to i16
  %49 = getelementptr i8, ptr %44, i16 2
  store i16 %48, ptr %49, align 2
  br label %.preheader8
}

; Function Attrs: nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smax.i64(i64, i64) addrspace(1) #0

; Function Attrs: nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smin.i64(i64, i64) addrspace(1) #0

; Function Attrs: nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) addrspace(1) #0

; Function Attrs: nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.smax.i16(i16, i16) addrspace(1) #0

; uselistorder directives
uselistorder ptr addrspace(1) @llvm.smax.i64, { 1, 0 }
uselistorder ptr addrspace(1) @llvm.smin.i64, { 1, 0 }

attributes #0 = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }
```

</details>

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to