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
Index: compiler/avr/aoptcpu.pas
===================================================================
--- compiler/avr/aoptcpu.pas	(revision 42485)
+++ compiler/avr/aoptcpu.pas	(working copy)
@@ -1076,7 +1076,22 @@
                           GetNextInstruction(hp1,hp1);
                           if not assigned(hp1) then
                             break;
-                        end;
+                        end
+                    {
+                      This removes the mov from
+                      mov rX,...
+                      ldi/s rX,...
+                    }
+                    else if GetNextInstruction(p,hp1) and
+                       (MatchInstruction(hp1,A_LDI) or MatchInstruction(hp1,A_LDS)) and
+                       (taicpu(p).oper[0]^.typ = top_reg) and
+                       (taicpu(hp1).oper[0]^.typ = top_reg) and
+                       (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg) then
+                      begin
+                        DebugMsg('Peephole MovLDx2LDx performed', p);
+
+                        result:=RemoveCurrentP(p);
+                      end;
                   end;
                 A_SBIC,
                 A_SBIS:
_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to