+
+replace restart {
+ ld de,#%1 + %2
+ ld a,e
+ add a,%3
+ ld e,a
+ ld a,d
+ adc a,%4
+ ld d,a
+} by {
+ ld a,#<(%1 + %2)
+ add a,%3
+ ld e,a
+ ld a,#>(%1 + %2)
+ ; peephole 38 directly used (%1 + %2) in calculation instead of placing it in de first.
+ adc a,%4
+ ld d,a
+}
+
+replace restart {
+ rlca
+ ld a,#0x00
+ rla
+} by {
+ rlca
+ and a,#0x01
+ ; peephole 39 replaced zero load, rla by and since rlca writes the same value to carry bit and least significant bit.
+}
+
+replace restart {
+ ld %1,%2
+ push %1
+ pop %4
+ ld %1,%3
+} by {
+ ld %4,%2
+ ; peephole 40 moved %2 directly into de instead of going through %1.
+ ld %1,%3
+}
+
+replace restart {
+ add a,#0x00
+ ld %2,a
+ ld a,%3
+ adc a,%4
+} by {
+ ; peephole 41 removed lower part of multibyte addition.
+ ld %2,a
+ ld a,%3
+ add a,%4
+}
+
+replace restart {
+ ld %1,a
+ ld a,%2
+ add a,%1
+} by {
+ ; peephole 42 removed loads by exploiting commutativity of addition.
+ add a,%2
+} if notVolatile(%1), notUsed(%1), operandsNotRelated(%2 '(bc)' '(de)')
+
+replace restart {
+ ld %1 (ix),a
+ ld a,#%2
+ add a,%1 (ix)
+} by {
+ ld %1 (ix),a
+ ; peephole 42a removed loads by exploiting commutativity of addition.
+ add a,#%2
+}
+// Don't need to check for volatile, since ix is used to access the stack.
+
+// sdcc does not use the H flag. sla resets it, while add sets it.
+// To ensure that the state of the H flag is not changed by this
+// peephole uncomment the add %3, %4 at the end (since it overwrite the H flag).
+replace restart {
+ ld %1, a
+ sla %1
+ ld a, %2
+ //add %3, %4
+} by {
+ add a, a
+ ; peephole 42b shifted in accumulator insted of %1
+ ld %1, a
+ ld a, %2
+ //add %3, %4
+}
+
+// sdcc does not use the H flag. sla resets it, while add sets it.
+// To ensure that the state of the H flag is not changed by this
+// peephole uncomment the add %3, %4 at the end (since it overwrite the H flag).
+replace restart {
+ ld %1, a
+ sla %1
+ sla %1
+ ld a, %2
+ //add %3, %4
+} by {
+ add a, a
+ add a, a
+ ; peephole 42b' shifted in accumulator insted of %1
+ ld %1, a
+ ld a, %2
+ //add %3, %4
+}
+
+replace restart {
+ ld l,%1 (ix)
+ ld h,%2 (ix)
+ ld a,(hl)
+ inc a
+ ld l,%1 (ix)
+ ld h,%2 (ix)
+ ld (hl),a
+} by {
+ ld l,%1 (ix)
+ ld h,%2 (ix)
+ inc (hl)
+ ; peephole 42c incremented in (hl) instead of going through a.
+} if notUsed('a')
+
+replace restart {
+ ld l,%1 (ix)
+ ld h,%2 (ix)
+ ld a,(hl)
+ dec a
+ ld l,%1 (ix)
+ ld h,%2 (ix)
+ ld (hl),a
+} by {
+ ld l,%1 (ix)
+ ld h,%2 (ix)
+ dec (hl)
+ ; peephole 42d decremented in (hl) instead of going through a.
+} if notUsed('a')
+
+replace restart {
+ ld %1,a
+ ld a,%2
+ add a,%1
+} by {
+ ld %1, a
+ ; peephole 43 removed load by exploiting commutativity of addition.
+ add a,%2
+} if operandsNotRelated(%2 '(bc)' '(de)')
+
+replace restart {
+ ld c,l
+ ld b,h
+ ld hl,#%1
+ add hl,bc
+} by {
+ ; peephole 43a removed loads by exploiting commutativity of addition.
+ ld bc,#%1
+ add hl,bc
+} if notUsed('bc')
+
+replace restart {
+ ld hl,#%1
+ add hl,%2
+ ld bc,#%4
+ add hl,bc
+} by {
+ ; peephole 43b removed loads by exploiting commutativity of addition.
+ ld hl,#%1 + %4
+ add hl,%2
+} if notUsed('bc')
+
+replace restart {
+ ld c,e
+ ld b,d
+ ld hl,#%1
+ add hl,bc
+} by {
+ ; peephole 43c removed loads by exploiting commutativity of addition.
+ ld hl,#%1
+ add hl,de
+} if notUsed('bc')
+
+replace restart {
+ or a,%1
+ jp NZ,%2
+ xor a,a
+ or a,%3
+} by {
+ or a,%1
+ jp NZ,%2
+ ; peephole 44 removed redundant zeroing of a (which has just been tested to be #0x00).
+ or a,%3
+}
+
+replace restart {
+ or a,%1
+ jp NZ,%2
+ ld %3,#0x00
+} by {
+ or a,%1
+ jp NZ,%2
+ ld %3,a
+ ; peephole 45 replaced constant #0x00 by a (which has just been tested to be #0x00).
+}
+
+replace restart {
+ and a,%1
+ jp NZ,%2
+ ld %3,#0x00
+} by {
+ and a,%1
+ jp NZ,%2
+ ld %3,a
+ ; peephole 46 replaced constant #0x00 by a (which has just been tested to be #0x00).
+}
+
+replace restart {
+ sub a,%1
+ jp NZ,%2
+ ld %3,#0x00
+} by {
+ sub a,%1
+ jp NZ,%2
+ ld %3,a
+ ; peephole 47 replaced constant #0x00 by a (which has just been tested to be #0x00).
+}
+
+replace restart {
+ dec a
+ jp NZ,%1
+ ld %2,#0x00
+} by {
+ dec a
+ jp NZ,%1
+ ld %2,a
+ ; peephole 48 replaced constant #0x00 by a (which has just been tested to be #0x00).
+}
+
+//replace restart {
+// or a,%1
+// jp NZ,%2
+// ld a,%3
+// or a,a
+//} by {
+// or a,%1
+// jp NZ,%2
+// or a,%3
+// ; peephole 49 shortened or using a (which has just been tested to be #0x00).
+//} if operandsNotSame3(%3 '(bc)' '(de)')
+
+//replace restart {
+// and a,%1
+// jp NZ,%2
+// ld a,%3
+// or a,a
+//} by {
+// and a,%1
+// jp NZ,%2
+// or a,%3
+// ; peephole 50 shortened or using a (which has just been tested to be #0x00).
+//} if operandsNotSame3(%3 '(bc)' '(de)')
+
+// Commented out sine it triggers for %3 od the form (#...)
+//replace restart {
+// sub a,%1
+// jp NZ,%2
+// ld a,%3
+// or a,a
+//} by {
+// sub a,%1
+// jp NZ,%2
+// or a,%3
+// ; peephole 51 shortened or using a (which has just been tested to be #0x00).
+//}
+//if operandsNotSame3(%3 '(bc)' '(de)')
+
+replace restart {
+ dec a
+ jp NZ,%1
+ ld a,%2
+ or a,a
+} by {
+ dec a
+ jp NZ,%1
+ or a,%2
+ ; peephole 52 shortened or using a (which has just been tested to be #0x00).
+}
+//if operandsNotSame3(%2 '(bc)' '(de)')
+
+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 53 replaced constant #0x00 by a (which has just been tested to be #0x00).
+}
+
+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 54 replaced constant #0x00 by a (which has just been tested to be #0x00).
+}
+
+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 55 replaced constant #0x00 by a (which has just been tested to be #0x00).
+}
+
+replace restart {
+ dec a
+ jp NZ,%1
+ push %2
+ ld %3,#0x00
+} by {
+ dec a
+ jp NZ,%1
+ push %2
+ ld %3,a
+ ; peephole 56 replaced constant #0x00 by a (which has just been tested to be #0x00).
+}
+
+replace restart {
+ ld de,#%1 + %2
+ inc de
+} by {
+ ld de,#%1 + %2+1
+ ; peephole 59 moved increment of de to constant.
+}
+
+replace restart {
+ ld bc,#%1 + %2
+ inc bc
+} by {
+ ld bc,#%1 + %2+1
+ ; peephole 62 moved increment of bc to constant.
+}
+
+replace restart {
+ ld bc,#%1
+ ld a,c
+ add a,#0x%2
+ ld c,a
+ ld a,b
+ adc a,#0x%3
+ ld b,a
+} by {
+ ld bc,#%1 + 0x%3%2
+ ; peephole 63 moved addition of constant 0x%3%2 to bc to constant.
+}
+
+replace restart {
+ call %1
+ ret
+} by {
+ jp %1
+ ; peephole 65 replaced call at end of function by jump.
+}
+
+// Callee saves ix.
+replace restart {
+ call %1
+ pop ix
+ ret
+} by {
+ pop ix
+ jp %1
+ ; peephole 66 replaced call at end of function by jump moving call beyond pop ix.
+}
+
+replace restart {
+ ld %1,a
+ ld %2,%1
+ ld %3,%1
+} by {
+ ; peephole 67 loaded %2, %3 from a instead of %1.
+ ld %1,a
+ ld %2,a
+ ld %3,a
+} if notVolatile(%1)
+
+replace restart {
+ ld %1,a
+ ld %2,a
+ ld %3,%1
+} by {
+ ld %1,a
+ ld %2,a
+ ; peephole 68 loaded %3 from a instead of %1.
+ ld %3,a
+} if notVolatile(%1)
+
+replace restart {
+ ld %1,l
+ xor a,a
+ or a,%1
+} by {
+ ld %1,l
+ xor a,a
+ ; peephole 69 used l in or instead of %1.
+ or a,l
+} if notVolatile(%1)
+
+replace restart {
+ ld %1,#%2
+ ld %3,%4
+ ld %1,#%2
+} by {
+ ld %1,#%2
+ ld %3,%4
+ ; peephole 70 removed load of #%2 into %1 since it's still there.
+} if notVolatile(%1), operandsNotRelated(%3 %1)
+
+replace restart {
+ ld hl,#%1
+ ld de,#%1
+} by {
+ ; peephole 70a used #%1 from hl for load into de.
+ ld hl,#%1
+ ld e,l
+ ld d,h
+}
+
+replace restart {
+ ld hl,#%1
+ push hl
+ ld l,e
+ ld h,d
+ jp (hl)
+%1:
+} by {
+ ; peephole 71 used ex to get de into hl.
+ ex de,hl
+ ld de,#%1
+ push de
+ jp (hl)
+%1:
+}
+// Lets' hope this is never applied to a jump table. It's fine for calls.
+
+// Should be after 0z.
+replace restart {
+ ld %1 (ix),l
+ ld %2 (ix),h
+ ld %3,%1 (ix)
+ ld %4,%2 (ix)
+} by {
+ ld %1 (ix),l
+ ld %2 (ix),h
+ ; peephole 72 used hl instead of %2 (ix), %1 (ix) to load %4%3.
+ ld %3,l
+ ld %4,h
+} if operandsNotRelated('h' %3)
+// Don't check for volatile since ix points to the stack.
+
+replace restart {
+ ld %1, a
+ ld a, %2 (%3)
+ adc a, #%4
+ ld l, %1
+} by {
+ ld l, a
+ ld a, %2 (%3)
+ adc a, #%4
+ ; peephole 76 loaded l from a directly instead of going through %1.
+} if notUsed(%1)
+
+replace restart {
+ ld %1, a
+ ld a, #%2
+ adc a, #%3
+ ld l, %1
+} by {
+ ld l, a
+ ld a, #%2
+ adc a, #%3
+ ; peephole 77 loaded l from a directly instead of going through %1.
+} if notUsed(%1)
+
+replace restart {
+ ld hl, #%1
+ add hl, %2
+ ex de, hl
+ ld hl, #%3
+ add hl, de
+} by {
+ ld hl, #%1+%3
+ add hl, %2
+ ; peephole 78 removed addition and loads exploiting commutativity of addition.
+} if notUsed('de')
+
+replace restart {
+ ex de, hl
+ ld hl, #%1
+ add hl, de
+} by {
+ ; peephole 78a removed ex exploiting commutativity of addition.
+ ld de, #%1
+ add hl, de
+} if notUsed('de')
+
+replace restart {
+ ld hl, #%1
+ add hl, %2
+ ex de, hl
+ inc de
+} by {
+ ld hl, #%1+1
+ ; peephole 79 moved increment to constant.
+ add hl, %2
+ ex de, hl
+} if notUsed('hl')
+
+// These ex-generating rules should be among the last ones since ex counts as a read from both hl and de for notUsed().
+replace restart {
+ ld d,h
+ ld e,l
+} by {
+ ; peephole 80 used ex to load hl into de.
+ ex de,hl
+} if notUsed('hl')
+
+replace restart {
+ ld e,l
+ ld d,h
+} by {
+ ; peephole 81 used ex to load hl into de.
+ ex de,hl
+} if notUsed('hl')
+
+replace restart {
+ ld l,e
+ ld h,d
+} by {
+ ; peephole 82 used ex to load de into hl.
+ ex de,hl
+} if notUsed('de')
+
+// peeph-z80.def - Z80 peephole rules
+//
+//
+// (c) Philipp Klaus Krause (pkk@spth.de, philipp@colecovision.eu) 2006 - 2007
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option) any
+// later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+replace restart {
+ ld %1,#%2
+ ld a,%3 (%1)
+ ld %4,a
+ ld %1,#%5
+} by {
+ ld a,(#%2 + %3)
+ ; peephole z1 used #%2 directly instead of going through %1 using indirect addressing.
+ ld %4,a
+ ld %1,#%5
+}
+
+replace restart {
+ ld %1,#%2
+ ld %3 (%1),a
+%4:
+ ld %1,%5
+} by {
+ ld (#%2 + %3),a
+ ; peephole z2 directly used #%2 instead of going through %1 using indirect addressing.
+%4:
+ ld %1,%5
+}
+
+replace restart {
+ pop af
+ ld %1,#%2
+ ld %3 (%1),%4
+ ld %1,#%5
+} by {
+ ld a,%4
+ ld (#%2 + %3),a
+ ; peephole z3 used #%2 directly instead of going through %1 using indirect addressing.
+ pop af
+ ld %1,#%5
+} if operandsNotRelated(%3 'a')
+
+replace restart {
+ ld bc,#%1 + %2
+ ld a,%3
+ ld (bc),a
+} by {
+ ld a,%3
+ ld (#%1 + %2),a
+ ; peephole z4 directly used address #%1 + %2 instead of placing it in bc first.
+} if notUsed('bc')
+
+replace restart {
+ ld bc,#%1 + %2 + %6
+ ld a,%3
+ ld (bc),a
+} by {
+ ld a,%3
+ ld (#%1 + %2 + %6),a
+ ; peephole z5 directly used address #%1 + %2 + %6 instead of placing it in bc first.
+} if notUsed('bc')
+
+replace restart {
+ ld c,%1
+ ld l,c
+ ret
+} by {
+ ld l,%1
+ ; peephole z8 moved %1 directly into l instead of going through c.
+ ret
+}
+
+replace restart {
+ ld b,h
+ ld c,l
+ pop af
+ push bc
+ call %1
+} by {
+ ex (sp),hl
+ ; peephole z9 moved hl directly to the stack instead of going through bc.
+ call %1
+}
+
+replace restart {
+ ld d,h
+ ld e,l
+ pop af
+ push de
+ call %1
+} by {
+ ex (sp),hl
+ ; peephole z10 moved hl directly to the stack instead of going through de.
+ call %1
+}
+
+replace restart {
+ jp %5
+} by {
+ ret
+ ; peephole z11 replaced jump by return.
+} if labelIsReturnOnly(), labelRefCountChange(%5 -1)
+
+replace restart {
+ jp %1,%5
+} by {
+ ret %1
+ ; peephole z11a replaced jump by return.
+} if labelIsReturnOnly(), labelRefCountChange(%5 -1)
+
+// Should be one of the last ones. Opens the code to further peephole optimization.
+replace restart {
+%1:
+} by {
+ ; peephole z12 removed unused label %1.
+} if labelRefCount(%1 0)
+
+// Applying z11 or z11a followed by z12 will often leave a dead ret at the end of the function. Remove it.
+replace {
+ jp %5
+ ret
+} by {
+ jp %5
+ ; peephole z13 removed unused ret.
+}
+
+// These four need two be here since the peephole optimizer continues to apply rules further down the file even for replace restart rules.
+replace restart {
+ jp NC,%1
+ jp %2
+%1:
+} by {
+ jp C,%2
+ ; peephole 5' removed jp by using inverse jump logic
+%1:
+} if labelRefCountChange(%1 -1)
+
+replace restart {
+ jp C,%1
+ jp %2
+%1:
+} by {
+ jp NC,%2
+ ; peephole 5' removed jp by using inverse jump logic
+%1:
+} if labelRefCountChange(%1 -1)
+
+replace restart {
+ jp NZ,%1
+ jp %2
+%1:
+} by {
+ jp Z,%2
+ ; peephole 5' removed jp by using inverse jump logic
+%1:
+} if labelRefCountChange(%1 -1)
+
+replace restart {
+ jp Z,%1
+ jp %2
+%1:
+} by {
+ jp NZ,%2
+ ; peephole 6' removed jp by using inverse jump logic
+%1:
+} if labelRefCountChange(%1 -1)
+
+// These should be the last rules, so that the peepholes above need to look at jp only.
+replace {
+ jp %5
+} by {
+ jr %5
+ ; peephole z14 changed absolute to relative unconditional jump.
+} if labelInRange()
+
+replace {
+ jp Z,%5
+} by {
+ jr Z,%5
+ ; peephole z15 changed absolute to relative conditional jump.
+} if labelInRange()
+
+replace {
+ jp NZ,%5
+} by {
+ jr NZ,%5
+ ; peephole z16 changed absolute to relative conditional jump.
+} if labelInRange()
+
+replace {
+ jp C,%5
+} by {
+ jr C,%5
+ ; peephole z17 changed absolute to relative conditional jump.
+} if labelInRange()
+
+replace {
+ jp NC,%5
+} by {
+ jr NC,%5
+ ; peephole z18 changed absolute to relative conditional jump.
+} if labelInRange()