* Makefile.in, configure.in, configure,
[fw/sdcc] / src / mcs51 / peeph.def
index f6b05b3b53e59094796ef2cdf261cfd7e0add2b2..08f8b0225133a3b4c085e1fc078623e9cf47e292 100644 (file)
@@ -1,20 +1,3 @@
-//replace restart {
-//       pop  %1
-//       push %1
-//} by {
-//       ;       Peephole 1     removed pop %1 push %1 (not push pop)
-//}
-
-//replace restart {
-//       pop  %1
-//       mov  %2,%3
-//       push %1
-//} by {
-//       ;       Peephole 2     removed pop %1 push %1 (not push pop)
-//       mov  %2,%3
-//}
-
-//
 // added by Jean Louis VERN for
 // his shift stuff
 replace {
@@ -44,7 +27,7 @@ replace {
        mov     %1,a
 }
 
-replace {
+replace restart {
 // saving 1 byte, loosing 1 cycle but maybe allowing peephole 3.b to start
        mov     %1,#0x00
        mov     %2,#0x00
@@ -193,17 +176,63 @@ replace {
        movx    @dptr,a
 } if notVolatile %1
 
+// applies to f.e. lib/src/time.c (--model-large)
 replace {
        mov     a,%1
        movx    @dptr,a
        inc     dptr
        mov     a,%1
        movx    @dptr,a
+       inc     dptr
+       mov     a,%1
+       movx    @dptr,a
+       inc     dptr
+       mov     a,%1
+       movx    @dptr,a
 } by {
        mov     a,%1
        movx    @dptr,a
        inc     dptr
-       ;       Peephole 101    removed redundant mov
+       ;       Peephole 101.a  removed redundant moves
+       movx    @dptr,a
+       inc     dptr
+       movx    @dptr,a
+       inc     dptr
+       movx    @dptr,a
+} if notVolatile %1
+
+// applies to f.e. support/regression/tests/literalop.c (--model-large)
+replace {
+       mov     a,%1
+       movx    @dptr,a
+       inc     dptr
+       mov     a,%1
+       movx    @dptr,a
+       inc     dptr
+       mov     a,%1
+       movx    @dptr,a
+} by {
+       mov     a,%1
+       movx    @dptr,a
+       inc     dptr
+       ;       Peephole 101.b  removed redundant moves
+       movx    @dptr,a
+       inc     dptr
+       movx    @dptr,a
+} if notVolatile %1
+
+// applies to f.e. support/regression/tests/onebyte.c (--model-large)
+replace {
+       mov     a,%1
+       movx    @dptr,a
+       inc     dptr
+       mov     a,%1
+       movx    @dptr,a
+} by {
+       mov     a,%1
+       movx    @dptr,a
+       inc     dptr
+       ;       Peephole 101.c  removed redundant mov
        movx    @dptr,a
 } if notVolatile %1
 
@@ -215,19 +244,19 @@ replace {
 %3:
        mov     dpl,%1
 %7:
-       mov     sp,bp
-       pop     bp
+       mov     sp,_bp
+       pop     _bp
 } by {
-       ;       Peephole 102    removed redundant mov
+       ;       Peephole 102    removed redundant mov to %1
        mov     dpl,%2
        ljmp    %3
 %4:
        mov     dpl,%5
 %3:
 %7:
-       mov     sp,bp
-       pop     bp
-} if notVolatile %1
+       mov     sp,_bp
+       pop     _bp
+} if notVolatile(%1), labelRefCount(%3 1)
 
 replace {
        mov     %1,%2
@@ -237,37 +266,38 @@ replace {
 %3:
        mov     dpl,%1
 %7:
-       mov     sp,bp
-       pop     bp
+       mov     sp,_bp
+       pop     _bp
 } by {
-       ;       Peephole 103    removed redundant mov
+       ;       Peephole 103    removed redundant mov to %1
        mov     dpl,%2
        ljmp    %3
 %4:
        mov     dpl,%5
 %3:
 %7:
-       mov     sp,bp
-       pop     bp
-}
+       mov     sp,_bp
+       pop     _bp
+} if labelRefCount(%3 1)
 
-replace {
-       mov     a,bp
-       clr     c
-       add     a,#0x01
-       mov     r%1,a
-} by {
-       ;       Peephole 104    optimized increment (acc not set to r%1, flags undefined)
-       mov     r%1,bp
-       inc     r%1
-}
+// Does not seem to be triggered anymore
+//replace {
+//     mov     a,_bp
+//     clr     c
+//     add     a,#0x01
+//     mov     r%1,a
+//} by {
+//     ;       Peephole 104    optimized increment (acc not set to r%1, flags undefined)
+//     mov     r%1,_bp
+//     inc     r%1
+//}
 
 replace {
        mov     %1,a
        mov     a,%1
 } by {
        mov     %1,a
-;      Peephole 105    removed redundant mov
+       ;       Peephole 105    removed redundant mov
 } if notVolatile %1
 
 replace {
@@ -286,7 +316,7 @@ replace {
 } by {
        ;       Peephole 107    removed redundant ljmp
 %1:
-} labelRefCountChange(%1 -1)
+} if labelRefCountChange(%1 -1)
 
 replace {
        jc      %1
@@ -402,7 +432,7 @@ replace {
        ;       Peephole 115.b  jump optimization
        jz      %3
 %2:
-} labelRefCountChange(%2 -1)
+} if labelRefCountChange(%2 -1)
 
 replace {
        clr     a
@@ -582,11 +612,11 @@ replace {
 replace {
        push    psw
        mov     psw,%1
-       push    bp
-       mov     bp,%2
+       push    _bp
+       mov     _bp,%2
 %3:
-       mov     %2,bp
-       pop     bp
+       mov     %2,_bp
+       pop     _bp
        pop     psw
        ret
 } by {
@@ -684,7 +714,7 @@ replace {
        mov     a,%2
        add     a,#0xff - %1
        mov     %3,c
-}
+} if operandsLiteral(%1)
 
 replace {
        clr     c
@@ -696,7 +726,7 @@ replace {
        mov     a,%2
        add     a,#0xff - %1
        jnc     %5
-}
+} if operandsLiteral(%1)
 
 replace {
        clr     c
@@ -708,19 +738,20 @@ replace {
        mov     a,%2
        add     a,#0xff - %1
        jc      %5
-}
+} if operandsLiteral(%1)
 
-replace {
-       clr     c
-       mov     a,%1
-       subb    a,#%2
-       mov     %3,c
-} by {
-       ;       Peephole 132.d  optimized genCmpGt by inverse logic
-       mov     a,#0x100 - %2
-       add     a,%1
-       mov     %3,c
-} if operandsNotRelated('0x00' %2)
+// disabled. See bug1734654.c
+//replace {
+//     clr     c
+//     mov     a,%1
+//     subb    a,#%2
+//     mov     %3,c
+//} by {
+//     ;       Peephole 132.d  optimized genCmpGt by inverse logic
+//     mov     a,#0x100 - %2
+//     add     a,%1
+//     mov     %3,c
+//} if operandsNotRelated('0x00' %2), operandsLiteral(%2)
 
 replace {
        clr     c
@@ -732,7 +763,7 @@ replace {
        mov     a,#0x100 - %2
        add     a,%1
        jc      %5
-} if operandsNotRelated('0x00' %2)
+} if operandsNotRelated('0x00' %2), operandsLiteral(%2)
 
 replace {
        clr     c
@@ -744,7 +775,7 @@ replace {
        mov     a,#0x100 - %2
        add     a,%1
        jnc     %5
-} if operandsNotRelated('0x00' %2)
+} if operandsNotRelated('0x00' %2), operandsLiteral(%2)
 
 
 replace {
@@ -792,7 +823,7 @@ replace {
        mov     %1,a
        mov     dpl,%2
        mov     dph,%3
-       ;       Peephole 136    removed redundant move
+       ;       Peephole 136    removed redundant mov
 } if notVolatile %1
 
 // WTF? Doesn't look sensible to me...
@@ -844,7 +875,8 @@ replace {
 } by {
        ;       Peephole 139.b  removed redundant mov
        orl     a,%2
-       mov     r%1,a }
+       mov     r%1,a
+}
 
 replace {
        mov     r%1,a
@@ -856,12 +888,25 @@ replace {
        mov     r%1,a
 }
 
+// applies to genlshOne
+replace {
+       mov     ar%1,@%2
+       mov     a,r%1
+       add     a,acc
+       mov     r%1,a
+} by {
+       ;       Peephole 140    removed redundant mov
+       mov     a,@%2
+       add     a,@%2
+       mov     r%1,a
+}
+
 replace {
        mov     r%1,a
        mov     r%2,ar%1
        mov     ar%1,@r%2
 } by {
-       ;       Peephole 142    removed redundant moves
+       ;       Peephole 142    removed redundant mov
        mov     r%2,a
        mov     ar%1,@r%2
 }
@@ -1063,7 +1108,7 @@ replace {
        ;       Peephole 160.a  removed sjmp by inverse jump logic
        jc      %2
 %1:
-} labelRefCountChange(%1 -1)
+} if labelRefCountChange(%1 -1)
 
 replace {
        jc      %1
@@ -1073,7 +1118,7 @@ replace {
        ;       Peephole 160.b  removed sjmp by inverse jump logic
        jnc     %2
 %1:
-} labelRefCountChange(%1 -1)
+} if labelRefCountChange(%1 -1)
 
 replace {
        jnz     %1
@@ -1083,7 +1128,7 @@ replace {
        ;       Peephole 160.c  removed sjmp by inverse jump logic
        jz      %2
 %1:
-} labelRefCountChange(%1 -1)
+} if labelRefCountChange(%1 -1)
 
 replace {
        jz      %1
@@ -1093,7 +1138,7 @@ replace {
        ;       Peephole 160.d  removed sjmp by inverse jump logic
        jnz     %2
 %1:
-} labelRefCountChange(%1 -1)
+} if labelRefCountChange(%1 -1)
 
 replace {
        jnb     %3,%1
@@ -1103,7 +1148,7 @@ replace {
        ;       Peephole 160.e  removed sjmp by inverse jump logic
        jb      %3,%2
 %1:
-} labelRefCountChange(%1 -1)
+} if labelRefCountChange(%1 -1)
 
 replace {
        jb      %3,%1
@@ -1113,7 +1158,7 @@ replace {
        ;       Peephole 160.f  removed sjmp by inverse jump logic
        jnb     %3,%2
 %1:
-} labelRefCountChange(%1 -1)
+} if labelRefCountChange(%1 -1)
 
 replace {
        mov     %1,%2
@@ -1142,7 +1187,7 @@ replace {
        ;       Peephole 168    jump optimization
        jb      %1,%3
 %2:
-} labelRefCountChange(%2 -1)
+} if labelRefCountChange(%2 -1)
 
 replace {
        jb      %1,%2
@@ -1152,7 +1197,7 @@ replace {
        ;       Peephole 169    jump optimization
        jnb     %1,%3
 %2:
-} labelRefCountChange(%2 -1)
+} if labelRefCountChange(%2 -1)
 
 replace {
        clr     a
@@ -1270,9 +1315,9 @@ replace {
        mov     %1,%2
        mov     %1,%3
 } by {
-       ;       Peephole 177.c  removed redundant move
+       ;       Peephole 177.c  removed redundant mov
        mov     %1,%3
-} if notVolatile %1 %2
+} if notVolatile(%1 %2),operandsNotRelated(%1 %3)
 
 // applies to f.e. bug-408972.c
 // not before peephole 177.c
@@ -1283,24 +1328,23 @@ replace restart {
 } by {
        mov     %1,%2
        mov     %3,%4
-       ;       Peephole 177.d  removed redundant move
-} if notVolatile(%1 %2),operandsNotRelated(%1 %3)
+       ;       Peephole 177.d  removed redundant mov
+} if notVolatile(%1 %2),operandsNotRelated(%1 %2 %3)
 
 // applies to f.e. bug-607243.c
-// also check notVolatile %3, as it will return FALSE if it's @r%1
 replace {
-       mov     r%1,%2
-       mov     ar%1,%3
+       mov     %1,%2
+       mov     a%1,%3
 } by {
-       ;       peephole 177.e  removed redundant move
-       mov     ar%1,%3
-} if notVolatile %2 %3
+       ;       peephole 177.e  removed redundant mov %1,%2
+       mov     a%1,%3
+} if notVolatile(%2), operandsNotRelated(%1 %3)
 
 replace {
        mov     ar%1,%2
        mov     r%1,%3
 } by {
-       ;       peephole 177.f  removed redundant move
+       ;       peephole 177.f  removed redundant mov
        mov     r%1,%3
 } if notVolatile %2
 
@@ -1322,6 +1366,26 @@ replace {
        mov     %1,a
 } if notVolatile %2
 
+// applies to f.e. testfwk.c
+replace {
+       mov     r%1,a
+       mov     ar%2,r%1
+} by {
+       mov     r%1,a
+       ;       peephole 177.i  optimized mov sequence
+       mov     r%2,a
+}
+
+replace {
+       mov     r%1,%2
+       mov     ar%3,r%1
+       mov     r%1,%4
+} by {
+       ;       peephole 177.j  optimized mov sequence
+       mov     r%3,%2
+       mov     r%1,%4
+}
+
 replace {
        mov     a,%1
        mov     b,a
@@ -1439,17 +1503,18 @@ replace {
        mov     %1,a
 } if notVolatile %1
 
-replace {
-// acc being incremented might cause problems
-       mov     %1,a
-       inc     %1
-} by {
-       ;       Peephole 185    changed order of increment (acc incremented also!)
-       inc     a
-       mov     %1,a
-} if notVolatile %1
+//replace {
+// acc being incremented might cause problems with register tracking
+//     mov     %1,a
+//     inc     %1
+//} by {
+//     ;       Peephole 185    changed order of increment (acc incremented also!)
+//     inc     a
+//     mov     %1,a
+//} if notVolatile %1
 
-replace {
+// char indexed access to: long code table[] = {4,3,2,1};
+replace restart {
        add     a,#%1
        mov     dpl,a
        clr     a
@@ -1468,25 +1533,28 @@ replace {
        mov     %4,a
        inc     dptr
        clr     a
+       movc    a,@a+dptr
 } by {
        ;       Peephole 186.a  optimized movc sequence
+       mov     b,a
        mov     dptr,#%1
-       mov     b,acc
        movc    a,@a+dptr
        mov     %2,a
-       mov     acc,b
        inc     dptr
+       mov     a,b
        movc    a,@a+dptr
        mov     %3,a
-       mov     acc,b
        inc     dptr
+       mov     a,b
        movc    a,@a+dptr
        mov     %4,a
-       mov     acc,b
        inc     dptr
+       mov     a,b
+       movc    a,@a+dptr
 }
 
-replace {
+// char indexed access to: void* code table[] = {4,3,2,1};
+replace restart {
        add     a,#%1
        mov     dpl,a
        clr     a
@@ -1501,21 +1569,24 @@ replace {
        mov     %3,a
        inc     dptr
        clr     a
+       movc    a,@a+dptr
 } by {
        ;       Peephole 186.b  optimized movc sequence
+       mov     b,a
        mov     dptr,#%1
-       mov     b,acc
        movc    a,@a+dptr
        mov     %2,a
-       mov     acc,b
        inc     dptr
+       mov     a,b
        movc    a,@a+dptr
        mov     %3,a
-       mov     acc,b
        inc     dptr
+       mov     a,b
+       movc    a,@a+dptr
 }
 
-replace {
+// char indexed access to: int code table[] = {4,3,2,1};
+replace restart {
        add     a,#%1
        mov     dpl,a
        clr     a
@@ -1526,14 +1597,15 @@ replace {
        mov     %2,a
        inc     dptr
        clr     a
+       movc    a,@a+dptr
 } by {
        ;       Peephole 186.c  optimized movc sequence
+       mov     %2,a
        mov     dptr,#%1
-       mov     b,acc
        movc    a,@a+dptr
-       mov     %2,a
-       mov     acc,b
+       xch     a,%2
        inc     dptr
+       movc    a,@a+dptr
 }
 
 // char indexed access to: char code table[] = {4,3,2,1};
@@ -1638,15 +1710,26 @@ replace {
        ;       Peephole 191    removed redundant mov
 } if notVolatile %1
 
+// applies to f.e. regression/ports/mcs51/support.c
 replace {
        mov     r%1,a
        mov     @r%2,ar%1
 } by {
        mov     r%1,a
-       ;       Peephole 192    used a instead of ar%1 as source
+       ;       Peephole 192.a  used a instead of ar%1 as source
        mov     @r%2,a
 }
 
+// applies to f.e. printf_large.c
+replace {
+       mov     ar%1,@r%2
+       mov     a,r%1
+} by {
+       ;       Peephole 192.b  used a instead of ar%1 as destination
+       mov     a,@r%2
+       mov     r%1,a
+}
+
 replace {
        jnz     %3
        mov     a,%4
@@ -1669,7 +1752,7 @@ replace {
        cjne    %13,%14,%8
        sjmp    %7
 %3:
-} if labelRefCount(%3 4), labelRefCountChange(%3 -4), labelRefCountChange(%8 3)
+} if labelInRange(%8), labelRefCount(%3 4), labelRefCountChange(%3 -4), labelRefCountChange(%8 3)
 
 replace {
        cjne    %1,%2,%3
@@ -1693,7 +1776,7 @@ replace {
        cjne    %13,%14,%8
        sjmp    %7
 %3:
-} if labelRefCount(%3 4), labelRefCountChange(%3 -4), labelRefCountChange(%8 3)
+} if labelInRange(%8), labelRefCount(%3 4), labelRefCountChange(%3 -4), labelRefCountChange(%8 3)
 
 replace {
        cjne    @%1,%2,%3
@@ -1717,7 +1800,7 @@ replace {
        cjne    @%1,%14,%8
        sjmp    %7
 %3:
-} if labelRefCount(%3 4), labelRefCountChange(%3 -4), labelRefCountChange(%8 3)
+} if labelInRange(%8), labelRefCount(%3 4), labelRefCountChange(%3 -4), labelRefCountChange(%8 3)
 
 replace {
        cjne    %1,%2,%3
@@ -1735,7 +1818,7 @@ replace {
        cjne    %13,%14,%8
        sjmp    %7
 %3:
-} if labelRefCount(%3 4), labelRefCountChange(%3 -4), labelRefCountChange(%8 3)
+} if labelInRange(%8), labelRefCount(%3 4), labelRefCountChange(%3 -4), labelRefCountChange(%8 3)
 
 replace {
        jnz     %3
@@ -1755,7 +1838,7 @@ replace {
        cjne    %10,%11,%8
        sjmp    %7
 %3:
-} if labelRefCount(%3 3), labelRefCountChange(%3 -3), labelRefCountChange(%8 2)
+} if labelInRange(%8), labelRefCount(%3 3), labelRefCountChange(%3 -3), labelRefCountChange(%8 2)
 
 replace {
        cjne    %1,%2,%3
@@ -1775,7 +1858,7 @@ replace {
        cjne    %10,%11,%8
        sjmp    %7
 %3:
-} if labelRefCount(%3 3), labelRefCountChange(%3 -3), labelRefCountChange(%8 2)
+} if labelInRange(%8), labelRefCount(%3 3), labelRefCountChange(%3 -3), labelRefCountChange(%8 2)
 
 replace {
        cjne    @%1,%2,%3
@@ -1795,7 +1878,7 @@ replace {
        cjne    @%1,%11,%8
        sjmp    %7
 %3:
-} if labelRefCount(%3 3), labelRefCountChange(%3 -3), labelRefCountChange(%8 2)
+} if labelInRange(%8), labelRefCount(%3 3), labelRefCountChange(%3 -3), labelRefCountChange(%8 2)
 
 replace {
        cjne    %1,%2,%3
@@ -1811,7 +1894,7 @@ replace {
        cjne    %10,%11,%8
        sjmp    %7
 %3:
-} if labelRefCount(%3 3), labelRefCountChange(%3 -3), labelRefCountChange(%8 2)
+} if labelInRange(%8), labelRefCount(%3 3), labelRefCountChange(%3 -3), labelRefCountChange(%8 2)
 
 replace {
        jnz     %3
@@ -1827,7 +1910,7 @@ replace {
        cjne    %5,%6,%8
        sjmp    %7
 %3:
-} if labelRefCount(%3 2), labelRefCountChange(%3 -2), labelRefCountChange(%8 1)
+} if labelInRange(%8), labelRefCount(%3 2), labelRefCountChange(%3 -2), labelRefCountChange(%8 1)
 
 replace {
        cjne    %1,%2,%3
@@ -1843,7 +1926,7 @@ replace {
        cjne    %5,%6,%8
        sjmp    %7
 %3:
-} if labelRefCount(%3 2), labelRefCountChange(%3 -2), labelRefCountChange(%8 1)
+} if labelInRange(%8), labelRefCount(%3 2), labelRefCountChange(%3 -2), labelRefCountChange(%8 1)
 
 replace {
        cjne     @%1,%2,%3
@@ -1859,7 +1942,7 @@ replace {
        cjne    @%1,%6,%8
        sjmp    %7
 %3:
-} if labelRefCount(%3 2), labelRefCountChange(%3 -2), labelRefCountChange(%8 1)
+} if labelInRange(%8), labelRefCount(%3 2), labelRefCountChange(%3 -2), labelRefCountChange(%8 1)
 
 replace {
        cjne    %1,%2,%3
@@ -1873,7 +1956,7 @@ replace {
        cjne    %5,%6,%8
        sjmp    %7
 %3:
-} if labelRefCount(%3 2), labelRefCountChange(%3 -2), labelRefCountChange(%8 1)
+} if labelInRange(%8), labelRefCount(%3 2), labelRefCountChange(%3 -2), labelRefCountChange(%8 1)
 
 replace {
        cjne    %1,%2,%3
@@ -1885,7 +1968,7 @@ replace {
        cjne    %1,%2,%5
        sjmp    %4
 %3:
-} if labelRefCount(%3 1), labelRefCountChange(%3 -1)
+} if labelInRange(%5), labelRefCount(%3 1), labelRefCountChange(%3 -1)
 
 replace {
        sjmp    %1
@@ -1893,7 +1976,7 @@ replace {
 } by {
        ;       Peephole 200.a  removed redundant sjmp
 %1:
-} labelRefCountChange(%1 -1)
+} if labelRefCountChange(%1 -1)
 
 replace {
        sjmp    %1
@@ -1903,7 +1986,7 @@ replace {
        ;       Peephole 200.b  removed redundant sjmp
 %2:
 %1:
-} labelRefCountChange(%1 -1)
+} if labelRefCountChange(%1 -1)
 
 replace {
        push    acc
@@ -1950,16 +2033,17 @@ replace {
        mov     %1,%1
 } by {
        ;       Peephole 206    removed redundant mov %1,%1
-} if notVolatile
+} if notVolatile %1
 
-replace {
-       mov     a,_bp
-       add     a,#0x00
-       mov     %1,a
-} by {
-       ;       Peephole 207    removed zero add (acc not set to %1, flags undefined)
-       mov     %1,_bp
-}
+// Does not seem to be triggered anymore
+//replace {
+//     mov     a,_bp
+//     add     a,#0x00
+//     mov     %1,a
+//} by {
+//     ;       Peephole 207    removed zero add (acc not set to %1, flags undefined)
+//     mov     %1,_bp
+//}
 
 replace {
        push    acc
@@ -1970,16 +2054,17 @@ replace {
        mov     r%1,_bp
 }
 
-replace {
-       mov     a,_bp
-       add     a,#0x00
-       inc     a
-       mov     %1,a
-} by {
-       ;       Peephole 209    optimized increment (acc not set to %1, flags undefined)
-       mov     %1,_bp
-       inc     %1
-}
+// Does not seem to be triggered anymore
+//replace {
+//     mov     a,_bp
+//     add     a,#0x00
+//     inc     a
+//     mov     %1,a
+//} by {
+//     ;       Peephole 209    optimized increment (acc not set to %1, flags undefined)
+//     mov     %1,_bp
+//     inc     %1
+//}
 
 replace {
        mov     dptr,#((((%1 >> 8)) <<8) + %1)
@@ -1995,15 +2080,16 @@ replace {
        ;       Peephole 211    removed redundant push %1 pop %1
 }
 
-replace {
-       mov     a,_bp
-       add     a,#0x01
-       mov     r%1,a
-} by {
-       ;       Peephole 212    reduced add sequence to inc
-       mov     r%1,_bp
-       inc     r%1
-}
+// Does not seem to be triggered anymore
+//replace {
+//     mov     a,_bp
+//     add     a,#0x01
+//     mov     r%1,a
+//} by {
+//     ;       Peephole 212    reduced add sequence to inc
+//     mov     r%1,_bp
+//     inc     r%1
+//}
 
 // reverts peephole 159? asx8051 cannot handle, too complex?
 replace {
@@ -2029,7 +2115,7 @@ replace    {
        add     a,%1
 } by {
        mov     %1,a
-       ;       Peephole 214    reduced some extra moves
+       ;       Peephole 214.a  removed redundant mov
        add     a,%2
 } if operandsNotSame
 
@@ -2038,7 +2124,7 @@ replace {
        add     a,%2
        mov     %1,a
 } by {
-       ;       Peephole 215    removed some moves
+       ;       Peephole 214.b  removed redundant mov
        add     a,%2
        mov     %1,a
 } if operandsNotSame
@@ -2157,13 +2243,13 @@ replace {
 replace {
        mov     %1 + %2,(%2 + %1)
 } by {
-       ;       Peephole 221.a  remove redundant move
+       ;       Peephole 221.a  remove redundant mov
 } if notVolatile
 
 replace {
        mov     (%1 + %2 + %3),((%2 + %1) + %3)
 } by {
-       ;       Peephole 221.b  remove redundant move
+       ;       Peephole 221.b  remove redundant mov
 } if notVolatile
 
 replace {
@@ -2211,6 +2297,18 @@ replace {
        ;       Peephole 225    removed redundant move to acc
 } if notVolatile %1
 
+replace {
+       clr     a
+       movx    @%1,a
+       inc     %1
+       clr     a
+} by {
+       clr     a
+       movx    @%1,a
+       inc     %1
+       ;       Peephole 226.a  removed unnecessary clr
+}
+
 replace {
        clr     a
        movx    @%1,a
@@ -2224,7 +2322,7 @@ replace {
        inc     %1
        movx    @%1,a
        inc     %1
-       ;       Peephole 226    removed unnecessary clr
+       ;       Peephole 226.b  removed unnecessary clr
 }
 
 replace {
@@ -2482,7 +2580,7 @@ replace {
        mov     %3,%4
 %1:
        ret
-} labelRefCountChange(%1 -1)
+} if labelRefCountChange(%1 -1)
 
 replace {
        sjmp    %1
@@ -2501,7 +2599,7 @@ replace {
        mov     dph,%6
 %1:
        ret
-} labelRefCountChange(%1 -1)
+} if labelRefCountChange(%1 -1)
 
 // applies to f.e. device/lib/log10f.c
 replace {
@@ -2530,7 +2628,7 @@ replace {
        mov     %7,%15
        mov     %8,%16
        ;       Peephole 238.a  removed 4 redundant moves
-} if operandsNotSame8 %1 %2 %3 %4 %5 %6 %7 %8
+} if operandsNotSame8(%1 %2 %3 %4 %5 %6 %7 %8), notVolatile(%1 %2 %3 %4 %9 %10 %11 %12)
 
 // applies to device/lib/log10f.c
 replace {
@@ -2548,7 +2646,7 @@ replace {
        mov     %3,%7
        mov     %4,%8
        ;       Peephole 238.b  removed 3 redundant moves
-} if operandsNotSame7 %1 %2 %3 %4 %5 %6 %7
+} if operandsNotSame7(%1 %2 %3 %4 %5 %6 %7), notVolatile(%1 %2 %3 %5 %6 %7)
 
 // applies to f.e. device/lib/time.c
 replace {
@@ -2567,7 +2665,7 @@ replace {
        mov     %3,%7
        mov     %4,%8
        ;       Peephole 238.c  removed 2 redundant moves
-} if operandsNotSame4 %1 %2 %3 %4
+} if operandsNotSame4(%1 %2 %3 %4), notVolatile(%1 %2 %5 %6)
 
 // applies to f.e. support/regression/tests/bug-524209.c
 replace {
@@ -2583,7 +2681,7 @@ replace {
        mov     %2,%5
        mov     %3,%6
        ;       Peephole 238.d  removed 3 redundant moves
-} if operandsNotSame6 %1 %2 %3 %4 %5 %6
+} if operandsNotSame6(%1 %2 %3 %4 %5 %6), notVolatile(%1 %2 %3 %4 %5 %6)
 
 // applies to f.e. ser_ir.asm
 replace {
@@ -2602,133 +2700,180 @@ replace restart {
        addc    a,%1
 }
 
-// peepholes 241.a to 241.c and 241.d to 241.f need to be in order
+// peepholes 241.a to 241.d and 241.e to 241.h need to be in order
 replace {
-       cjne    r%1,#%2,%3
-       cjne    r%4,#%5,%3
-       cjne    r%6,#%7,%3
-       cjne    r%8,#%9,%3
+       cjne    r%2,#%3,%0
+       cjne    r%4,#%5,%0
+       cjne    r%6,#%7,%0
+       cjne    r%8,#%9,%0
        mov     a,#0x01
-       sjmp    %10
-%3:
+       sjmp    %1
+%0:
        clr     a
-%10:
+%1:
 } by {
        ;       Peephole 241.a  optimized compare
        clr     a
-       cjne    r%1,#%2,%3
-       cjne    r%4,#%5,%3
-       cjne    r%6,#%7,%3
-       cjne    r%8,#%9,%3
+       cjne    r%2,#%3,%0
+       cjne    r%4,#%5,%0
+       cjne    r%6,#%7,%0
+       cjne    r%8,#%9,%0
        inc     a
-%3:
-%10:
-} labelRefCountChange(%10 -1)
+%0:
+%1:
+} if labelRefCountChange(%1 -1)
 
-// applies to f.e. time.c
+// applies to generic pointer compare
 replace {
-       cjne    r%1,#%2,%3
-       cjne    r%4,#%5,%3
+       cjne    r%2,#%3,%0
+       cjne    r%4,#%5,%0
+       cjne    r%6,#%7,%0
        mov     a,#0x01
-       sjmp    %6
-%3:
+       sjmp    %1
+%0:
        clr     a
-%6:
+%1:
 } by {
        ;       Peephole 241.b  optimized compare
        clr     a
-       cjne    r%1,#%2,%3
-       cjne    r%4,#%5,%3
+       cjne    r%2,#%3,%0
+       cjne    r%4,#%5,%0
+       cjne    r%6,#%7,%0
        inc     a
-%3:
-%6:
-} labelRefCountChange(%6 -1)
+%0:
+%1:
+} if labelRefCountChange(%1 -1)
 
-// applies to f.e. malloc.c
+// applies to f.e. time.c
 replace {
-       cjne    r%1,#%2,%3
+       cjne    r%2,#%3,%0
+       cjne    r%4,#%5,%0
        mov     a,#0x01
-       sjmp    %4
-%3:
+       sjmp    %1
+%0:
        clr     a
-%4:
+%1:
 } by {
        ;       Peephole 241.c  optimized compare
        clr     a
-       cjne    r%1,#%2,%3
+       cjne    r%2,#%3,%0
+       cjne    r%4,#%5,%0
        inc     a
-%3:
-%4:
-} labelRefCountChange(%4 -1)
+%0:
+%1:
+} if labelRefCountChange(%1 -1)
+
+// applies to f.e. malloc.c
+replace {
+       cjne    r%2,#%3,%0
+       mov     a,#0x01
+       sjmp    %1
+%0:
+       clr     a
+%1:
+} by {
+       ;       Peephole 241.d  optimized compare
+       clr     a
+       cjne    r%2,#%3,%0
+       inc     a
+%0:
+%1:
+} if labelRefCountChange(%1 -1)
 
 // applies to f.e. j = (k!=0x1000);
 // with volatile idata long k;
 replace {
-       cjne    @r%1,#%2,%3
-       inc     r%1
-       cjne    @r%1,#%4,%3
-       inc     r%1
-       cjne    @r%1,#%5,%3
-       inc     r%1
-       cjne    @r%1,#%6,%3
+       cjne    @r%0,#%3,%1
+       inc     r%0
+       cjne    @r%0,#%4,%1
+       inc     r%0
+       cjne    @r%0,#%5,%1
+       inc     r%0
+       cjne    @r%0,#%6,%1
        mov     a,#0x01
-       sjmp    %7
-%3:
+       sjmp    %2
+%1:
        clr     a
-%7:
+%2:
 } by {
-       ;       Peephole 241.d  optimized compare
+       ;       Peephole 241.e  optimized compare
        clr     a
-       cjne    @r%1,#%2,%3
-       inc     r%1
-       cjne    @r%1,#%4,%3
-       inc     r%1
-       cjne    @r%1,#%5,%3
-       inc     r%1
-       cjne    @r%1,#%6,%3
+       cjne    @r%0,#%3,%1
+       inc     r%0
+       cjne    @r%0,#%4,%1
+       inc     r%0
+       cjne    @r%0,#%5,%1
+       inc     r%0
+       cjne    @r%0,#%6,%1
        inc     a
-%3:
-%7:
-} labelRefCountChange(%7 -1)
+%1:
+%2:
+} if labelRefCountChange(%2 -1)
+
+// applies to f.e. j = (p!=NULL);
+// with volatile idata char *p;
+replace {
+       cjne    @r%0,#%3,%1
+       inc     r%0
+       cjne    @r%0,#%4,%1
+       inc     r%0
+       cjne    @r%0,#%5,%1
+       mov     a,#0x01
+       sjmp    %2
+%1:
+       clr     a
+%2:
+} by {
+       ;       Peephole 241.f  optimized compare
+       clr     a
+       cjne    @r%0,#%3,%1
+       inc     r%0
+       cjne    @r%0,#%4,%1
+       inc     r%0
+       cjne    @r%0,#%5,%1
+       inc     a
+%1:
+%2:
+} if labelRefCountChange(%2 -1)
 
 // applies to f.e. j = (k!=0x1000);
 // with volatile idata int k;
 replace {
-       cjne    @r%1,#%2,%3
-       inc     r%1
-       cjne    @r%1,#%4,%3
+       cjne    @r%0,#%3,%1
+       inc     r%0
+       cjne    @r%0,#%4,%1
        mov     a,#0x01
-       sjmp    %7
-%3:
+       sjmp    %2
+%1:
        clr     a
-%7:
+%2:
 } by {
-       ;       Peephole 241.e  optimized compare
+       ;       Peephole 241.g  optimized compare
        clr     a
-       cjne    @r%1,#%2,%3
-       inc     r%1
-       cjne    @r%1,#%4,%3
+       cjne    @r%0,#%3,%1
+       inc     r%0
+       cjne    @r%0,#%4,%1
        inc     a
-%3:
-%7:
-} labelRefCountChange(%7 -1)
+%1:
+%2:
+} if labelRefCountChange(%2 -1)
 
 // applies to f.e. vprintf.asm (--stack-auto)
 replace {
-       cjne    @r%1,#%2,%3
+       cjne    @r%0,#%3,%1
        mov     a,#0x01
-       sjmp    %7
-%3:
+       sjmp    %2
+%1:
        clr     a
-%7:
+%2:
 } by {
-       ;       Peephole 241.f  optimized compare
+       ;       Peephole 241.h  optimized compare
        clr     a
-       cjne    @r%1,#%2,%3
+       cjne    @r%0,#%3,%1
        inc     a
-%3:
-%7:
-} labelRefCountChange(%7 -1)
+%1:
+%2:
+} if labelRefCountChange(%2 -1)
 
 // applies to f.e. scott-bool1.c
 replace {
@@ -3042,8 +3187,6 @@ replace {
 } if notVolatile %1
 
 
-
-
 // rules 247.x apply to f.e. bitfields.c
 replace {
        mov     r%5,#%1
@@ -3600,37 +3743,27 @@ replace {
        mov     r%3,a
 }
 
-// applies to: void test( char c ) { if( c ) func1(); else func2(); }
-replace {
-       lcall   %1
-       ret
-} by {
-       ;       Peephole 253.a  replaced lcall/ret with ljmp
-       ljmp    %1
-}
-
-// applies to: void test( char c ) { if( c ) func1(); else func2(); }
+// unsigned char i=8; do{ } while(--i != 0);
+// this applies if i is kept in a register
 replace {
-       lcall   %1
-%2:
-       ret
+       dec     %1
+       cjne    %1,#0x00,%2
 } by {
-       ;       Peephole 253.b  replaced lcall/ret with ljmp
-       ljmp    %1
-       ;
-} if labelRefCount %2 0
+       ;       Peephole 253.a  optimized decrement with compare
+       djnz    %1,%2
+} if notVolatile(%1)
 
-// applies to f.e. scott-bool1.c
+// unsigned char i=8; do{ } while(--i != 0);
+// this applies if i is kept in data memory
+// must come before 256, see bug 1721024
 replace {
-       lcall   %1
-%2:
-       ret
+       dec     %1
+       mov     a,%1
+       jnz     %2
 } by {
-       ;       Peephole 253.c  replaced lcall with ljmp
-       ljmp    %1
-%2:
-       ret
-}
+       ;       Peephole 253.b  optimized decrement with compare
+       djnz    %1,%2
+} if notVolatile(%1), operandsNotRelated(%1 '@r0' '@r1')
 
 
 // applies to f.e. funptrs.c
@@ -3751,17 +3884,6 @@ replace {
 } if labelRefCount(%2 0),operandsNotRelated('a' %4 %6 %8)
 
 
-// unsigned char i=8; do{ } while(--i != 0);
-// this currently only applies if i is kept in a register
-replace {
-       dec     %1
-       cjne    %1,#0x00,%2
-} by {
-       ;       Peephole 257    optimized decrement with compare
-       djnz    %1,%2
-} if notVolatile %1
-
-
 // in_byte<<=1; if(in_bit) in_byte|=1;
 // helps f.e. reading data on a 3-wire (SPI) bus
 replace {
@@ -3799,7 +3921,7 @@ replace {
        mov     r%1,a
 %4:
 %3:
-} labelRefCountChange(%3 -1)
+} if labelRefCountChange(%3 -1)
 
 // in_byte>>=1; if(in_bit) in_byte|=0x80;
 replace {
@@ -3839,7 +3961,7 @@ replace {
        mov     r%1,a
 %4:
 %3:
-} labelRefCountChange(%3 -1)
+} if labelRefCountChange(%3 -1)
 
 // out_bit=out_byte&0x80; out_byte<<=1;
 // helps f.e. writing data on a 3-wire (SPI) bus
@@ -3876,16 +3998,16 @@ replace {
        mov     %1,a
 } if notVolatile %1
 
-// Peepholes 259.x rely on the correct labelRefCount. Otherwise they are 
+// Peepholes 259.x rely on the correct labelRefCount. Otherwise they are
 // not compatible with peepholes 250.x
-// Peepholes 250.x add jumps to a previously unused label. If the 
+// Peepholes 250.x add jumps to a previously unused label. If the
 // labelRefCount is not increased, peepholes 259.x are (mistakenly) applied.
 // (Mail on sdcc-devel 2004-10-25)
 //
 // applies to f.e. vprintf.c
 replace {
        sjmp    %1
-%2:    
+%2:
        ret
 } by {
        sjmp    %1
@@ -3896,7 +4018,7 @@ replace {
 // applies to f.e. gets.c
 replace {
        ljmp    %1
-%2:    
+%2:
        ret
 } by {
        ljmp    %1
@@ -4421,9 +4543,216 @@ replace {
        mov     %1,a
 }
 
+replace {
+       cpl     c
+       cpl     c
+} by {
+       ;       Peephole 262    removed redundant cpl c
+}
+
+replace {
+       mov     %1,#%2
+       inc     %1
+       inc     %1
+       inc     %1
+} by {
+       ;       Peephole 263.a  optimized loading const
+       mov     %1,#(%2 + 3)
+} if notVolatile(%1)
+
+replace {
+       mov     %1,#%2
+       inc     %1
+       inc     %1
+} by {
+       ;       Peephole 263.b  optimized loading const
+       mov     %1,#(%2 + 2)
+} if notVolatile(%1)
+
+replace {
+       mov     %1,#%2
+       inc     %1
+} by {
+       ;       Peephole 263.c  optimized loading const
+       mov     %1,#(%2 + 1)
+} if notVolatile(%1)
+
+
+replace {
+       clr     a
+       cjne    %1,%2,%3
+       inc     a
+%3:
+       jz      %4
+} by {
+       ;       Peephole 264    jump optimization (acc not set)
+       cjne    %1,%2,%4
+%3:
+} if labelRefCount(%3 1), labelRefCountChange(%3 -1)
+
+
+replace {
+       mov     %1,c
+       cpl     %1
+} by {
+       ;       Peephole 265    optimized mov/cpl sequence (carry differs)
+       cpl     c
+       mov     %1,c
+} if notVolatile(%1)
+
+replace {
+       mov     %1,c
+       jb      %1,%2
+} by {
+       ;       Peephole 266.a  optimized mov/jump sequence
+       mov     %1,c
+        jc     %2
+} if notVolatile(%1)
+
+replace {
+       mov     %1,c
+       jnb     %1,%2
+} by {
+       ;       Peephole 266.b  optimized mov/jump sequence
+       mov     %1,c
+        jnc    %2
+} if notVolatile(%1)
+
+replace {
+       jnc     %1
+       setb    %2
+       sjmp    %3
+%1:
+       clr     %2
+%3:
+} by {
+       ;       Peephole 267.a  optimized mov bit sequence
+       mov     %2,c
+%1:
+%3:
+} if labelRefCount(%1 1), labelRefCountChange(%1 -1), labelRefCountChange(%3 -1)
+
+replace {
+       jc      %1
+       clr     %2
+       sjmp    %3
+%1:
+       setb    %2
+%3:
+} by {
+       ;       Peephole 267.b  optimized mov bit sequence
+       mov     %2,c
+%1:
+%3:
+} if labelRefCount(%1 1), labelRefCountChange(%1 -1), labelRefCountChange(%3 -1)
+
+replace {
+       mov     %1,c
+       mov     %1,c
+} by {
+       ;       Peephole 268    removed redundant mov
+       mov     %1,c
+} if notVolatile(%1)
+
+replace {
+       mov     %1,c
+       mov     c,%1
+} by {
+       ;       Peephole 269    removed redundant mov
+       mov     %1,c
+} if notVolatile(%1)
+
+//accessing struct/array on stack
+replace {
+       add     a,#%1
+       add     a,#%2
+} by {
+       ;       Peephole 270    removed redundant add
+       add     a,#%1+%2
+}
+
+replace {
+       jz      %1
+       mov     %2,%4
+       sjmp    %3
+%1:
+       mov     %2,#0x00
+%3:
+} by {
+       jz      %1
+       ;       Peephole 271    optimized ternary operation (acc different)
+       mov     a,%4
+%1:
+       mov     %2,a
+%3:
+} if operandsNotRelated('a' 'dptr' %2), labelRefCount(%1 1), labelRefCountChange(%3 -1)
+
+
+replace restart {
+       pop     ar%1
+} by {
+       ;       Peephole 300    pop ar%1 removed
+} if deadMove(%1)
+
+replace {
+       mov     r%1,%2
+} by {
+       ;       Peephole 301    mov r%1,%2 removed
+} if notVolatile(%2), deadMove(%1)
+
+
+// applies to: void test( char c ) { if( c ) func1(); else func2(); }
+replace {
+       lcall   %1
+       ret
+} by {
+       ;       Peephole 400.a  replaced lcall/ret with ljmp
+       ljmp    %1
+}
+
+// applies to: void test( char c ) { if( c ) func1(); else func2(); }
+replace {
+       lcall   %1
+%2:
+       ret
+} by {
+       ;       Peephole 400.b  replaced lcall/ret with ljmp
+       ljmp    %1
+       ;
+} if labelRefCount %2 0
+
+// applies to f.e. scott-bool1.c
+replace {
+       lcall   %1
+%2:
+       ret
+} by {
+       ;       Peephole 400.c  replaced lcall with ljmp
+       ljmp    %1
+%2:
+       ret
+}
+
+// for programs less than 2k
+replace {
+       lcall   %1
+} by {
+       ;       Peephole 400.d  replaced lcall with acall
+       acall   %1
+} if useAcallAjmp
+
+// for programs less than 2k
+replace {
+       ljmp    %1
+} by {
+       ;       Peephole 400.e  replaced ljmp with ajmp
+       ajmp    %1
+} if useAcallAjmp
+
+
 // should be one of the last peepholes
 replace{
 %1:
 } by {
-       ;       Peephole 300    removed redundant label %1
+       ;       Peephole 500    removed redundant label %1
 } if labelRefCount(%1 0)