TRSE icon indicating copy to clipboard operation
TRSE copied to clipboard

[z80 optimizations] Remove redundant jumps

Open ColinPitrat opened this issue 3 years ago • 0 comments

An example will be worth hundred words:

smallfloat_add_f1_zero:
    ld ix, [SmallFloat_f2]
    ld a,(ix)
    ld(iy), a
    ld a,(ix+1)
    ld(iy+1), a
    jr smallfloat_add_finished
smallfloat_add_f2_zero:
    ld ix, [SmallFloat_f1]
    ld a,(ix)
    ld(iy), a
    ld a,(ix+1)
    ld(iy+1), a
    jr smallfloat_add_finished
smallfloat_add_finished:
   ret

Here the two jumps can be removed:

  • The first one can be replaced by a ret because that's what the next instruction is just after the label
  • The second one can be removed even without knowing that it points to the ret because it just points to the next instruction

Note that this also works for conditional jumps that can be replaced by conditional returns:

  jr z, some_label
  ... some code ...
some_label:
  ret

can be replaced by:

  ret z
  ... some code ...
some_label:
  ret

Removing the ret instruction if the label is completely unused doesn't seem necessary and is probably hard. It is left as an exercise to the reader to determine whether it should be done or not ...

ColinPitrat avatar Apr 30 '21 09:04 ColinPitrat