Skip to content

getelementptr turns into add turns into or due to wrong alignment setting #63

Closed
@gergoerdi

Description

@gergoerdi

LLVM IR:

%"chip8_engine::machine::Machine" = type { i16, [0 x i8], i16, [0 x i8], [16 x i8], [0 x i8] }

; Function Attrs: noreturn nounwind uwtable
define void @main() unnamed_addr {
start:
  %_12.i = alloca %"core::option::Option<chip8_engine::opcodes::Op>", align 8
  %vmachine = alloca %"chip8_engine::machine::Machine", align 8
  call void @_ZN12chip8_engine7machine7Machine3new17hc6366a1c56686268E(%"chip8_engine::machine::Machine"* noalias nocapture nonnull sret dereferenceable(20) %vmachine) #6
  tail call fastcc void @_ZN9chip8_avr3spi5setup17hf04b9f9f265ec074E() #6
  tail call fastcc void @_ZN9chip8_avr10serial_ram5setup17h96855e7b777cd1ceE() #6
  %v1 = getelementptr inbounds %"chip8_engine::machine::Machine", %"chip8_engine::machine::Machine"* %vmachine, i16 0, i32 2
  br label %bb4

bb4:  
  %v6 = load i16, i16* %v1, align 2
  %v7 = call fastcc i8 @"_ZN75_$LT$chip8_avr..Board$u20$as$u20$chip8_engine..peripherals..Peripherals$GT$8read_ram17h4a6718a884d22702E"(i16 %v6) #6
  ret void
}

This dereferences the wrong address when setting %v6. If I remove the br label %bb4 and replace it with just a fallthrough, then the right address is loaded into %v6.

Wrong assembly:

main:                                   ; @main
; BB#0:                                 ; %start
	push	r28
	push	r29
	push	r16
	push	r17
	in	r28, 61
	in	r29, 62
	sbiw	r28, 28
	in	r0, 63
	cli
	out	62, r29
	out	63, r0
	out	61, r28
	movw	r16, r28
	subi	r16, 255
	sbci	r17, 255
	mov	r24, r16
	mov	r25, r17
	call	_ZN12chip8_engine7machine7Machine3new17hc6366a1c56686268E
	call	_ZN9chip8_avr3spi5setup17hf04b9f9f265ec074E
	call	_ZN9chip8_avr10serial_ram5setup17h96855e7b777cd1ceE
	mov	r30, r16
	mov	r31, r17
	ori	r30, 2
	ld	r24, Z
	ldd	r25, Z+1
	call	_ZN75_$LT$chip8_avr..Board$u20$as$u20$chip8_engine..peripherals..Peripherals$GT$8read_ram17h4a6718a884d22702E
	adiw	r28, 28
	in	r0, 63
	cli
	out	62, r29
	out	63, r0
	out	61, r28
	pop	r17
	pop	r16
	pop	r29
	pop	r28
	ret

Correct assembly, when the br label %bb4 is removed:

main:                                   ; @main
; BB#0:                                 ; %start
	push	r28
	push	r29
	in	r28, 61
	in	r29, 62
	sbiw	r28, 30
	in	r0, 63
	cli
	out	62, r29
	out	63, r0
	out	61, r28
	movw	r24, r28
	adiw	r24, 1
	call	_ZN12chip8_engine7machine7Machine3new17hc6366a1c56686268E
	call	_ZN9chip8_avr3spi5setup17hf04b9f9f265ec074E
	call	_ZN9chip8_avr10serial_ram5setup17h96855e7b777cd1ceE
	ldd	r24, Y+3
	ldd	r25, Y+4
	call	_ZN75_$LT$chip8_avr..Board$u20$as$u20$chip8_engine..peripherals..Peripherals$GT$8read_ram17h4a6718a884d22702E
	adiw	r28, 30
	in	r0, 63
	cli
	out	62, r29
	out	63, r0
	out	61, r28
	pop	r29
	pop	r28
	ret

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-llvmAffects the LLVM AVR backendhas-reduced-testcaseA small LLVM IR file exists that demonstrates the problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions