1 // peeph
-sm83.def
- SM83 peephole rules
4 // (c
) Philipp Klaus
Krause (pkk@spth.de
, philipp@colecovision.eu
) 2006 - 2007
5 // (c
) Sebastian
'basxto' Riedel (sdcc@basxto.de
) 2020 - 2022
7 // This program is free software
; you can redistribute it and
/or modify it
8 // under the terms of the GNU General Public License as published by the
9 // Free Software Foundation
; either version
2, or (at your option
) any
12 // This program is distributed in the hope that it will be useful
,
13 // but WITHOUT ANY WARRANTY
; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program
; if not
, write to the Free Software
19 // Foundation
, 59 Temple Place
- Suite
330, Boston
, MA
02111-1307, USA.
25 ; sm83 peephole
0 used ldh instead of ld a
, (hl
).
27 } if
operandsLiteral(%1), immdInRange(0xff00 0xffff '|' %1 0 %2), notUsed('hl')
32 ; sm83 peephole
0b used ldh instead of ld.
34 } if
operandsLiteral(%1), immdInRange(0xff00 0xffff '|' %1 0 %2)
40 ; sm83 peephole
1 used ldh instead of
ld (hl
), a.
42 } if
operandsLiteral(%1), immdInRange(0xff00 0xffff '|' %1 0 %2), notUsed('hl')
47 ; sm83 peephole
1b used ldh instead of ld.
49 } if
operandsLiteral(%1), immdInRange(0xff00 0xffff '|' %1 0 %2)
56 ; sm83 peephole
2 used ldi to increment hl after load
65 ; sm83 peephole
2b used ldi to increment hl after load
74 ; sm83 peephole
3 used ldd to decrement hl after load
82 ; sm83 peephole
3b used ldd to decrement hl after load
90 ; sm83 peephole
4 used ldi to increment hl after load
99 ; sm83 peephole
4b used ldi to increment hl after load
107 ; sm83 peephole
5 used ldd to decrement hl after load
116 ; sm83 peephole
5b used ldd to decrement hl after load
126 ; sm83 peephole
7 used ldi to increment hl
129 } if
notSame('hl' %2)
138 ; sm83 peephole
8 used ldi to increment hl
141 } if
notSame('hl' %2)
149 ; sm83 peephole
9 reversed loading order of hl
160 ; sm83 peephole
10a removed redundant ldhl.
163 } if
notSame('(hl+)' %2 %3), notSame('(hl+)' %2 %3)
165 // 10b and c are for pushing stack addresses
175 ; sm83 peephole
10b turned ldhl into dec hl
177 } if
operandsLiteral(%1 %2), notSame('l' %3 %5), notSame('l' %3 %5), immdInRange(0x01 0x01 '-' %1 %2 %3), operandsNotRelated('hl' %3 %4 %5 %6)
188 ; sm83 peephole
10c turned ldhl into inc hl
190 } if
operandsLiteral(%1 %2), notSame('l' %3 %5), notSame('l' %3 %5), immdInRange(0x01 0x01 '-' %2 %1 %3), operandsNotRelated('hl' %3 %4 %5 %6)
196 ; sm83 peephole
10d combined ld and dec.
198 } if
immdInRange(-128 127 '-' %1 1 %2)
204 ; sm83 peephole
10e combined ld and inc.
206 } if
immdInRange(-128 127 '+' %1 1 %2)
213 ; sm83 peephole
11 combined redundant ANDs #
%2 and swapped #
%1.
216 } if
operandsLiteral(%1 %2), immdInRange(0x00 0xff '/' %1 0x10 %3), immdInRange(0x00 0xfff '*' %1 0x10 %4), immdInRange(0x00 0xff '&' %4 0xF0 %5), immdInRange(0x00 0xff '|' %3 %5 %6), immdInRange(0x00 0xff '&' %2 %6 %x7
)
223 ; sm83 peephole
12 turned ldh into
inc (hl
).
224 ld hl
, #
((%1) |
0xFF00)
226 } if
notSame(%1 'c'), notUsed('a' 'hl')
233 ; sm83 peephole
13 turned ldh into
dec (hl
).
234 ld hl
, #
((%1) |
0xFF00)
236 } if
notSame(%1 'c'), notUsed('a' 'hl')
243 ; sm83 peephole
14 turned ldh into
srl (hl
).
244 ld hl
, #
((%1) |
0xFF00)
246 } if
notSame(%1 'c'), notUsed('a' 'hl')
252 ; sm83 peephole
16 removed redundant rra
/rla
253 } if
notUsed('cf' 'hf' 'nf' 'zf')
263 ; sm83 peephole
17a pushed double `xor a` fast
270 //TODO
: fails for unknown reasons
275 // ; sm83 peephole
17b pushed #
%2 faster
279 //} if
operandsLiteral(%2), immdInRange(0 0 '+' %2 0 %3), notUsed(%1 'af')
285 ; sm83 peephole
18a pushed #
%2 faster
288 } if
operandsLiteral(%2), immdInRange(0x00C0 0x00C0 '+' %2 0 %x3
), notUsed(%1 'af')
291 // Those rules save
1 byte and
4 cycles each
296 ; sm83 peephole
18b pushed #
%2 faster
299 } if
operandsLiteral(%2), immdInRange(0x0080 0x0080 '+' %2 0 %x3
), notUsed(%1 'af')
305 ; sm83 peephole
18c pushed #
%2 faster
309 } if
operandsLiteral(%2), immdInRange(0x0090 0x0090 '+' %2 0 %x3
), notUsed(%1 'af')
315 ; sm83 peephole
18d pushed #
%2 faster
319 } if
operandsLiteral(%2), immdInRange(0x00A0 0x00A0 '+' %2 0 %x3
), notUsed(%1 'af')
325 ; sm83 peephole
18e pushed #
%2 faster
329 } if
operandsLiteral(%2), immdInRange(0x00D0 0x00D0 '+' %2 0 %x3
), notUsed(%1 'af')
335 ; sm83 peephole
18f pushed #
%2 faster
339 } if
operandsLiteral(%2), immdInRange(0x0100 0x0100 '+' %2 0 %x3
), notUsed(%1 'af')
345 ; sm83 peephole
18g pushed #
%2 faster
349 } if
operandsLiteral(%2), immdInRange(0xFF30 0xFF30 '+' %2 0 %x3
), notUsed(%1 'af')
355 ; sm83 peephole
18h pushed #
%2 faster
359 } if
operandsLiteral(%2), immdInRange(0xFF60 0xFF60 '+' %2 0 %x3
), notUsed(%1 'af')
368 ; sm83 peephole
19 pushed #
%2 via flags
371 } if
operandsLiteral(%2), immdInRange(0xC0 0xC0 '+' %2 0 %x3
), notUsed('f')
380 ; sm83 peephole
20 reversed ld and or.
406 ; sm83 peephole
21 sped up loop body.
408 } if
canSplitReg(%6 %9 %10), operandsNotRelated(%3 %6 'hl'), operandsNotRelated(%3 %6 'hl'), notSame(%3 %4), labelRefCount(%2 1)
418 ; sm83 peephole
22 remove unnecessary load back
427 ; peephole sp1 removed not needed push
437 ; peephole sp2 removed not needed push
440 } if
notUsed('sp'), notSame(%1 %2)
445 ; peephole sp3 removed not needed add sp
451 ; peephole sp6 removed not needed pop
452 } if
notUsed(%1), notUsed('sp')
454 // for
2B it wouldn
't be worth it
460 ; peephole sp7 increased SP by addition
468 ; peephole sp8a increased SP by addition
470 } if immdInRange(-128 127 '+' %1 1 %2), notUsed('hf
' 'cf
')
476 ; peephole sp8b increased SP by addition
478 } if immdInRange(-128 127 '+' %1 1 %2)
484 ; peephole sp9 combined SP additions
486 } if immdInRange(-128 127 '+' %1 %2 %3), notUsed('hf
' 'cf
')
488 // only handle stack adjustment after calls to not
489 // disturb detection of uninitialized reads in emulators
495 ; peephole sp10a increased SP by %1 through pop
497 } if immdInRange(2 2 '+' %1 0 %2), unusedReg(%3 'bc
' 'de
' 'hl
'), notUsed('f
')
499 // unusedReg does not support 'af
'
505 ; peephole sp10b increased SP by %1 through pop
507 } if immdInRange(2 2 '+' %1 0 %2), notUsed('af
')
516 ; peephole sp10c increased SP by 2 through pop
518 } if unusedReg(%3 'bc
' 'de
' 'hl
')