<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jul 23, 2019 at 9:49 PM Christo Crause <<a href="mailto:christo.crause@gmail.com">christo.crause@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>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:</div><div><br></div><div>program testRegAlloc;<br>var<br>  a, b: byte;<br>procedure testDummyParam(var x: byte);<br>begin<br>  inc(a);<br>end;<br>begin<br>  testDummyParam(b);<br>end.</div><div><br></div><div>The generated code for testDummyParam:</div><div></div><div>  mov     r18, r24<br>  ldi        r18, 0x00       ; 0<br>  ldi     r19, 0x01       ; 1<br>  movw    r30, r18<br>  ld r20, Z<br>  inc  r20<br>  movw    r30, r18<br>  st Z, r20<br>  ret</div><div><br></div><div>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?</div></div></blockquote><div><br></div><div>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.<br></div></div></div>