Fixed bugs #2728224, #2728218
[fw/sdcc] / src / z80 / peeph-z80.def
index c50c48e6ee548fa32898de7492986feb168a6285..cf5a83d4f7d1ec66a522cb456f06b6a85c0f6447 100644 (file)
@@ -163,10 +163,20 @@ replace restart {
        ex      de,hl
        push    de
 } by {
-       ; peephole 0k' pushed de directly instead of going through hl.
+       ; peephole 0k' pushed hl directly instead of going through de.
        push    hl
 } if notUsed('de'), notUsed('hl')
 
+replace restart {
+       ld      l,%1
+       ld      h,d
+       push    hl
+} by {
+       ; peephole 0k'' pushed de instead of hl removing a load.
+       ld      e,%1
+       push    de
+} if notUsed('hl'), notUsed('e')
+
 replace restart {
        ex      de,hl
        push    bc
@@ -186,6 +196,16 @@ replace restart {
        push    bc
 } if notUsed('hl')
 
+replace restart {
+       ld      l,%1
+       ld      h,b
+       push    hl
+} by {
+       ; peephole 0m' pushed bc instead of hl removing a load.
+       ld      c,%1
+       push    bc
+} if notUsed('hl'), notUsed('c')
+
 replace restart {
        ld      c,a
        push    de
@@ -239,16 +259,17 @@ replace restart {
        ld      b, %2 (%3)
 } if notVolatile(%1), notUsed(%1)
 
-replace restart {
-       ld      %1,a
-       ld      %2,%3
-       ld      %4,%1
-} by {
-       ; peephole 0t loaded %4 from a instead of going through %1.
-       ld      %1,a
-       ld      %2,%3
-       ld      %4,a
-} if notVolatile(%1), operandsNotRelated(%1 %3), operandsNotRelated(%1 %2)
+// Bug #2728445
+//replace restart {
+//     ld      %1,a
+//     ld      %2,%3
+//     ld      %4,%1
+//} by {
+//     ; peephole 0t loaded %4 from a instead of going through %1.
+//     ld      %1,a
+//     ld      %2,%3
+//     ld      %4,a
+//} if notVolatile(%1), operandsNotRelated(%1 %3), operandsNotRelated(%1 %2)
 
 replace restart {
        ld      %1,a
@@ -839,7 +860,25 @@ replace restart {
        //add   %3, %4
 } by {
        add     a, a
-       ; peephole 42b shifts in accumulator insted of %1
+       ; 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
@@ -998,17 +1037,18 @@ replace restart {
 //     ; peephole 50 shortened or using a (which has just been tested to be #0x00).
 //} if operandsNotSame3(%3 '(bc)' '(de)')
 
-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).
-}
+// 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 {
@@ -1245,6 +1285,28 @@ replace restart {
        ; 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