-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeeph-z80.def
2212 lines (2005 loc) · 36.5 KB
/
peeph-z80.def
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Aim is to alter this file as little as neccessary
// peeph-z80.def - Z80 peephole rules
//
// Some of these peepholes could be potentially moved to peeph.def, but a
// GBZ80 expert should have a look at them before.
//
// (c) Philipp Klaus Krause (pkk@spth.de, philipp@colecovision.eu) 2006 - 2020
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option) any
// later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
replace restart {
ld %1, %1
} by {
; z80 peephole 0 removed redundant load from %1 into %1.
} if notVolatile(%1)
replace restart {
ld %1, %2
} by {
; z80 peephole 1 removed dead load from %2 into %1.
} if notVolatile(%1), notUsed(%1), notVolatile(%2), notSame(%1 '(hl+)' '(hl-)'), notSame(%2 '(hl+)' '(hl-)')
// will never be true
replace restart {
add ix,sp
} by {
; z80 peephole 2 removed dead frame pointer setup.
} if notUsed('ix')
replace restart {
ld %1, %2 + %3
} by {
; z80 peephole 3 removed dead load from %2 + %3 into %1.
} if notVolatile(%1), notUsed(%1), notSame(%1 '(hl+)' '(hl-)')
// Should probably check for notVolatile(%2), too, but gives many false positives and no regression tests fail.
// will never match
replace restart {
ld %1, (iy)
} by {
ld %1, 0 (iy)
; z80 peephole 4 made 0 offset explicit.
}
// will never match
replace restart {
ld (iy), %1
} by {
ld 0 (iy), %1
; z80 peephole 5 made 0 offset explicit.
}
replace restart {
inc hl
} by {
; z80 peephole 5a removed dead increment of hl.
} if notUsed('hl')
replace restart {
dec hl
} by {
; z80 peephole 6 removed dead decrement of hl.
} if notUsed('hl')
// will never match
replace restart {
ld %1, %2 (iy)
} by {
; z80 peephole 7 removed dead load from %2 (iy) into %1.
} if notUsed(%1)
// Should probably check for notVolatile(), but gives many false positives and no regression tests fail.
// will never match
replace restart {
ld %1, %2 (ix)
} by {
; z80 peephole 8 removed dead load from %2 (ix) into %1.
} if notUsed(%1)
replace restart {
ld %1, %2
ld %3, %1
} by {
; z80 peephole 9 loaded %3 from %2 directly instead of going through %1.
ld %3, %2
} if canAssign(%3 %2), notVolatile(%1), notUsed(%1), notSame(%3 '(hl)' '(de)' '(bc)' '(hl+)' '(hl-)'), notSame(%1 '(hl+)' '(hl-)')
replace restart {
ld %1, %2
ld %3, %1
} by {
; z80 peephole 9a loaded %3 from %2 directly instead of going through %1.
ld %3, %2
} if canAssign(%3 %2), notVolatile(%1), notUsed(%1), canAssign('a' %1), notSame(%1 '(hl+)' '(hl-)')
replace restart {
ld %1, hl
ld hl, %1
} by {
; z80 peephole 9b reused value still in hl.
ld %1, hl
} if notVolatile(%1), notSame(%1 '(hl+)' '(hl-)')
replace restart {
ld %1, %2
ld %3, %4
ld %5, %1
} by {
ld %5, %2
; z80 peephole 10 loaded %5 from %2 directly instead of going through %1.
ld %3, %4
} if canAssign(%5 %2), notVolatile(%1), operandsNotRelated(%1 %4), operandsNotRelated(%1 %3), operandsNotRelated(%3 %5), operandsNotRelated(%4 %5), notUsed(%1), notSame(%3 %4 '(hl)' '(de)' '(bc)' '(hl+)' '(hl-)'),
notVolatile(%5), notSame(%1 '(hl+)' '(hl-)'), notSame(%5 '(hl+)' '(hl-)')
// Rule OK unless both %5 and %4 are volatile, but we can't express that directly.
// will never match
replace restart {
ld %3 (ix), %1
ld %4 (ix), %2
ld %5, %3 (ix)
ld %6, %4 (ix)
} by {
ld %3 (ix), %1
ld %4 (ix), %2
; z80 peephole 11 loaded %5%6 from %1%2 directly instead of going through %3 (ix) %4 (ix).
ld %5, %1
ld %6, %2
} if canAssign(%5 %1), canAssign(%6 %2), notSame(%5 %2)
// gbz80 only has that syntax for lda
replace restart {
ld %1, %2 (%3)
ld %4, %1
} by {
; z80 peephole 12 loaded %2 (%3) into %4 directly instead of going through %1.
ld %4, %2 (%3)
} if canAssign(%4 %2 %3), notVolatile(%1), notUsed(%1)
replace restart {
ld %1, %2
ld %3 (%4), %1
} by {
; z80 peephole 13 loaded %2 into %3 (%4) directly instead of going through %1.
ld %3 (%4), %2
} if canAssign(%3 %4 %2), notVolatile(%1), notUsed(%1), canAssign('a' %1)
// canAssign ('a' %1) checks that %1 is an 8-bit value
replace restart {
ld %1, %2 (%3)
ld %4, %5 (%6)
ld %7, %1
} by {
ld %7, %2 (%3)
; z80 peephole 14 loaded %2 (%3) into %7 directly instead of going through %1.
ld %4, %5 (%6)
} if canAssign(%7 %2 %3), notVolatile(%1), notUsed(%1), notSame(%1 %4), notSame(%7 %4)
replace restart {
ld %1, %2 (%3)
ld %4, %5
ld %7, %1
} by {
ld %7, %2 (%3)
; z80 peephole 15 loaded %2 (%3) into %7 directly instead of going through %1.
ld %4, %5
} if canAssign(%7 %2 %3), notVolatile(%1), notUsed(%1), operandsNotRelated(%1 %5), operandsNotRelated(%7 %4), operandsNotRelated(%7 %5), notSame(%4 '(hl)' '(de)' '(bc)' '(hl+)' '(hl-)'), notSame(%5 '(hl)' '(de)' '(bc)' '(hl+)' '(hl-)' '(iy)')
replace restart {
ld %1,#%2
ld a,%3 (%1)
} by {
; z80 peephole 16 loaded %2 into a directly instead of going through %1.
ld a,(#%2 + %3)
} if notUsed(%1)
replace restart {
ld hl, #%1
ld a, (hl)
} by {
ld a, (#%1)
; z80 peephole 17 loaded a from (#%1) directly instead of using hl.
} if notUsed('hl')
replace restart {
ld hl,#%1 + %2
ld a,(hl)
} by {
; z80 peephole 18 loaded %2 into a directly instead of using hl.
ld a, (#%1 + %2)
} if notUsed('hl')
replace restart {
ld hl,#%1
ld (hl),a
} by {
ld (#%1),a
; z80 peephole 19 loaded (#%1) from a directly instead of using hl.
} if notUsed('hl')
replace restart {
ld hl,#%1 + %2
ld (hl),a
} by {
ld (#%1 + %2),a
; z80 peephole 20 loaded (#%1) from a directly instead of using hl.
} if notUsed('hl')
replace restart {
ld a, %1 (%2)
srl a
ld %1 (%2), a
} by {
srl %1 (%2)
ld a, %1 (%2)
; z80 peephole 21 shifted in (%2) instead of a.
}
replace restart {
ld %1,(hl)
ld a,%2 (%3)
sub a,%1
} by {
ld a,%2 (%3)
; z80 peephole 22 used (hl) in sub directly instead of going through %1.
sub a,(hl)
} if notVolatile(%1), notUsed(%1)
replace restart {
ld l,c
ld h,b
ld a,(hl)
inc hl
ld h,(hl)
ld l,a
} by {
ld a,(bc)
ld l,a
inc bc
ld a,(bc)
ld h,a
; z80 peephole 22a optimize 16-bit load from BC.
} if notUsed('bc'), notUsed('a')
replace restart {
inc bc
ld l,c
ld h,b
} by {
ld l,c
ld h,b
; z80 peephole 23 incremented in hl instead of bc.
inc hl
} if notUsed('bc')
replace restart {
inc de
ld l,e
ld h,d
} by {
ld l,e
ld h,d
; z80 peephole 24 incremented in hl instead of de.
inc hl
} if notUsed('de')
replace restart {
ld c,l
ld b,h
ld a,#%1
ld (bc),a
} by {
ld c,l
ld b,h
ld (hl),#%1
; z80 peephole 25 loaded #%1 into (hl) instead of (bc).
} if notUsed('a')
replace restart {
ex de, hl
push de
} by {
; z80 peephole 26 pushed hl directly instead of going through de.
push hl
} if notUsed('de'), notUsed('hl')
replace restart {
ld l,%1
ld h,d
push hl
} by {
; z80 peephole 27 pushed de instead of hl removing a load.
ld e,%1
push de
} if notUsed('hl'), notUsed('e')
replace restart {
ex de, hl
push bc
push de
} by {
; z80 peephole 28 pushed hl directly instead of going through de.
push bc
push hl
} if notUsed('de'), notUsed('hl')
replace restart {
ld l,c
ld h,b
push hl
} by {
; z80 peephole 29 pushed bc directly instead of going through hl.
push bc
} if notUsed('hl')
replace restart {
ld c,l
ld b,h
push %1
push bc
} by {
; z80 peephole 30 pushed hl directly instead of going through bc.
push %1
push hl
} if notUsed('bc'), notSame(%1 'bc')
replace restart {
pop de
ld l, e
ld h, d
} by {
; z80 peephole 31 popped hl directly instead of going through de.
pop hl
} if notUsed('de')
replace restart {
pop bc
ld l, c
ld h, b
} by {
; z80 peephole 32 popped hl directly instead of going through bc.
pop hl
} if notUsed('bc')
// won't match
replace restart {
ld %1 (ix), %2
ld %3, %1 (ix)
} by {
; z80 peephole 33 loaded %3 from %2 instead of going through %1 (ix).
ld %1 (ix), %2
ld %3, %2
} if canAssign(%3 %2)
// Don't need to check for volatile, since ix is used for the stack.
replace restart {
ld %1 (ix), a
push de
ld %2, %1 (ix)
} by {
ld %1 (ix), a
push de
; z80 peephole 34 loaded %2 from a instead of %1 (ix)
ld %2, a
} if canAssign(%2 'a')
// those are handy for gbz80
replace restart {
push af
inc sp
ld a,e
push af
inc sp
} by {
; z80 peephole 35 pushed de instead of pushing a twice.
ld d,a
push de
} if notUsed('d'), notUsed('a')
replace restart {
push af
inc sp
ld a,#%1
push af
inc sp
} by {
; z80 peephole 36 pushed de instead of pushing a twice.
ld d,a
ld e,#%1
push de
} if notUsed('de')
replace restart {
push af
inc sp
ld a,#%1
push af
inc sp
} by {
; z80 peephole 37 pushed bc instead of pushing a twice.
ld b,a
ld c,#%1
push bc
} if notUsed('bc')
replace restart {
push hl
inc sp
push de
inc sp
} by {
ld l, d
; z80 peephole 38 combined pushing of h and d.
push hl
} if notUsed('l')
replace restart {
push bc
inc sp
push de
inc sp
} by {
ld c, d
; z80 peephole 39 combined pushing of b and d.
push bc
} if notUsed('c')
replace restart {
push de
inc sp
push bc
inc sp
} by {
ld e, b
; z80 peephole 41 combined pushing of d and b.
push de
} if notUsed('e')
replace restart {
push af
inc sp
ld a, c
push af
inc sp
} by {
ld b, a
; z80 peephole 42 combined pushing of a and c.
push bc
} if notUsed('b'), notUsed('a')
replace restart {
push de
inc sp
ld a, c
push af
inc sp
} by {
ld e, c
; z80 peephole 43 combined pushing of d and c.
push de
} if notUsed('e'), notUsed('a')
replace restart {
ld a, e
push af
inc sp
ld a, %2 (ix)
push af
inc sp
} by {
ld d, e
ld e, %2 (ix)
; z80 peephole 44 combined pushing of e and %2 (ix).
push de
} if notUsed('d'), notUsed('e'), notUsed('a')
replace restart {
ld a,#%1
ld d,a
} by {
; z80 peephole 45 loaded #%1 into d directly instead of going through a.
ld d,#%1
} if notUsed('a')
replace restart {
ld %1,a
ld %2,%1
} by {
; z80 peephole 46 loaded %2 from a directly instead of going through %1.
ld %2,a
} if notUsed(%1)
replace restart {
ld a, %1 (%3)
push af
inc sp
ld a, %2 (%3)
push af
inc sp
} by {
; z80 peephole 47 pushed %1 (%3), %2 (%3) through hl instead of af.
ld h, %1 (%3)
ld l, %2 (%3)
push hl
} if notUsed('a'), notUsed('hl')
replace restart {
ld c, l
ld b, h
push bc
} by {
; z80 peephole 48 pushed hl instead of bc.
push hl
} if notUsed('bc')
replace restart {
ld a, (hl)
inc hl
ld h, (hl)
ld l, a
push hl
} by {
; z80 peephole 49 pushed bc instead of hl.
ld c, (hl)
inc hl
ld b, (hl)
push bc
} if notUsed('bc'), notUsed('hl')
replace restart {
pop %1
push %1
} by {
; z80 peephole 50 eleminated dead pop/push pair.
} if notUsed(%1)
replace restart {
pop %1
ld %2 (ix), %3
push %1
} by {
; z80 peephole 51 eleminated dead pop/push pair.
ld %2 (ix), %3
} if notUsed(%1), operandsNotRelated(%1 %3)
replace restart {
push hl
pop bc
} by {
ld c, l
ld b, h
; z80 peephole 52 replaced push/pop pair by loads.
} if notUsed('hl')
replace restart {
push hl
pop de
} by {
ld e, l
ld d, h
; z80 peephole 53 replaced push/pop pair by loads.
} if notUsed('hl')
replace restart {
ld iy,#%1
or a,%2 (iy)
} by {
; z80 peephole 54 used hl instead of iy.
ld hl,#%1 + %2
or a,(hl)
} if notUsed('iy'), notUsed('hl')
replace restart {
ld iy,#%1
ld %2,%3 (iy)
} by {
; z80 peephole 55 used hl instead of iy.
ld hl,#%1 + %3
ld %2, (hl)
} if notUsed('iy'), notUsed('hl')
replace restart {
ld iy,#%1
ld h,%3 (iy)
} by {
; z80 peephole 56 used hl instead of iy.
ld hl,#%1 + %3
ld h, (hl)
} if notUsed('iy'), notUsed('l')
replace restart {
ld iy,#%1
ld %2 (iy), %3
} by {
; z80 peephole 57 used hl instead of iy.
ld hl,#%1 + %2
ld (hl), %3
} if notUsed('iy'), notUsed('hl'), notSame(%3 'h' 'l')
replace restart {
ld iy,#%1
ld %2,0 (iy)
ld %3,1 (iy)
} by {
; z80 peephole 58 used hl instead of iy.
ld hl,#%1
ld %2, (hl)
inc hl
ld %3, (hl)
} if notUsed('iy'), notUsed('hl'), operandsNotRelated(%2 'hl')
replace restart {
ld iy,#%1
ld %2 (iy),%3
ld l,%2 (iy)
} by {
; z80 peephole 59 used hl instead of iy.
ld hl,#%1 + %2
ld (hl),%3
ld l,(hl)
} if notUsed('iy'), notUsed('h')
replace restart {
ld iy,#%1
ld %2 (%3), %4
} by {
; z80 peephole 60 used hl instead of iy.
ld hl,#%1 + %2
ld (hl), %4
} if notUsed('iy'), notUsed('hl'), operandsNotRelated(%4 'hl')
replace restart {
ld iy,#%1
bit %2,%3 (iy)
} by {
; z80 peephole 61 used hl instead of iy.
ld hl,#%1+%3
bit %2, (hl)
} if notUsed('iy'), notUsed('hl')
replace restart {
ld iy, #%1
add iy, sp
ld %2, %3 (iy)
} by {
; z80 peephole 62 used hl instead of iy.
ld hl, #%1+%3
add hl, sp
ld %2, (hl)
} if notUsed('iy'), notUsed('hl')
replace restart {
ld iy, #%1
add iy, sp
ld %2, 0 (iy)
ld %3, 1 (iy)
} by {
; z80 peephole 63 used hl instead of iy.
ld hl, #%1
add hl, sp
ld %2, (hl)
inc hl
ld %3, (hl)
} if notUsed('iy'), notUsed('hl')
replace restart {
ld iy, #%1
add iy, sp
ld l, 0 (iy)
ld h, 1 (iy)
} by {
; z80 peephole 64 used hl instead of iy.
ld hl, #%1
add hl, sp
ld a, (hl)
inc hl
ld h, (hl)
ld l, a
} if notUsed('iy'), notUsed('a')
replace restart {
ld iy, #%1
add iy, sp
ld 0 (iy), #%2
ld 1 (iy), #%3
} by {
; z80 peephole 65 used hl instead of iy.
ld hl, #%1
add hl, sp
ld (hl), #%2
inc hl
ld (hl), #%3
} if notUsed('iy'), notUsed('hl')
replace restart {
ld iy, #%1
add iy, sp
ld a, 1 (iy)
or a, 0 (iy)
} by {
ld hl, #%1+1
add hl, sp
ld a, (hl)
dec hl
or a, (hl)
; z80 peephole 66 used hl instead of iy.
} if notUsed('iy'), notUsed('hl')
replace restart {
ld iy, #%1
add iy, sp
bit %2, %3 (iy)
} by {
ld hl, #%1+%3
add hl, sp
bit %2, (hl)
; z80 peephole 67 used hl instead of iy.
} if notUsed('iy'), notUsed('hl')
replace restart {
ld iy, #%1
add iy, sp
or a, %2 (iy)
} by {
ld hl, #%1+%2
add hl, sp
or a, (hl)
; z80 peephole 68 used hl instead of iy.
} if notUsed('iy'), notUsed('hl')
replace restart {
ld c,l
ld b,h
inc bc
} by {
; z80 peephole 69 incremented in hl instead of bc.
inc hl
ld c,l
ld b,h
} if notUsed('hl')
replace restart {
ld iy, #%1
inc %2 (iy)
} by {
; z80 peephole 70 incremented in (hl) instead of %2 (iy).
ld hl, #%1+%2
inc (hl)
} if notUsed('hl'), notUsed('iy')
replace restart {
ld iy, #%1
dec %2 (iy)
} by {
; z80 peephole 71 decremented in (hl) instead of %2 (iy).
ld hl, #%1+%2
dec (hl)
} if notUsed('hl'), notUsed('iy')
replace restart {
ld a,%1 (%2)
bit %3,a
} by {
; z80 peephole 72 tested bit %3 of %1 (%2) directly instead of going through a.
bit %3,%1 (%2)
} if notUsed('a')
replace restart {
ld %1, (hl)
bit %2, %1
} by {
; z80 peephole 73 tested bit %2 of (hl) directly instead of going through %1.
bit %2, (hl)
} if notUsed(%1)
replace restart {
ld a,%1
bit %2,a
} by {
; z80 peephole 74 tested bit %2 of %1 directly instead of going through a.
bit %2,%1
} if notUsed('a'), canAssign(%1 'b')
replace restart {
ld a, %1
set %2, a
ld %1, a
} by {
; z80 peephole 75 set bit %2 of %1 directly instead of going through a.
set %2, %1
ld a, %1
} if canAssign(%1 'b'), notSame(%1 '(hl+)' '(hl-)')
// canAssign(%1 'b') is true, iff set b, %1 is possible.
replace restart {
ld a, %1 (%2)
set %3, a
ld %1 (%2), a
} by {
; z80 peephole 76 set bit %3 of %1 (%2) directly instead of going through a.
set %3, %1 (%2)
ld a, %1 (%2)
}
replace restart {
ld a, %1
res %2, a
ld %1, a
} by {
; z80 peephole 77 reset bit %2 of %1 directly instead of going through a.
res %2, %1
ld a, %1
} if canAssign(%1 'b'), notSame(%1 '(hl+)' '(hl-)')
// canAssign(%1 'b') is true, iff set b, %1 is possible.
replace restart {
ld a, %1 (%2)
res %3, a
ld %1 (%2), a
} by {
; z80 peephole 78 reset bit %3 of %1 (%2) directly instead of going through a.
res %3, %1 (%2)
ld a, %1 (%2)
}
replace restart {
jp NC,%1
jp %2
%1:
} by {
jp C,%2
; z80 peephole 79 removed jp by using inverse jump logic
%1:
} if labelRefCountChange(%1 -1)
replace restart {
jp C,%1
jp %2
%1:
} by {
jp NC,%2
; z80 peephole 80 removed jp by using inverse jump logic
%1:
} if labelRefCountChange(%1 -1)
replace restart {
jp NZ,%1
jp %2
%1:
} by {
jp Z,%2
; z80 peephole 81 removed jp by using inverse jump logic
%1:
} if labelRefCountChange(%1 -1)
replace restart {
jp Z,%1
jp %2
%1:
} by {
jp NZ,%2
; z80 peephole 82 removed jp by using inverse jump logic
%1:
} if labelRefCountChange(%1 -1)
replace restart {
jp %5
} by {
jp %6
; z80 peephole 83 jumped to %6 directly instead of via %5.
} if labelIsUncondJump(), notSame(%5 %6), labelRefCountChange(%5 -1), labelRefCountChange(%6 +1)
replace restart {
jp %1,%5
} by {
jp %1,%6
; z80 peephole 84 jumped to %6 directly instead of via %5.
} if labelIsUncondJump(), notSame(%5 %6), labelRefCountChange(%5 -1), labelRefCountChange(%6 +1)
replace restart {
jp %1
%2:
%1:
} by {
; z80 peephole 85 eliminated jump.
%2:
%1:
} if labelRefCountChange(%1 -1)
// A peephole that makes the code longer. Let's hope it's worth it in speed gain and further optimization potential.
replace restart {
ld a,#0x00
%1:
bit %2,a
jp Z,%3
} by {
ld a,#0x00
jp %3
; z80 peephole 86 jumped directly to %3 instead of testing a first.
%1:
bit %2,a
jp Z,%3
} if labelRefCountChange(%3 +1)
replace restart {
ld %1, %2
jp %3
jp %4
} by {
ld %1, %2
jp %3
; z80 peephole 87 removed unreachable jump to %3.
}
replace restart {
ld %1, %2
jp %3
%3:
} by {
ld %1, %2
%3:
; z80 peephole 88 removed redundant jump to %3.
} if labelRefCountChange(%3 -1)
replace restart {
ld %1, #0x01
bit 0, %1
jp Z, %2
} by {
ld %1, #0x01
; z80 peephole 89 removed impossible jump to %2.
} if labelRefCountChange(%2 -1)
replace restart {
rlca
and a, #0x01
jp Z, %1
} by {
rlca
; z80 peephole 90 removed and by changing jump condition.
jp NC, %1
} if notUsed('a'), notUsedFrom(%1 'a')
replace restart {
rlca
and a, #0x01
jp NZ, %1
} by {
rlca
; z80 peephole 91 removed and by changing jump condition.
jp C, %1
} if notUsed('a'), notUsedFrom(%1 'a')
replace {
ld e, #0x%1
ld d, #0x%2
} by {
ld de, #0x%2%1
; z80 peephole 92 combined constant loads into register pair.
}
replace {
ld d, #0x%1
ld e, #0x%2
} by {
ld de, #0x%1%2
; z80 peephole 93 combined constant loads into register pair.
}
replace {
ld l, #0x%1
ld h, #0x%2
} by {
ld hl, #0x%2%1
; z80 peephole 94 combined constant loads into register pair.
}
replace {
ld h, #0x%1
ld l, #0x%2
} by {
ld hl, #0x%1%2
; z80 peephole 95 combined constant loads into register pair.
}
replace {
ld c, #0x%1
ld b, #0x%2
} by {