5 // ; Peephole 1 removed pop %1 push %1 (not push pop)
13 // ; Peephole 2 removed pop %1 push %1 (not push pop)
18 // added by Jean Louis VERN for
24 ; Peephole 2.a removed redundant xch xch
32 ; Peephole 3.a changed mov to clr
42 ; Peephole 3.b changed mov to clr
48 // saving 1 byte, loosing 1 cycle but maybe allowing peephole 3.b to start
53 ; Peephole 3.c changed mov to clr
66 ; Peephole 100 removed redundant mov
79 ; Peephole 101 removed redundant mov
97 ; Peephole 102 removed redundant mov
119 ; Peephole 103 removed redundant mov
136 ; Peephole 104 optimized increment (acc not set to r%1, flags undefined)
145 ; Peephole 105 removed redundant mov
154 ; Peephole 106 removed redundant mov
163 ; Peephole 107 removed redundant ljmp
172 ; Peephole 108 removed ljmp by inverse jump logic
182 ; Peephole 109 removed ljmp by inverse jump logic
192 ; Peephole 110 removed ljmp by inverse jump logic
202 ; Peephole 111 removed ljmp by inverse jump logic
212 ; Peephole 112.a removed ljmp by inverse jump logic
221 ; Peephole 112.b changed ljmp to sjmp
235 ; Peephole 113 optimized misc sequence
240 } if labelRefCount %3 1
251 ; Peephole 114 optimized misc sequence
257 } if labelRefCount %3 2
266 ; Peephole 115 jump optimization
270 } if labelRefCount %3 1
280 ; Peephole 116 jump optimization
285 } if labelRefCount %3 2
296 ; Peephole 117 jump optimization
302 } if labelRefCount %3 3
314 ; Peephole 118 jump optimization
321 } if labelRefCount %3 4
330 ; Peephole 119 jump optimization
333 } if labelRefCount %3 1
343 ; Peephole 120 jump optimization
347 } if labelRefCount %3 2
358 ; Peephole 121 jump optimization
363 } if labelRefCount %3 3
375 ; Peephole 122 jump optimization
381 } if labelRefCount %3 4
390 ; Peephole 123 jump optimization
394 } if labelRefCount %3 1
404 ; Peephole 124 jump optimization
409 } if labelRefCount %3 2
420 ; Peephole 125 jump optimization
426 } if labelRefCount %3 3
438 ; Peephole 126 jump optimization
445 } if labelRefCount %3 4
458 ; Peephole 127 removed misc sequence
460 } if labelRefCount %3 0
467 ; Peephole 128 jump optimization
476 ; Peephole 129 jump optimization
483 ; Peephole 130 changed target address mode r%1 to ar%1
493 ; Peephole 131 optimized decrement (not caring for c)
505 ; Peephole 133 removed redundant moves
518 ; Peephole 134 removed redundant moves
529 ; Peephole 135 removed redundant mov
540 ; Peephole 136 removed redundant moves
546 // WTF? Doesn't look sensible to me...
556 // ; Peephole 137 optimized misc jump sequence
560 //} if labelRefCount %4 1
571 // ; Peephole 138 optimized misc jump sequence
576 //} if labelRefCount %4 1
583 ; Peephole 139 removed redundant mov
593 ; Peephole 140 removed redundant mov
602 ; Peephole 141 removed redundant mov
612 ; Peephole 142 removed redundant moves
621 ; Peephole 143 converted rlc to rl
629 ; Peephole 144 converted rrc to rc
637 ; Peephole 145 changed to add without carry
646 ; Peephole 146 changed to add without carry
654 ; Peephole 147 changed target address mode r%1 to ar%1
661 ; Peephole 148 changed target address mode r%1 to ar%1
668 ; Peephole 149 changed target address mode r%1 to ar%1
678 ; Peephole 150 removed misc moves via dpl before return
691 ; Peephole 151 removed misc moves via dph, dpl before return
703 ; Peephole 152 removed misc moves via dph, dpl before return
718 ; Peephole 153 removed misc moves via dph, dpl, b before return
731 ; Peephole 154 removed misc moves via dph, dpl, b before return
745 ; Peephole 155 removed misc moves via dph, dpl, b before return
762 ; Peephole 156 removed misc moves via dph, dpl, b, a before return
777 ; Peephole 157 removed misc moves via dph, dpl, b, a before return
791 ; Peephole 158 removed misc moves via dph, dpl, b, a before return
800 ; Peephole 159 avoided xrl during execution
809 ; Peephole 160 removed sjmp by inverse jump logic
819 ; Peephole 161 removed sjmp by inverse jump logic
829 ; Peephole 162 removed sjmp by inverse jump logic
839 ; Peephole 163 removed sjmp by inverse jump logic
849 ; Peephole 164 removed sjmp by inverse jump logic
859 ; Peephole 165 removed sjmp by inverse jump logic
869 ; Peephole 166 removed redundant mov
872 } if notVolatile %1 %2
879 ; Peephole 167 removed redundant bit moves (c not set to %1)
888 ; Peephole 168 jump optimization
898 ; Peephole 169 jump optimization
910 ; Peephole 170 jump optimization
913 } if labelRefCount %3 1
923 ; Peephole 171 jump optimization
927 } if labelRefCount %3 2
938 ; Peephole 172 jump optimization
943 } if labelRefCount %3 3
955 ; Peephole 173 jump optimization
961 } if labelRefCount %3 4
970 ; Peephole 174 optimized decrement (acc not set to %2, flags undefined)
981 ; Peephole 175 optimized increment (acc not set to %2, flags undefined)
991 ; Peephole 176 optimized increment, removed redundant mov
996 // this one will screw assignes to volatile/sfr's
1001 ; Peephole 177.a removed redundant mov
1003 } if notVolatile %1 %2
1005 // applies to f.e. scott-add.asm (--model-large)
1010 ; Peephole 177.b removed redundant mov
1014 // applies to f.e. bug-408972.c
1019 ; Peephole 177.c removed redundant move
1021 } if notVolatile %1 %2
1023 // applies to f.e. bug-408972.c
1024 // not before peephole 177.c
1030 ; Peephole 177.d removed redundant move
1033 } if notVolatile %1 %2
1040 ; Peephole 178 removed redundant mov
1045 // rules 179-182 provided by : Frieder <fe@lake.iup.uni-heidelberg.de>
1046 // saving 2 byte, 1 cycle
1051 ; Peephole 179 changed mov to clr
1057 // volatile xdata char t; t=0x01; t=0x03;
1064 ; Peephole 180.a removed redundant mov to dptr
1070 // volatile xdata char t; t=0x01; t=0x03; t=0x01;
1079 ; Peephole 180.b removed redundant mov to dptr
1087 // saving 1 byte, 0 cycles
1091 ; Peephole 181 changed mov to clr
1095 // saving 3 bytes, 2 cycles
1096 // provided by Bernhard Held <bernhard.held@de.westinghouse.com>
1101 ; Peephole 182.a used 16 bit load of DPTR
1105 // saving 3 byte, 2 cycles, return(NULL) profits here
1110 ; Peephole 182.b used 16 bit load of dptr
1114 // saving 3 byte, 2 cycles. Probably obsoleted by 182.b
1119 ; Peephole 182.c used 16 bit load of dptr
1120 mov dptr,#(((%2)<<8) + %1)
1123 // applies to return 0.0; in f.e. sincosf.c
1129 ; Peephole 182.d used 16 bit load of dptr
1130 mov dptr,#(%1&0x00ff)
1138 ; Peephole 183 avoided anl during execution
1147 ; Peephole 184 removed redundant mov
1153 // acc being incremented might cause problems
1157 ; Peephole 185 changed order of increment (acc incremented also!)
1182 ; Peephole 186.a optimized movc sequence
1215 ; Peephole 186.b optimized movc sequence
1240 ; Peephole 186.c optimized movc sequence
1249 // char indexed access to: char code table[] = {4,3,2,1};
1259 ; Peephole 186.d optimized movc sequence
1264 // char indexed access to: int code table[] = {4,3,2,1};
1279 ; Peephole 186.e optimized movc sequence (b, dptr differ)
1297 ; Peephole 187 used a instead of ar%1 for anl
1309 ; Peephole 188 removed redundant mov
1321 ; Peephole 189 removed redundant mov and anl
1326 // rules 190 & 191 need to be in order
1332 ; Peephole 190 removed redundant mov
1344 ; Peephole 191 removed redundant mov
1355 ; Peephole 192 used a instead of ar%1 as source
1372 ; Peephole 193.a optimized misc jump sequence
1382 } if labelRefCount %3 4
1396 ; Peephole 193.b optimized misc jump sequence
1406 } if labelRefCount %3 4
1420 ; Peephole 193.c optimized misc jump sequence
1430 } if labelRefCount %3 4
1441 ; Peephole 194 optimized misc jump sequence
1448 } if labelRefCount %3 4
1460 ; Peephole 195.a optimized misc jump sequence
1468 } if labelRefCount %3 3
1480 ; Peephole 195.b optimized misc jump sequence
1488 } if labelRefCount %3 3
1500 ; Peephole 195.c optimized misc jump sequence
1508 } if labelRefCount %3 3
1518 ; Peephole 196 optimized misc jump sequence
1524 } if labelRefCount %3 3
1534 ; Peephole 197.a optimized misc jump sequence
1540 } if labelRefCount %3 2
1550 ; Peephole 197.b optimized misc jump sequence
1556 } if labelRefCount %3 2
1566 ; Peephole 197.c optimized misc jump sequence
1572 } if labelRefCount %3 2
1581 ; Peephole 198 optimized misc jump sequence
1586 } if labelRefCount %3 2
1594 ; Peephole 199 optimized misc jump sequence
1598 } if labelRefCount %3 1
1604 ; Peephole 200 removed redundant sjmp
1613 ; Peephole 201 removed redundant sjmp
1623 ; Peephole 202 removed redundant push pop
1632 ; Peephole 203 removed mov r%1,_spx
1641 ; Peephole 204 removed redundant mov
1653 ; Peephole 205 optimized misc jump sequence
1657 } if labelRefCount %2 1
1662 ; Peephole 206 removed redundant mov %1,%1
1670 ; Peephole 207 removed zero add (acc not set to %1, flags undefined)
1679 ; Peephole 208 removed redundant push pop
1689 ; Peephole 209 optimized increment (acc not set to %1, flags undefined)
1695 mov dptr,#((((%1 >> 8)) <<8) + %1)
1697 ; Peephole 210 simplified expression
1705 ; Peephole 211 removed redundant push %1 pop %1
1713 ; Peephole 212 reduced add sequence to inc
1719 mov %1,#(( %2 >> 8 ) ^ 0x80)
1721 ; Peephole 213.a inserted fix
1727 mov %1,#(( %2 + %3 >> 8 ) ^ 0x80)
1729 ; Peephole 213.b inserted fix
1730 mov %1,#((%2 + %3) >> 8)
1740 ; Peephole 214 reduced some extra moves
1743 } if operandsNotSame
1750 ; Peephole 215 removed some moves
1753 } if operandsNotSame
1763 ; Peephole 216 simplified clear (2bytes)
1782 ; Peephole 217 simplified clear (3bytes)
1806 ; Peephole 218 simplified clear (4bytes)
1825 ; Peephole 219 removed redundant clear
1841 ; Peephole 219.a removed redundant clear
1854 ; Peephole 220.a removed bogus DPS set
1862 ; Peephole 220.b removed bogus DPS set
1867 mov %1 + %2,(%2 + %1)
1869 ; Peephole 221.a remove redundant move
1873 mov (%1 + %2 + %3),((%2 + %1) + %3)
1875 ; Peephole 221.b remove redundant move
1882 ; Peephole 222 removed dec/inc pair
1891 ; Peephole 223 removed redundant dph/dpl moves
1894 } if notVolatile %1 %2
1902 ; Peephole 224 removed redundant dph/dpl moves
1915 ; Peephole 225 removed redundant move to acc
1931 ; Peephole 226 removed unnecessary clr
1953 ; Peephole 227 replaced inefficient 32 bit clear
1981 ; Peephole 228 replaced inefficient 32 constant
2003 ; Peephole 229 replaced inefficient 16 bit clear
2021 ; Peephole 230 replaced inefficient 16 bit constant
2031 // this last peephole often removes the last mov from 227-230
2036 ; Peephole 231 removed redundant mov to dptr
2043 ; Peephole 232 using movc to read xdata (--xram-movc)
2051 ; Peephole 233 using _gptrgetc instead of _gptrget (--xram-movc)
2061 ; Peephole 234 loading dpl directly from a(ccumulator), r%1 not set
2074 ; Peephole 235 loading dph directly from a(ccumulator), r%1 not set
2081 // 14 rules by Fiorenzo D. Ramaglia <fd.ramaglia@tin.it>
2086 ; Peephole 236.a used r%1 instead of ar%1
2093 ; Peephole 236.b used r%1 instead of ar%1
2100 ; Peephole 236.c used r%1 instead of ar%1
2107 ; Peephole 236.d used r%1 instead of ar%1
2114 ; Peephole 236.e used r%1 instead of ar%1
2121 ; Peephole 236.f used r%1 instead of ar%1
2128 ; Peephole 236.g used r%1 instead of ar%1
2135 ; Peephole 236.h used r%1 instead of ar%1
2142 ; Peephole 236.i used r%1 instead of ar%1
2149 ; Peephole 236.j used r%1 instead of ar%1
2156 ; Peephole 236.k used r%1 instead of ar%1
2163 ; Peephole 236.l used r%1 instead of ar%1
2170 ; Peephole 236.m used r%1 instead of ar%1
2177 ; Peephole 236.n used r%1 instead of ar%1
2188 ; Peephole 237.a removed sjmp to ret
2205 ; Peephole 237.b removed sjmp to ret
2215 // applies to f.e. device/lib/log10f.c
2241 ; Peephole 238.a removed 4 redundant moves
2242 } if operandsNotSame8 %1 %2 %3 %4 %5 %6 %7 %8
2244 // applies to device/lib/log10f.c
2259 ; Peephole 238.b removed 3 redundant moves
2260 } if operandsNotSame7 %1 %2 %3 %4 %5 %6 %7
2262 // applies to f.e. device/lib/time.c
2278 ; Peephole 238.c removed 2 redundant moves
2279 } if operandsNotSame4 %1 %2 %3 %4
2281 // applies to f.e. support/regression/tests/bug-524209.c
2294 ; Peephole 238.d removed 3 redundant moves
2295 } if operandsNotSame6 %1 %2 %3 %4 %5 %6
2297 // applies to f.e. ser_ir.asm
2301 ; Peephole 239 used a instead of acc
2309 ; Peephole 240 use clr instead of addc a,#0
2314 // peepholes 241.a to 241.c and 241.d to 241.f need to be in order
2326 ; Peephole 241.a optimized compare
2337 // applies to f.e. time.c
2347 ; Peephole 241.b optimized compare
2356 // applies to f.e. malloc.c
2365 ; Peephole 241.c optimized compare
2373 // applies to f.e. j = (k!=0x1000);
2374 // with volatile idata long k;
2389 ; Peephole 241.d optimized compare
2403 // applies to f.e. j = (k!=0x1000);
2404 // with volatile idata int k;
2415 ; Peephole 241.e optimized compare
2425 // applies to f.e. vprintf.asm (--stack-auto)
2434 ; Peephole 241.f optimized compare
2442 // applies to f.e. scott-bool1.c
2449 ; Peephole 242.a avoided branch jnz to jz
2454 } if labelRefCount %1 1
2456 // applies to f.e. scott-bool1.c
2464 ; Peephole 242.b avoided branch jnz to jz
2470 } if labelRefCount %1 1
2472 // applies to f.e. logic.c
2482 ; Peephole 242.c avoided branch jnz to jz
2490 } if labelRefCount %1 1
2492 // applies to f.e. vprintf.c
2493 // this is a rare case, usually the "tail increment" is noticed earlier
2500 ; Peephole 243 avoided branch to sjmp
2507 // applies to f.e. simplefloat.c (saving 1 cycle)
2512 ; Peephole 244.a moving first to a instead of r%1
2517 // applies to f.e. _itoa.c (saving 1 cycle)
2522 ; Peephole 244.b moving first to a instead of r%1
2528 // applies to f.e. bug-460010.c (saving 1 cycle)
2533 ; Peephole 244.c loading dpl from a instead of r%1
2542 ; Peephole 244.d loading dph from a instead of r%1
2547 // this one is safe but disables 245.a 245.b
2548 // please remove 245 if 245.a 245.b are found to be safe
2549 // applies to f.e. scott-compare.c
2560 ; Peephole 245 optimized complement (r%1 and acc set needed?)
2565 } if labelRefCount %2 1
2567 // this one will not be triggered if 245 is present
2568 // please remove 245 if 245.a 245.b are found to be safe
2569 // applies to f.e. vprintf.c
2581 ; Peephole 245.a optimized conditional jump (r%1 and acc not set!)
2583 } if labelRefCount %2 1
2585 // this one will not be triggered if 245 is present
2586 // please remove 245 if 245.a 245.b are found to be safe
2587 // applies to f.e. scott-compare.c
2599 ; Peephole 245.b optimized conditional jump (r%1 and acc not set!)
2601 } if labelRefCount %2 1
2604 // rules 246.x apply to f.e. bitfields.c
2615 ; Peephole 246.a combined clr/clr
2632 ; Peephole 246.b combined set/set
2649 ; Peephole 246.c combined set/clr
2667 ; Peephole 246.d combined clr/set
2686 ; Peephole 246.e combined set/clr/clr
2705 ; Peephole 246.f combined set/clr/set
2725 ; Peephole 246.g combined clr/set/clr
2745 ; Peephole 246.h combined clr/set/set
2756 // rules 247.x apply to f.e. bitfields.c
2767 ; Peephole 247.a combined clr/clr
2784 ; Peephole 247.b combined set/set
2801 ; Peephole 247.c combined set/clr
2819 ; Peephole 247.d combined clr/set
2838 ; Peephole 247.e combined set/clr/clr
2857 ; Peephole 247.f combined set/clr/set
2877 ; Peephole 247.g combined clr/set/clr
2897 ; Peephole 247.h combined clr/set/set
2906 // Peepholes 248.x have to be compatible with the keyword volatile.
2907 // They optimize typical accesses to memory mapped I/O devices:
2908 // volatile xdata char t; t|=0x01;
2918 ; Peephole 248.a optimized or to xdata
2926 // volatile xdata char t; t&=0x01;
2936 ; Peephole 248.b optimized and to xdata
2944 // volatile xdata char t; t^=0x01;
2954 ; Peephole 248.c optimized xor to xdata
2962 // volatile xdata char t; t|=0x01; t&=~0x01; t|=0x01;
2982 ; Peephole 248.d optimized or/and/or to volatile xdata
2996 // volatile xdata char t; t&=~0x01; t|=0x01; t&=~0x01;
3016 ; Peephole 248.e optimized and/or/and to volatile xdata
3030 // volatile xdata char t; t|=0x01; t&=~0x01;
3044 ; Peephole 248.f optimized or/and to volatile xdata
3055 // volatile xdata char t; t&=~0x01; t|=0x01;
3069 ; Peephole 248.g optimized and/or to volatile xdata
3080 // volatile xdata char t; t^=0x01; t^=0x01;
3094 ; Peephole 248.h optimized xor/xor to volatile xdata
3109 ; Peephole 249.a jump optimization
3110 } if labelRefCount %1 1
3116 ; Peephole 249.b jump optimization
3117 } if labelRefCount %1 1
3120 // This allows non-interrupt and interrupt code to safely compete
3121 // for a resource without the non-interrupt code having to disable
3123 // volatile bit resource_is_free;
3124 // if( resource_is_free ) {
3125 // resource_is_free=0; do_something; resource_is_free=1;
3132 ; Peephole 250.a using atomic test and clear
3136 } if labelRefCount %3 0
3144 ; Peephole 250.b using atomic test and clear
3148 } if labelRefCount %2 1
3151 // not before peephole 250.b
3155 ; Peephole 251.a replaced ljmp to ret with ret
3157 } if labelIsReturnOnly
3159 // not before peephole 250.b
3163 ; Peephole 251.b replaced sjmp to ret with ret
3165 } if labelIsReturnOnly
3167 // applies to shifts.c and when accessing arrays with an unsigned integer index
3168 // saving 1 byte, 2 cycles
3178 ; Peephole 252 optimized left shift
3187 // applies to: void test( char c ) { if( c ) func1(); else func2(); }
3192 ; Peephole 253.a replaced lcall/ret with ljmp
3196 // applies to: void test( char c ) { if( c ) func1(); else func2(); }
3202 ; Peephole 253.b replaced lcall/ret with ljmp
3204 } if labelRefCount %2 0
3206 // applies to f.e. scott-bool1.c
3212 ; Peephole 253.c replaced lcall with ljmp
3219 // applies to f.e. funptrs.c
3220 // saves one byte if %1 is a register or @register
3225 ; Peephole 254 optimized left shift
3230 // applies to f.e. switch.c
3243 ; Peephole 255 optimized jump table index calculation
3245 cjne a,#(%1+0x01),.+1
3254 // applies to f.e. jump tables and scott-bool1.c.
3255 // similar peepholes can be constructed for other instructions
3256 // after which a flag or a register is known (like: djnz, cjne, jnc)
3262 ; Peephole 256.a removed redundant clr c
3265 } if labelRefCount %2 0
3267 // applies to f.e. logf.c
3273 ; Peephole 256.b removed redundant clr a
3276 } if labelRefCount %2 0
3279 // unsigned char i=8; do{ } while(--i != 0);
3280 // this currently only applies if i is kept in a register
3285 ; Peephole 257 optimized decrement with compare
3290 // in_byte<<=1; if(in_bit) in_byte|=1;
3291 // helps f.e. reading data on a 3-wire (SPI) bus
3301 ; Peephole 258.a optimized bitbanging
3310 // in_byte<<=1; if(in_bit) in_byte|=1;
3320 ; Peephole 258.b optimized bitbanging
3329 // in_byte>>=1; if(in_bit) in_byte|=0x80;
3340 ; Peephole 258.c optimized bitbanging
3349 // in_byte>>=1; if(in_bit) in_byte|=0x80;
3360 ; Peephole 258.d optimized bitbanging
3369 // out_bit=out_byte&0x80; out_byte<<=1;
3370 // helps f.e. writing data on a 3-wire (SPI) bus
3379 ; Peephole 258.e optimized bitbanging
3386 // out_bit=out_byte&0x01; out_byte>>=1;
3396 ; Peephole 258.f optimized bitbanging