-replace {
- ld (hl),(hl)
-} by {
- ERROR - peephole - caught (hl),(hl)
-}
+// peeph.def - Common Z80 and gbz80 peephole rules
-replace restart {
- ld %1,%1
-} by {
- ; peephole removed redundant load.
-}
+// This file should contain common Z80/GBZ80 peepholes.
+// However the GBZ80 port is currently unmaintained;
+// a GBZ80 expert could extract peepholes that are useful for both Z80 and GBZ80
+// from peeph-z80.def and peeph-gbz80.def and move them here.
-replace restart {
- xor a,a
- or a,%1
- or a,a
- jp %2,%3
-} by {
- xor a,a
- or a,%1
- jp %2,%3
-}
-
-replace restart {
- jp NZ,%1
- jp %2
-%1:
- jp %3
-%2:
-} by {
- jp Z,%2
-%1:
- jp %3
-%2:
-} if labelRefCountChange(%1 -1)
-
-replace restart {
- jp NZ,%1
- jp %2
-%1:
-} by {
- jp Z,%2
-%1:
-} if labelRefCountChange(%1 -1)
-
-replace restart {
- jp Z,%1
- jp %2
-%1:
-} by {
- jp NZ,%2
-%1:
-} if labelRefCountChange(%1 -1)
-
-replace restart {
- or a,%1
- or a,a
-} by {
- or a,%1
-}
-
-replace restart {
- or a,%1)
- or a,a
-} by {
- or a,%1)
-}
-
-replace restart {
- xor a,a
- or a,%1
- jp NZ,%2
- xor a,a
- or a,%3
- jp Z,%2
-} by {
- xor a,a
- or a,%1
- jp NZ,%2
- or a,%3
- jp Z,%2
-}
-
-replace restart {
- jp NZ,%1
- inc %3)
-%1:
- jp %2
-} by {
- jp NZ,%2
- inc %3)
-%1:
- jp %2
-} if labelRefCountChange(%1 -1), labelRefCountChange(%2 1)
-
-replace restart {
- xor a,a
- ld a,#0x00
-} by {
- xor a,a
-}
-
-replace {
- ld e,#0x00
- ld d,#0x00
-} by {
- ld de,#0x0000
-}
-
-replace {
- ld l,#0x00
- ld h,#0x00
-} by {
- ld hl,#0x0000
-}
-
-replace {
- ld c,#0x00
- ld b,#0x00
-} by {
- ld bc,#0x0000
-}
-
-replace restart {
- ld %1,a
- ld a,%1
-} by {
- ld %1,a
-} if notVolatile %1
-
-replace restart {
- jp %1,%2
- jr %3
-%2:
- jp %4
-} by {
- jp %1,%4
- jr %3
-%2:
- jp %4
-} if labelRefCountChange(%2 -1), labelRefCountChange(%4 1)
-
-replace {
- ld l,e
- ld h,d
- push hl
- ld l,c
- ld h,b
- push hl
-} by {
- push de
- push bc
-}
-
-replace {
- and a,#%1
- or a,a
-} by {
- and a,#%1
-}
-
-replace {
- ld b,l
- ld a,b
- pop bc
- ld b,a
-} by {
- ld a,l
- pop bc
- ld b,a
-}
-
-replace {
- ld d,l
- ld a,d
- pop de
- ld d,a
-} by {
- ld a,l
- pop de
- ld d,a
-}
-
-replace {
- ld a,b
- push af
- inc sp
-} by {
- push bc
- inc sp
-}
-
-replace {
- ld a,d
- push af
- inc sp
-} by {
- push de
- inc sp
-}
-
-replace {
- ld a,%1
- sub a,#%2
- jp M,%3
- ld a,%1
- sub a,#%4
- jp P,%5
-} by {
- ld a,%1
- cp a,#%2
- jp M,%3
- cp a,#%4
- jp P,%5
-}
-
-replace {
- ld e,l
- ld d,h
- ld l,e
- ld h,d
-} by {
- ld e,l
- ld d,h
-}
-
-replace {
- ld %1,%2
- ld %3,%4
- ld %2,%1
- ld %4,%3
-} by {
- ld %1,%2
- ld %3,%4
-}
-
-// From: Philipp Klaus Krause
-replace restart {
- ld %1,a
- ld a,%2
- add a,%1
- ld %1,a
-} by {
- ; peephole removed loads by exploiting commutativity of addition.
- add a,%2
- ld %1,a
-}
-
-// From: Philipp Klaus Krause
-replace restart {
- ld %1,a
- ld a,%2
- add a,%1
-} by {
- ld %1, a
- ; peephole removed load by exploiting commutativity of addition.
- add a, %2
-}
-
-// From: Philipp Klaus Krause
-replace {
- or a,%1
- jp NZ,%2
- ld %3,#0x00
-} by {
- or a,%1
- jp NZ,%2
- ld %3,a
- ; peephole replaced constant #0x00 by a (which has just been tested to be #0x00).
-}
-
-// From: Philipp Klaus Krause
-replace {
- and a,%1
- jp NZ,%2
- ld %3,#0x00
-} by {
- and a,%1
- jp NZ,%2
- ld %3,a
- ; peephole replaced constant #0x00 by a (which has just been tested to be #0x00).
-}
-
-// From: Philipp Klaus Krause
-replace {
- sub a,%1
- jp NZ,%2
- ld %3,#0x00
-} by {
- sub a,%1
- jp NZ,%2
- ld %3,a
- ; peephole replaced constant #0x00 by a (which has just been tested to be #0x00).
-}
-
-// From: Philipp Klaus Krause
-replace {
- dec a
- jp NZ,%1
- ld %2,#0x00
-} by {
- dec a
- jp NZ,%1
- ld %2,a
- ; peephole replaced constant #0x00 by a (which has just been tested to be #0x00).
-}
-
-// From: Philipp Klaus Krause
-replace {
- or a,%1
- jp NZ,%2
- ld a,%3
- or a,a
-} by {
- or a,%1
- jp NZ,%2
- or a,%3
- ; peephole shortened or using a (which has just been tested to be #0x00).
-}
-
-// From: Philipp Klaus Krause
-replace {
- and a,%1
- jp NZ,%2
- ld a,%3
- or a,a
-} by {
- and a,%1
- jp NZ,%2
- or a,%3
- ; peephole shortened or using a (which has just been tested to be #0x00).
-}
-
-// From: Philipp Klaus Krause
-replace {
- sub a,%1
- jp NZ,%2
- ld a,%3
- or a,a
-} by {
- sub a,%1
- jp NZ,%2
- or a,%3
- ; peephole shortened or using a (which has just been tested to be #0x00).
-}
-
-// From: Philipp Klaus Krause
-replace {
- dec a
- jp NZ,%1
- ld a,%2
- or a,a
-} by {
- dec a
- jp NZ,%1
- or a,%2
- ; peephole shortened or using a (which has just been tested to be #0x00).
-}
-
-// From: Philipp Klaus Krause
-replace restart {
- or a,%1
- jp NZ,%2
- push %3
- ld %4,#0x00
-} by {
- or a,%1
- jp NZ,%2
- push %3
- ld %4,a
- ; peephole replaced constant #0x00 by a (which has just been tested to be #0x00).
-}
-
-// From: Philipp Klaus Krause
-replace restart {
- and a,%1
- jp NZ,%2
- push %3
- ld %4,#0x00
-} by {
- sub a,%1
- jp NZ,%2
- push %3
- ld %4,a
- ; peephole replaced constant #0x00 by a (which has just been tested to be #0x00).
-}
-
-// From: Philipp Klaus Krause
-replace restart {
- sub a,%1
- jp NZ,%2
- push %3
- ld %4,#0x00
-} by {
- sub a,%1
- jp NZ,%2
- push %3
- ld %4,a
- ; peephole replaced constant #0x00 by a (which has just been tested to be #0x00).
-}
-
-// From: Philipp Klaus Krause
-replace restart {
- dec a
- jp NZ,%1
- push %2
- ld %3,#0x00
-} by {
- dec a
- jp NZ,%1
- push %2
- ld %3,a
- ; peephole replaced constant #0x00 by a (which has just been tested to be #0x00).
-}
-
-// From: Philipp Klaus Krause
-replace restart{
- jp NZ,%1
- or a,%2
- jp Z,%3
-%1:
- jp %4
-%3:
-} by {
- jp NZ,%4
- ; peephole redirected jump
- or a,%2
- jp NZ,%4
- ; peephole redirected jump
-%3:
-} if labelRefCount(%1 1), labelRefCountChange(%1 -1), labelRefCountChange(%3 -1), labelRefCountChange(%4 1)
-
-// Should be one of the last ones. Opens the code to further peephole optimization.
-// From: Philipp Klaus Krause
-replace restart {
-%1:
-} by {
- ; peephole removed unused label %1.
-} if labelRefCount(%1 0)
-
-// From: Philipp Klaus Krause
-replace {
- jp Z,%5
-} by {
- jr Z,%5
-} if labelInRange()
-
-replace {
- jp NZ,%5
-} by {
- jr NZ,%5
-} if labelInRange()
-
-replace {
- jp C,%5
-} by {
- jr C,%5
-} if labelInRange()
-
-replace {
- jp NC,%5
-} by {
- jr NC,%5
-} if labelInRange()