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
1035 // applies to f.e. bug-607243.c
1040 ; peephole 177.e removed redundant move
1049 ; Peephole 178 removed redundant mov
1054 // rules 179-182 provided by : Frieder <fe@lake.iup.uni-heidelberg.de>
1055 // saving 2 byte, 1 cycle
1060 ; Peephole 179 changed mov to clr
1066 // volatile xdata char t; t=0x01; t=0x03;
1073 ; Peephole 180.a removed redundant mov to dptr
1079 // volatile xdata char t; t=0x01; t=0x03; t=0x01;
1088 ; Peephole 180.b removed redundant mov to dptr
1096 // saving 1 byte, 0 cycles
1100 ; Peephole 181 changed mov to clr
1104 // saving 3 bytes, 2 cycles
1105 // provided by Bernhard Held <bernhard.held@de.westinghouse.com>
1110 ; Peephole 182.a used 16 bit load of DPTR
1114 // saving 3 byte, 2 cycles, return(NULL) profits here
1119 ; Peephole 182.b used 16 bit load of dptr
1123 // saving 3 byte, 2 cycles. Probably obsoleted by 182.b
1128 ; Peephole 182.c used 16 bit load of dptr
1129 mov dptr,#(((%2)<<8) + %1)
1132 // applies to return 0.0; in f.e. sincosf.c
1138 ; Peephole 182.d used 16 bit load of dptr
1139 mov dptr,#(%1&0x00ff)
1147 ; Peephole 183 avoided anl during execution
1156 ; Peephole 184 removed redundant mov
1162 // acc being incremented might cause problems
1166 ; Peephole 185 changed order of increment (acc incremented also!)
1191 ; Peephole 186.a optimized movc sequence
1224 ; Peephole 186.b optimized movc sequence
1249 ; Peephole 186.c optimized movc sequence
1258 // char indexed access to: char code table[] = {4,3,2,1};
1268 ; Peephole 186.d optimized movc sequence
1273 // char indexed access to: int code table[] = {4,3,2,1};
1288 ; Peephole 186.e optimized movc sequence (b, dptr differ)
1306 ; Peephole 187 used a instead of ar%1 for anl
1318 ; Peephole 188 removed redundant mov
1330 ; Peephole 189 removed redundant mov and anl
1335 // rules 190 & 191 need to be in order
1341 ; Peephole 190 removed redundant mov
1353 ; Peephole 191 removed redundant mov
1364 ; Peephole 192 used a instead of ar%1 as source
1381 ; Peephole 193.a optimized misc jump sequence
1391 } if labelRefCount %3 4
1405 ; Peephole 193.b optimized misc jump sequence
1415 } if labelRefCount %3 4
1429 ; Peephole 193.c optimized misc jump sequence
1439 } if labelRefCount %3 4
1450 ; Peephole 194 optimized misc jump sequence
1457 } if labelRefCount %3 4
1469 ; Peephole 195.a optimized misc jump sequence
1477 } if labelRefCount %3 3
1489 ; Peephole 195.b optimized misc jump sequence
1497 } if labelRefCount %3 3
1509 ; Peephole 195.c optimized misc jump sequence
1517 } if labelRefCount %3 3
1527 ; Peephole 196 optimized misc jump sequence
1533 } if labelRefCount %3 3
1543 ; Peephole 197.a optimized misc jump sequence
1549 } if labelRefCount %3 2
1559 ; Peephole 197.b optimized misc jump sequence
1565 } if labelRefCount %3 2
1575 ; Peephole 197.c optimized misc jump sequence
1581 } if labelRefCount %3 2
1590 ; Peephole 198 optimized misc jump sequence
1595 } if labelRefCount %3 2
1603 ; Peephole 199 optimized misc jump sequence
1607 } if labelRefCount %3 1
1613 ; Peephole 200 removed redundant sjmp
1622 ; Peephole 201 removed redundant sjmp
1632 ; Peephole 202 removed redundant push pop
1641 ; Peephole 203 removed mov r%1,_spx
1650 ; Peephole 204 removed redundant mov
1662 ; Peephole 205 optimized misc jump sequence
1666 } if labelRefCount %2 1
1671 ; Peephole 206 removed redundant mov %1,%1
1679 ; Peephole 207 removed zero add (acc not set to %1, flags undefined)
1688 ; Peephole 208 removed redundant push pop
1698 ; Peephole 209 optimized increment (acc not set to %1, flags undefined)
1704 mov dptr,#((((%1 >> 8)) <<8) + %1)
1706 ; Peephole 210 simplified expression
1714 ; Peephole 211 removed redundant push %1 pop %1
1722 ; Peephole 212 reduced add sequence to inc
1728 mov %1,#(( %2 >> 8 ) ^ 0x80)
1730 ; Peephole 213.a inserted fix
1736 mov %1,#(( %2 + %3 >> 8 ) ^ 0x80)
1738 ; Peephole 213.b inserted fix
1739 mov %1,#((%2 + %3) >> 8)
1749 ; Peephole 214 reduced some extra moves
1752 } if operandsNotSame
1759 ; Peephole 215 removed some moves
1762 } if operandsNotSame
1772 ; Peephole 216 simplified clear (2bytes)
1791 ; Peephole 217 simplified clear (3bytes)
1815 ; Peephole 218 simplified clear (4bytes)
1834 ; Peephole 219 removed redundant clear
1850 ; Peephole 219.a removed redundant clear
1863 ; Peephole 220.a removed bogus DPS set
1871 ; Peephole 220.b removed bogus DPS set
1876 mov %1 + %2,(%2 + %1)
1878 ; Peephole 221.a remove redundant move
1882 mov (%1 + %2 + %3),((%2 + %1) + %3)
1884 ; Peephole 221.b remove redundant move
1891 ; Peephole 222 removed dec/inc pair
1900 ; Peephole 223 removed redundant dph/dpl moves
1903 } if notVolatile %1 %2
1911 ; Peephole 224 removed redundant dph/dpl moves
1924 ; Peephole 225 removed redundant move to acc
1940 ; Peephole 226 removed unnecessary clr
1962 ; Peephole 227 replaced inefficient 32 bit clear
1990 ; Peephole 228 replaced inefficient 32 constant
2012 ; Peephole 229 replaced inefficient 16 bit clear
2030 ; Peephole 230 replaced inefficient 16 bit constant
2040 // this last peephole often removes the last mov from 227-230
2045 ; Peephole 231 removed redundant mov to dptr
2052 ; Peephole 232 using movc to read xdata (--xram-movc)
2060 ; Peephole 233 using _gptrgetc instead of _gptrget (--xram-movc)
2070 ; Peephole 234 loading dpl directly from a(ccumulator), r%1 not set
2083 ; Peephole 235 loading dph directly from a(ccumulator), r%1 not set
2090 // 14 rules by Fiorenzo D. Ramaglia <fd.ramaglia@tin.it>
2095 ; Peephole 236.a used r%1 instead of ar%1
2102 ; Peephole 236.b used r%1 instead of ar%1
2109 ; Peephole 236.c used r%1 instead of ar%1
2116 ; Peephole 236.d used r%1 instead of ar%1
2123 ; Peephole 236.e used r%1 instead of ar%1
2130 ; Peephole 236.f used r%1 instead of ar%1
2137 ; Peephole 236.g used r%1 instead of ar%1
2144 ; Peephole 236.h used r%1 instead of ar%1
2151 ; Peephole 236.i used r%1 instead of ar%1
2158 ; Peephole 236.j used r%1 instead of ar%1
2165 ; Peephole 236.k used r%1 instead of ar%1
2172 ; Peephole 236.l used r%1 instead of ar%1
2179 ; Peephole 236.m used r%1 instead of ar%1
2186 ; Peephole 236.n used r%1 instead of ar%1
2197 ; Peephole 237.a removed sjmp to ret
2214 ; Peephole 237.b removed sjmp to ret
2224 // applies to f.e. device/lib/log10f.c
2250 ; Peephole 238.a removed 4 redundant moves
2251 } if operandsNotSame8 %1 %2 %3 %4 %5 %6 %7 %8
2253 // applies to device/lib/log10f.c
2268 ; Peephole 238.b removed 3 redundant moves
2269 } if operandsNotSame7 %1 %2 %3 %4 %5 %6 %7
2271 // applies to f.e. device/lib/time.c
2287 ; Peephole 238.c removed 2 redundant moves
2288 } if operandsNotSame4 %1 %2 %3 %4
2290 // applies to f.e. support/regression/tests/bug-524209.c
2303 ; Peephole 238.d removed 3 redundant moves
2304 } if operandsNotSame6 %1 %2 %3 %4 %5 %6
2306 // applies to f.e. ser_ir.asm
2310 ; Peephole 239 used a instead of acc
2318 ; Peephole 240 use clr instead of addc a,#0
2323 // peepholes 241.a to 241.c and 241.d to 241.f need to be in order
2335 ; Peephole 241.a optimized compare
2346 // applies to f.e. time.c
2356 ; Peephole 241.b optimized compare
2365 // applies to f.e. malloc.c
2374 ; Peephole 241.c optimized compare
2382 // applies to f.e. j = (k!=0x1000);
2383 // with volatile idata long k;
2398 ; Peephole 241.d optimized compare
2412 // applies to f.e. j = (k!=0x1000);
2413 // with volatile idata int k;
2424 ; Peephole 241.e optimized compare
2434 // applies to f.e. vprintf.asm (--stack-auto)
2443 ; Peephole 241.f optimized compare
2451 // applies to f.e. scott-bool1.c
2458 ; Peephole 242.a avoided branch jnz to jz
2463 } if labelRefCount %1 1
2465 // applies to f.e. scott-bool1.c
2473 ; Peephole 242.b avoided branch jnz to jz
2479 } if labelRefCount %1 1
2481 // applies to f.e. logic.c
2491 ; Peephole 242.c avoided branch jnz to jz
2499 } if labelRefCount %1 1
2501 // applies to f.e. vprintf.c
2502 // this is a rare case, usually the "tail increment" is noticed earlier
2509 ; Peephole 243 avoided branch to sjmp
2516 // applies to f.e. simplefloat.c (saving 1 cycle)
2521 ; Peephole 244.a moving first to a instead of r%1
2526 // applies to f.e. _itoa.c (saving 1 cycle)
2531 ; Peephole 244.b moving first to a instead of r%1
2537 // applies to f.e. bug-460010.c (saving 1 cycle)
2542 ; Peephole 244.c loading dpl from a instead of r%1
2551 ; Peephole 244.d loading dph from a instead of r%1
2556 // this one is safe but disables 245.a 245.b
2557 // please remove 245 if 245.a 245.b are found to be safe
2558 // applies to f.e. scott-compare.c
2569 ; Peephole 245 optimized complement (r%1 and acc set needed?)
2574 } if labelRefCount %2 1
2576 // this one will not be triggered if 245 is present
2577 // please remove 245 if 245.a 245.b are found to be safe
2578 // applies to f.e. vprintf.c
2590 ; Peephole 245.a optimized conditional jump (r%1 and acc not set!)
2592 } if labelRefCount %2 1
2594 // this one will not be triggered if 245 is present
2595 // please remove 245 if 245.a 245.b are found to be safe
2596 // applies to f.e. scott-compare.c
2608 ; Peephole 245.b optimized conditional jump (r%1 and acc not set!)
2610 } if labelRefCount %2 1
2613 // rules 246.x apply to f.e. bitfields.c
2624 ; Peephole 246.a combined clr/clr
2641 ; Peephole 246.b combined set/set
2658 ; Peephole 246.c combined set/clr
2676 ; Peephole 246.d combined clr/set
2695 ; Peephole 246.e combined set/clr/clr
2714 ; Peephole 246.f combined set/clr/set
2734 ; Peephole 246.g combined clr/set/clr
2754 ; Peephole 246.h combined clr/set/set
2765 // rules 247.x apply to f.e. bitfields.c
2776 ; Peephole 247.a combined clr/clr
2793 ; Peephole 247.b combined set/set
2810 ; Peephole 247.c combined set/clr
2828 ; Peephole 247.d combined clr/set
2847 ; Peephole 247.e combined set/clr/clr
2866 ; Peephole 247.f combined set/clr/set
2886 ; Peephole 247.g combined clr/set/clr
2906 ; Peephole 247.h combined clr/set/set
2915 // Peepholes 248.x have to be compatible with the keyword volatile.
2916 // They optimize typical accesses to memory mapped I/O devices:
2917 // volatile xdata char t; t|=0x01;
2927 ; Peephole 248.a optimized or to xdata
2935 // volatile xdata char t; t&=0x01;
2945 ; Peephole 248.b optimized and to xdata
2953 // volatile xdata char t; t^=0x01;
2963 ; Peephole 248.c optimized xor to xdata
2971 // volatile xdata char t; t|=0x01; t&=~0x01; t|=0x01;
2991 ; Peephole 248.d optimized or/and/or to volatile xdata
3005 // volatile xdata char t; t&=~0x01; t|=0x01; t&=~0x01;
3025 ; Peephole 248.e optimized and/or/and to volatile xdata
3039 // volatile xdata char t; t|=0x01; t&=~0x01;
3053 ; Peephole 248.f optimized or/and to volatile xdata
3064 // volatile xdata char t; t&=~0x01; t|=0x01;
3078 ; Peephole 248.g optimized and/or to volatile xdata
3089 // volatile xdata char t; t^=0x01; t^=0x01;
3103 ; Peephole 248.h optimized xor/xor to volatile xdata
3118 ; Peephole 249.a jump optimization
3119 } if labelRefCount %1 1
3125 ; Peephole 249.b jump optimization
3126 } if labelRefCount %1 1
3129 // This allows non-interrupt and interrupt code to safely compete
3130 // for a resource without the non-interrupt code having to disable
3132 // volatile bit resource_is_free;
3133 // if( resource_is_free ) {
3134 // resource_is_free=0; do_something; resource_is_free=1;
3141 ; Peephole 250.a using atomic test and clear
3145 } if labelRefCount %3 0
3153 ; Peephole 250.b using atomic test and clear
3157 } if labelRefCount %2 1
3160 // not before peephole 250.b
3164 ; Peephole 251.a replaced ljmp to ret with ret
3166 } if labelIsReturnOnly
3168 // not before peephole 250.b
3172 ; Peephole 251.b replaced sjmp to ret with ret
3174 } if labelIsReturnOnly
3176 // applies to shifts.c and when accessing arrays with an unsigned integer index
3177 // saving 1 byte, 2 cycles
3187 ; Peephole 252 optimized left shift
3196 // applies to: void test( char c ) { if( c ) func1(); else func2(); }
3201 ; Peephole 253.a replaced lcall/ret with ljmp
3205 // applies to: void test( char c ) { if( c ) func1(); else func2(); }
3211 ; Peephole 253.b replaced lcall/ret with ljmp
3213 } if labelRefCount %2 0
3215 // applies to f.e. scott-bool1.c
3221 ; Peephole 253.c replaced lcall with ljmp
3228 // applies to f.e. funptrs.c
3229 // saves one byte if %1 is a register or @register
3234 ; Peephole 254 optimized left shift
3239 // applies to f.e. switch.c
3252 ; Peephole 255 optimized jump table index calculation
3254 cjne a,#(%1+0x01),.+1
3263 // applies to f.e. jump tables and scott-bool1.c.
3264 // similar peepholes can be constructed for other instructions
3265 // after which a flag or a register is known (like: djnz, cjne, jnc)
3271 ; Peephole 256.a removed redundant clr c
3274 } if labelRefCount %2 0
3276 // applies to f.e. logf.c
3282 ; Peephole 256.b removed redundant clr a
3285 } if labelRefCount %2 0
3288 // unsigned char i=8; do{ } while(--i != 0);
3289 // this currently only applies if i is kept in a register
3294 ; Peephole 257 optimized decrement with compare
3299 // in_byte<<=1; if(in_bit) in_byte|=1;
3300 // helps f.e. reading data on a 3-wire (SPI) bus
3310 ; Peephole 258.a optimized bitbanging
3319 // in_byte<<=1; if(in_bit) in_byte|=1;
3329 ; Peephole 258.b optimized bitbanging
3338 // in_byte>>=1; if(in_bit) in_byte|=0x80;
3349 ; Peephole 258.c optimized bitbanging
3358 // in_byte>>=1; if(in_bit) in_byte|=0x80;
3369 ; Peephole 258.d optimized bitbanging
3378 // out_bit=out_byte&0x80; out_byte<<=1;
3379 // helps f.e. writing data on a 3-wire (SPI) bus
3388 ; Peephole 258.e optimized bitbanging
3395 // out_bit=out_byte&0x01; out_byte>>=1;
3405 ; Peephole 258.f optimized bitbanging