+
+replace restart {
+ xor a,%1
+ or a,a
+} by {
+ xor a,%1
+ ; peephole 21 removed redundant or after xor.
+}
+
+replace restart {
+ ld %1,a
+ and a,%2
+ ld %1,a
+} by {
+ ; peephole 22 removed redundant load into %1.
+ and a,%2
+ ld %1,a
+} if notVolatile(%1)
+
+replace {
+ ld %1,%2
+ ld a,%2
+} by {
+ ld a,%2
+ ld %1,a
+ ; peephole 23 load value in a first and use it next
+} if notVolatile(%1 %2)
+
+replace restart {
+ ld %1,%2
+ ld %3,%4
+ ld %2,%1
+ ld %4,%3
+} by {
+ ld %1,%2
+ ld %3,%4
+ ; peephole 24 removed redundant load from %3%1 into %4%2
+} if notVolatile(%1 %2 %3 %4)
+
+replace restart {
+ ld b,%1
+ ld a,b
+ pop bc
+} by {
+ ld a,%1
+ ; peephole 25 removed load into b
+ pop bc
+}
+
+replace restart {
+ ld c,%1
+ ld a,c
+ pop bc
+} by {
+ ld a,%1
+ ; peephole 26 removed load into c
+ pop bc
+}
+
+replace restart {
+ ld d,%1
+ ld a,d
+ pop de
+} by {
+ ld a,%1
+ ; peephole 27 removed load into d
+ pop de
+}
+
+replace restart {
+ ld e,%1
+ ld a,e
+ pop de
+} by {
+ ld a,%1
+ ; peephole 28 removed load into e
+ pop de
+}
+
+replace restart {
+ ld h,%1
+ ld a,h
+ pop hl
+} by {
+ ld a,%1
+ ; peephole 29 removed load into h
+ pop hl
+}
+
+replace restart {
+ ld l,%1
+ ld a,l
+ pop hl
+} by {
+ ld a,%1
+ ; peephole 30 removed load into l
+ pop hl
+}
+
+replace restart {
+ ld a,c
+ push af
+ inc sp
+ ld a,#%2
+ push af
+ inc sp
+ call %3
+} by {
+ ld b,c
+ ld c,#%2
+ push bc
+ ; peephole 31 moved and pushed arguments c and #%2 through bc instead of pushing them individually.
+ call %3
+}
+
+replace restart {
+ ld a,e
+ push af
+ inc sp
+ ld a,#%2
+ push af
+ inc sp
+ call %3
+} by {
+ ld d,e
+ ld e,#%2
+ push de
+ ; peephole 32 moved and pushed arguments e and #%2 through de instead of pushing them individually.
+ call %3
+}
+
+replace restart {
+ ld a,%1
+ sub a,%2
+ jp %3,%4
+ ld a,%1
+} by {
+ ld a,%1
+ cp a,%2
+ jp %3,%4
+ ; peephole 33 removed load by replacing sub with cp
+ assert a=%1
+} if notVolatile(%1)
+
+replace restart {
+ assert a=%1
+ sub a,%2
+ jp %3,%4
+ ld a,%1
+} by {
+ cp a,#%2
+ jp %3,%4
+ ; peephole 34 removed load by replacing sub with cp
+ assert a=%1
+}
+
+replace restart {
+ assert a=%1
+} by {
+}
+
+replace restart {
+ sub a,#0xFF
+ jp Z,%1
+} by {
+ inc a
+ ; peephole 35 replaced sub a,#0xFF by inc a.
+ jp Z,%1
+}
+
+replace restart {
+ sub a,#0xFF
+ jp NZ,%1
+} by {
+ inc a
+ ; peephole 36 replaced sub a,#0xFF by inc a.
+ jp NZ,%1
+}
+
+replace restart {
+ ld bc,#%1 + %2
+ ld a,c
+ add a,%3
+ ld c,a
+ ld a,b
+ adc a,%4
+ ld b,a
+} by {
+ ld a,#<(%1 + %2)
+ add a,%3
+ ld c,a
+ ld a,#>(%1 + %2)
+ ; peephole 37 directly used (%1 + %2) in calculation instead of placing it in bc first.
+ adc a,%4
+ ld b,a
+}
+
+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
+ ld %1,a
+} by {
+ ; peephole 42 removed loads by exploiting commutativity of addition.
+ add a,%2
+ ld %1,a
+} if notVolatile(%1)
+
+// 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 42a shifts in accumulator insted of %1
+ ld %1, a
+ ld a, %2
+ //add %3, %4
+}
+
+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
+}
+