1 ////////////////////////////////////////////////////////
2 // Rules for common codegen patterns
3 ////////////////////////////////////////////////////////
15 ; Peephole Function_Entry_1
- eliminated unnecessary lda
%1
17 ; Peephole Function_Entry_1
- eliminated unnecessary lda
%2
19 } if
notVolatile(%1), notVolatile(%2)
31 ; Peephole Function_Entry_1a
- eliminated unnecessary lda
%1
33 ; Peephole Function_Entry_1a
- eliminated unnecessary lda
%2
35 } if
notVolatile(%1), notVolatile(%2)
45 ; Peephole Function_Entry_2
- eliminated redundant lda
%1/ldx
%2
46 } if
notVolatile(%1), notVolatile(%2)
56 ; Peephole Function_Entry_2a
- eliminated redundant lda
%1/ldx
%2
57 } if
notVolatile(%1), notVolatile(%2)
70 ; Peephole genpointer YX
- eliminated ldx
/ldy
72 ; Peephole genpointer YX
- replaced txa with lda
77 ; Peephole genpointer YX
- replaced tya with lda
82 // TODO
: Enable when
notUsed() gets implemented
!
94 // ; Peephole genpointer XA
- eliminated ldx
99 // ; Peephole genpointer XA
- replaced txa with lda
105 ////////////////////////////////////////////////////////
106 // Remove unnecessary loads
107 ////////////////////////////////////////////////////////
114 ; Peephole load
1 - removed redundant lda
115 } if
notVolatile(%1), notUsed('n' 'z')
117 // ora is cheaper than
lda (except for lda from immediate
, but that can
't happen for the two rules below)
124 ; Peephole load 1a - replaced redundant lda by ora
132 ; Peephole load 2 - removed redundant lda
133 } if notVolatile(%1), notUsed('n
' 'z
')
141 ; Peephole load 2a - replaced redundant lda by ora
149 ; Peephole load 3 - eliminated redundant ldx
150 } if notVolatile(%1), notUsed('n
' 'z
')
157 ; Peephole load 4 - eliminated redundant ldy
158 } if notVolatile(%1), notUsed('n
' 'z
')
160 ////////////////////////////////////////////////////////
162 ////////////////////////////////////////////////////////
168 ; Peephole load 5 - eliminated dead %1 %2
170 } if same(%1 'lda
' 'ldx
' 'ldy
'), notVolatile(%2)
177 ; Peephole load 6 - eliminated dead %1 %2
179 } if same(%1 'lda
' 'ldx
' 'ldy
'), notVolatile(%2)
185 ; Peephole load 7 - eliminated dead %1 %2
187 } if same(%1 'lda
' 'ldx
' 'ldy
'), notVolatile(%2)
194 ; Peephole load 8 - eliminated dead %1 %2
196 } if same(%1 'lda
' 'ldx
' 'ldy
'), notVolatile(%2)
202 ; Peephole load 9 - removed dead ldx followed by %2
204 } if same(%2 'tax
' 'tsx
'), notVolatile(%1)
210 ; Peephole load 10 - removed dead ldy followed by tay
218 ; Peephole load 11 - removed dead lda
220 } if same(%2 'txa
' 'tya
' 'pla
'), notVolatile(%1)
227 ; Peephole load 12 - removed dead lda not followed by store or branch
230 } if notSame(%1 'sta
' 'adc
' 'sbc
' 'cmp
' 'bne
' 'beq
' 'bpl
' 'bmi
' 'bcc
' 'bcs
' 'jsr
' 'jmp
'), notSame(%2 'a
'), notVolatile(%4)
237 ; Peephole load 13 - removed dead ldx not followed by store or branch
240 } if notSame(%1 'stx
' 'cpx
' 'bne
' 'beq
' 'bpl
' 'bmi
' 'jsr
' 'jmp
'), notVolatile(%4)
247 ; Peephole load 14 - removed dead ldy not followed by store or branch
250 } if notSame(%1 'sty
' 'cpy
' 'bne
' 'beq
' 'bpl
' 'bmi
' 'jsr
' 'jmp
'), notVolatile(%4)
257 ; Peephole load 15 - removed dead lda
260 } if notSame(%1 'pha
' 'php
' 'tax
' 'tay
'), notVolatile(%3)
267 ; Peephole load 16 - removed dead ldx
270 } if notSame(%1 'txa
' 'txs
' 'phx
'), notVolatile(%3)
277 ; Peephole load 17 - removed dead ldy
280 } if notSame(%1 'tya
' 'phy
'), notVolatile(%3)
282 ////////////////////////////////////////////////////////
283 // Remove dead register operation/transfers
284 ////////////////////////////////////////////////////////
290 ; Peephole regop 1 - removed dead %1 followed by lda
292 } if same(%1 'txa
' 'tya
')
298 ; Peephole regop 2 - removed dead %1 followed by lda
300 } if same(%1 'and
' 'eor
' 'ora
'), notVolatile(%3)
306 ; Peephole regop 3 - removed dead %1 followed by ldx
308 } if same(%1 'tax
' 'tsx
' 'inx
' 'dex
' )
314 ; Peephole regop 4 - removed dead %1 followed by tsx
316 } if same(%1 'tax
' 'inx
' 'dex
' )
322 ; Peephole regop 5 - removed dead %1 followed by tax
324 } if same(%1 'tsx
' 'inx
' 'dex
' )
330 ; Peephole regop 6 - removed dead %1 followed by ldy
332 } if same(%1 'tay
' 'iny
' 'dey
' )
339 ; Peephole regop 7 - removed redundant txa
347 ; Peephole regop 8 - removed redundant tya preceded by tay
355 ; Peephole regop 9 - removed redundant tax
358 } if notSame(%1 'stx
' 'cpx
' 'bne
' 'beq
' 'bpl
' 'bmi
' 'jsr
' 'jmp
')
360 // if same(%1 'ora
' 'and
' 'eor
' )
367 ; Peephole regop 10 - removed redundant tay
370 } if notSame(%1 'sty
' 'cpy
' 'bne
' 'beq
' 'bpl
' 'bmi
' 'jsr
' 'jmp
')
372 ////////////////////////////////////////////////////////
373 // Remove redundant/unnecessry stack operations
374 ////////////////////////////////////////////////////////
381 ; Peephole stack 1 - removed dead pla/pha followed by lda
390 ; Peephole stack 2 - removed dead pla/pha followed by txa/tya
392 } if same(%1 'txa
' 'tya
')
399 ; Peephole stack 3 - removed useless php/plp around a store
401 } if same(%1 'sta
' 'stx
' 'sty
' )
408 ; Peephole stack 4 - removed dead php/plp
411 ////////////////////////////////////////////////////////
412 // Remove unnecessary ora #00
413 ////////////////////////////////////////////////////////
417 ; Peephole or 1 - eliminated unnecessary ora #0x00
418 } if notUsed('n
' 'z
')
423 ; Peephole or 2 - eliminated unnecessary cmp #0x00
424 } if notUsed('n
' 'z
' 'c
')
426 ////////////////////////////////////////////////////////
427 // Remove unnecessary cmp/cpx/cpy #00
428 ////////////////////////////////////////////////////////
435 ; Peephole cmp 1 - eliminated unnecessary cmp #0x00
443 ; Peephole cmp 2 - eliminated unnecessary cmp #0x00
451 ; Peephole cmp 3 - eliminated unnecessary cmp #0x00
452 } if same(%1 'pla
' 'txa
' 'tya
' ), notUsed('c
')
459 ; Peephole cmp 4 - eliminated unnecessary cpx #0x00
467 ; Peephole cmp 5 - eliminated unnecessary cpy #0x00
475 ; Peephole cmp 6 - eliminated unnecessary cpx #0x00
476 } if same(%1 'inx
' 'dex
' 'tax
' ), notUsed('c
')
483 ; Peephole cmp 7 - eliminated unnecessary cpy #0x00
484 } if same(%1 'iny
' 'dey
' 'tay
' ), notUsed('c
')
486 ////////////////////////////////////////////////////////
487 // Remove redundant stores
488 ////////////////////////////////////////////////////////
495 ; Peephole store 1 - removed redundant sta
505 ; Peephole store 2 - removed redundant sta
506 } if notSame(%1 'pla
' 'txa
' 'tya
'), notVolatile(%2)
513 ; Peephole store 3 - removed redundant sta
516 } if same(%1 'asl
' 'rol
' 'ror
'), notVolatile(%2)
523 ; Peephole store 4 - removed redundant stx
533 ; Peephole store 5 - removed redundant stx
534 } if notSame(%1 'tax
' 'inx
' 'dex
'), notVolatile(%2)
541 ; Peephole store 6 - removed redundant sty
551 ; Peephole store 7 - removed redundant sty
552 } if notSame(%1 'tay
' 'iny
' 'dey
'), notVolatile(%2)
554 ////////////////////////////////////////////////////////
555 // Replace jmp with branch
556 ////////////////////////////////////////////////////////
564 ; Peephole branch 1 - inverted bne and eliminated jmp
566 } if labelInRange(%5), labelRefCountChange(%1 -1)
574 ; Peephole branch 2 - inverted beq and eliminated jmp
576 } if labelInRange(%5), labelRefCountChange(%1 -1)
584 ; Peephole branch 3 - inverted bcc and eliminated jmp
586 } if labelInRange(%5), labelRefCountChange(%1 -1)
594 ; Peephole branch 4 - inverted bcs and eliminated jmp
596 } if labelInRange(%5), labelRefCountChange(%1 -1)
604 ; Peephole branch 5 - inverted bpl and eliminated jmp
606 } if labelInRange(%5), labelRefCountChange(%1 -1)
614 ; Peephole branch 6 - inverted bmi and eliminated jmp
616 } if labelInRange(%5), labelRefCountChange(%1 -1)
624 ; Peephole branch 7 - inverted bvc and eliminated jmp
626 } if labelInRange(%5), labelRefCountChange(%1 -1)
634 ; Peephole branch 8 - inverted bvs and eliminated jmp
636 } if labelInRange(%5), labelRefCountChange(%1 -1)
638 ////////////////////////////////////////////////////////
639 // Replace branch to jmp with branch
640 ////////////////////////////////////////////////////////
645 ; Peephole branch 9 - replaced branch to jmp with branch
647 } if same(%1 'bne
' 'beq
' 'bcc
' 'bcs
' 'bvc
' 'bvs
' 'bpl
' 'bmi
'), labelIsUncondJump(%5), notSame(%5 %6), labelInRange(%6), labelRefCountChange(%5 -1), labelRefCountChange(%6 +1)
652 ; Peephole branch 10 - replaced jmp to jmp with jmp
654 } if labelIsUncondJump(%5), notSame(%5 %6), labelRefCountChange(%5 -1), labelRefCountChange(%6 +1)
659 ; Peephole branch 11 - replaced jmp to rts with rts
661 } if optimizeReturn(), labelIsReturnOnly(%5), labelRefCountChange(%5 -1)
663 ////////////////////////////////////////////////////////
664 // Remove branch/jump to following label
665 ////////////////////////////////////////////////////////
671 ; Peephole branch 12 - eliminated branch to %2
673 } if same(%1 'bne
' 'beq
' 'bcc
' 'bcs
' 'bvc
' 'bvs
' 'bpl
' 'bmi
' 'jmp
' 'bra
'), labelRefCountChange(%2 -1)
675 ////////////////////////////////////////////////////////
676 // Tail call optimization
677 ////////////////////////////////////////////////////////
684 ; Peephole ret 1 - eliminated redundant rts
694 ; Peephole ret 2 - tail call optimization.
695 } if optimizeReturn()
697 ////////////////////////////////////////////////////////
698 // Unused label removal
699 ////////////////////////////////////////////////////////
701 // should be one of the last peepholes
705 ; Peephole label - removed redundant label %1
706 } if labelRefCount(%1 0)