Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2019-07

AVR - redundant mov followed by ldi instruction

2 messages · 1 person · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Christo Crause

The following test code generates a redundant mov instruction which seems to be related to a situation with an unused procedure parameter and the inc() procedure:

program testRegAlloc;
var
  a, b: byte;
procedure testDummyParam(var x: byte);
begin
  inc(a);
end;
begin
  testDummyParam(b);
end.

The generated code for testDummyParam:
mov r18, r24
ldi r18, 0x00 ; 0
ldi r19, 0x01 ; 1
movw r30, r18
ld r20, Z
inc r20
movw r30, r18
st Z, r20
ret

Moving r24 to r18 is redundant since it will be overwritten by the next ldi instruction. Attached is a patch that can identify this situation and eliminate the redundant mov. Is it the correct approach to fix this in the peephole optimizer, or can it be fixed at an earlier stage?

best wishes,
Christo

Reply by email

Christo Crause
to Christo Crause
PM Christo Crause wrote 31 lines
The following test code generates a redundant mov instruction which seems to be related to a situation with an unused procedure parameter and the inc() procedure: program testRegAlloc; var a, b: byte; procedure testDummyParam(var x: byte); begin inc(a); end; begin testDummyParam(b); end. The generated code for testDummyParam: mov r18, r24 ldi r18, 0x00 ; 0 ldi r19, 0x01 ; 1 movw r30, r18 ld r20, Z inc r20 movw r30, r18 st Z, r20 ret Moving r24 to r18 is redundant since it will be overwritten by the next ldi instruction. Attached is a patch that can identify this situation and eliminate the redundant mov. Is it the correct approach to fix this in the peephole optimizer, or can it be fixed at an earlier stage?

After some more testing i think my approach to fixing this is completely backwards. The issue (of redundant code generation) is probably better addressed upstream (at node level?) by identifying that the parameter is unused and hence code should not be generated in the first place.

Reply by email