summaryrefslogtreecommitdiff
path: root/src/z80/peeph-gbz80.def
blob: 6116cc65a469b322d327418d2e9f9a22a4cb3c53 (plain) (blame)
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
// Due to major changes in the Z80 peepholes the old peepholes from peeph.def have been moved here.
// All new peepholes went into peeph-z80.def. A GBZ80 expert should look into this files and peeph-z80.def
// And move peepholes that are useful and correct for both Z80 and GBZ80 into peeph.def.

// peeph.def - Common Z80 and gbz80 peephole rules
//
//
// (c) Philipp Klaus Krause (pkk@spth.de, philipp@colecovision.eu) 2006 - 2007
//
// 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 {
	; peephole 1 removed redundant load.
} if notVolatile(%1)

replace restart {
	ldhl	sp,#%1
	dec	hl
} by {
	; peephole 2 combined ld and dec.
	ldhl	sp,#(%1 - 1)
}

replace restart {
	jp	NC,%1
	jp	%2
%1:
} by {
	jp	C,%2
	; peephole 3 removed jp by using inverse jump logic
%1:
} if labelRefCountChange(%1 -1)

replace restart {
	jp	C,%1
	jp	%2
%1:
} by {
	jp	NC,%2
	; peephole 4 removed jp by using inverse jump logic
%1:
} if labelRefCountChange(%1 -1)

replace restart {
	jp	NZ,%1
	jp	%2
%1:
} by {
	jp	Z,%2
	; peephole 5 removed jp by using inverse jump logic
%1:
} if labelRefCountChange(%1 -1)

replace restart {
	jp	Z,%1
	jp	%2
%1:
} by {
	jp	NZ,%2
	; peephole 6 removed jp by using inverse jump logic
%1:
} if labelRefCountChange(%1 -1)

replace restart {
	jp	%5
} by {
	jp	%6
	; peephole 7 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
	; peephole 8 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 {
   ; peephole 9 eliminated jump.
%2:
%1:
} if labelRefCountChange(%1 -1)

replace {
	ld	e,#0x%1
	ld	d,#0x%2
} by {
	ld	de,#0x%2%1
	; peephole 10 combined constant loads into register pair.
}

replace {
	ld	l,#0x%1
	ld	h,#0x%2
} by {
	ld	hl,#0x%2%1
	; peephole 11 combined constant loads into register pair.
}

replace {
	ld	c,#0x%1
	ld	b,#0x%2
} by {
	ld	bc,#0x%2%1
	; peephole 12 combined constant loads into register pair.
}

replace restart {
	ld	%1,a
	ld	a,%1
} by {
	ld	%1,a
	; peephole 13 removed redundant load from %1 into a.
} if notVolatile(%1), notSame(%1 '(hl+)'), notSame(%1 '(hl-)')

replace restart {
	ld	a,%1
	ld	%1,a
} by {
	ld	a,%1
	; peephole 14 removed redundant load from a into %1.
} if notVolatile(%1), notSame(%1 '(hl+)'), notSame(%1 '(hl-)')

replace restart {
	and	a,%1
	or	a,a
} by {
	and	a,%1
	; peephole 15 removed redundant or after and.
}

replace restart {
	xor	a,%1
	or	a,a
} by {
	xor	a,%1
	; peephole 16 removed redundant or after xor.
}

replace restart {
	ld	%1,a
	and	a,%2
	ld	%1,a
} by {
	; peephole 17 removed redundant load into %1.
	and	a,%2
	ld	%1,a
} if notVolatile(%1)

replace {
	ld	%1,%2
	ld	a,%2
} by {
	ld	a,%2
	ld	%1,a
	; peephole 18 load value in a first and use it next
} if notVolatile(%1 %2)

replace restart {
	ld	%1,%2
	ld	%3,%4
	ld	%2,%1
	ld	%4,%3
} by {
	ld	%1,%2
	ld	%3,%4
	; peephole 19 removed redundant load from %3%1 into %4%2
} if notVolatile(%1 %2 %3 %4)

replace restart {
	ld	a,%1
	sub	a,%2
	jp	%3,%4
	ld	a,%1
} by {
	ld	a,%1
	cp	a,%2
	jp	%3,%4
	; peephole 20 removed load by replacing sub with cp
	assert	a=%1
} if notVolatile(%1)

replace restart {
	assert	a=%1
	sub	a,%2
	jp	%3,%4
	ld	a,%1
} by {
	cp	a,#%2
	jp	%3,%4
	; peephole 21 removed load by replacing sub with cp
	assert	a=%1
}

replace restart {
	assert	a=%1
} by {
}

replace restart {
	rlca
	ld	a,#0x00
	rla
} by {
	rlca
	and	a,#0x01
	; peephole 22 replaced zero load, rla by and since rlca writes the same value to carry bit and least significant bit.
}

replace restart {
	add	a,#0x00
	ld	%2,a
	ld	a,%3
	adc	a,%4
} by {
	; peephole 23 removed lower part of multibyte addition.
	ld	%2,a
	ld	a,%3
	add	a,%4
}

replace restart {
	or	a,%1
	jp	NZ,%2
	ld	%3,#0x00
} by {
	or	a,%1
	jp	NZ,%2
	ld	%3,a
	; peephole 24 replaced constant #0x00 by a (which has just been tested to be #0x00).
}

replace restart {
	and	a,%1
	jp	NZ,%2
	ld	%3,#0x00
} by {
	and	a,%1
	jp	NZ,%2
	ld	%3,a
	; peephole 25 replaced constant #0x00 by a (which has just been tested to be #0x00).
}

replace restart {
	ld	bc,#%1 + %2
	inc	bc
	inc	bc
	inc	bc
} by {
	ld	bc,#%1 + %2 + 3
	; peephole 26 moved triple increment of bc to constant.
}

replace restart {
	ld	bc,#%1 + %2
	inc	bc
	inc	bc
} by {
	ld	bc,#%1 + %2 + 2
	; peephole 27 moved double increment of bc to constant.
}

replace restart {
	ld	bc,#%1 + %2
	inc	bc
} by {
	ld	bc,#%1 + %2 + 1
	; peephole 28 moved increment of bc to constant.
}

replace restart {
	ld	bc,#%1
	ld	a,c
	add	a,#0x%2
	ld	c,a
	ld	a,b
	adc	a,#0x%3
	ld	b,a
} by {
	ld	bc,#%1 + 0x%3%2
	; peephole 29 moved addition of constant 0x%3%2 to bc to constant.
}

replace restart {
	ld	bc,#%1 + %4
	ld	a,c
	add	a,#0x%2
	ld	c,a
	ld	a,b
	adc	a,#0x%3
	ld	b,a
} by {
	ld	bc,#%1 + %4 + 0x%3%2
	; peephole 30 moved addition of constant 0x%3%2 to bc to constant.
}

replace restart {
	call	%1
	ret
} by {
	jp	%1
	; peephole 31 replaced call at end of function by jump.
}

replace restart {
	jp	Z, %2
	ld	a, #%3
	jp	%1
%2:
	ld	a, #%4
%1:
} by {
	ld	a, #%3
	jp	NZ, %1
%2:
	ld	a, #%4
	; peephole 168z used double assignment in case of Z condition.
%1:
} if labelRefCountChange(%2 -1) 

replace restart {
	jp	NZ, %2
	ld	a, #%3
	jp	%1
%2:
	ld	a, #%4
%1:
} by {
	ld	a, #%3
	jp	Z, %1
%2:
	ld	a, #%4
	; peephole 168nz used double assignment in case of NZ condition.
%1:
} if labelRefCountChange(%2 -1) 

replace restart {
	jp	Z, %2
	ld	a, #%3
	jp	%1
%2:
	xor	a, a
%1:
} by {
	ld	a, #%3
	jp	NZ, %1
	; peephole 169xz used double assignment in case of Z condition.
%2:
	xor	a, a
%1:
} if labelRefCountChange(%2 -1)

replace restart {
	jp	NZ, %2
	ld	a, #%3
	jp	%1
%2:
	xor	a, a
%1:
} by {
	ld	a, #%3
	jp	Z, %1
	; peephole 169xnz used double assignment in case of NZ condition.
%2:
	xor	a, a
%1:
} if labelRefCountChange(%2 -1)

replace restart {
	jp	Z, %2
	ld	c, #%3
	jp	%1
%2:
	ld	c, #%4
%1:
} by {
	ld	c, #%3
	jp	NZ, %1
%2:
	ld	c, #%4
	; peephole 170z used double assignment in case of Z condition.
%1:
} if labelRefCountChange(%2 -1)

replace restart {
	jp	NZ, %2
	ld	c, #%3
	jp	%1
%2:
	ld	c, #%4
%1:
} by {
	ld	c, #%3
	jp	Z, %1
%2:
	ld	c, #%4
	; peephole 170nz used double assignment in case of NZ condition.
%1:
} if labelRefCountChange(%2 -1)

replace restart {
	jp	Z, %2
	ld	e, #%3
	jp	%1
%2:
	ld	e, #%4
%1:
} by {
	ld	e, #%3
	jp	NZ, %1
%2:
	ld	e, #%4
	; peephole 171z used double assignment in case of Z condition.
%1:
} if labelRefCountChange(%2 -1)

replace restart {
	jp	NZ, %2
	ld	e, #%3
	jp	%1
%2:
	ld	e, #%4
%1:
} by {
	ld	e, #%3
	jp	Z, %1
%2:
	ld	e, #%4
	; peephole 171nz used double assignment in case of NZ condition.
%1:
} if labelRefCountChange(%2 -1)

replace restart {
	jp	Z, %2
	ld	l, #%3
	jp	%1
%2:
	ld	l, #%4
%1:
} by {
	ld	l, #%3
	jp	NZ, %1
%2:
	ld	l, #%4
	; peephole 172z used double assignment in case of Z condition.
%1:
} if labelRefCountChange(%2 -1)

replace restart {
	jp	NZ, %2
	ld	l, #%3
	jp	%1
%2:
	ld	l, #%4
%1:
} by {
	ld	l, #%3
	jp	Z, %1
%2:
	ld	l, #%4
	; peephole 172nz used double assignment in case of NZ condition.
%1:
} if labelRefCountChange(%2 -1)

barrier

// Should be one of the last ones. Opens the code to further peephole optimization.
replace restart {
%1:
} by {
	; peephole 32 removed unused label %1.
} if labelRefCount(%1 0)

// Ensure that all rules above see only jp, not jr.
barrier

replace {
	add	hl,de
	pop	de
	jp	(hl)
%1:
	jp	%5
	jp	%6
	jp	%7
%2:
} by {
	; peephole 150-3 removed addition using short jumps in jump-table.
	pop	de
	jp	(hl)
%1:
	jr	%5
	jr	%6
	jr	%7
%2:
} if labelJTInRange

replace {
	add	hl,de
	jp	(hl)
%1:
	jp	%5
	jp	%6
	jp	%7
%2:
} by {
	; peephole 150-3' removed addition using short jumps in jump-table.
	jp	(hl)
%1:
	jr	%5
	jr	%6
	jr	%7
%2:
} if labelJTInRange

replace {
	add	hl,de
	pop	de
	jp	(hl)
%1:
	jp	%5
	jp	%6
	jp	%7
	jp	%8
%2:
} by {
	; peephole 150-4 removed addition using short jumps in jump-table.
	pop	de
	jp	(hl)
%1:
	jr	%5
	jr	%6
	jr	%7
	jr	%8
%2:
} if labelJTInRange

replace {
	add	hl,de
	jp	(hl)
%1:
	jp	%5
	jp	%6
	jp	%7
	jp	%8
%2:
} by {
	; peephole 150-4' removed addition using short jumps in jump-table.
	jp	(hl)
%1:
	jr	%5
	jr	%6
	jr	%7
	jr	%8
%2:
} if labelJTInRange

replace {
	add	hl,de
	pop	de
	jp	(hl)
%1:
	jp	%5
	jp	%6
	jp	%7
	jp	%8
	jp	%9
%2:
} by {
	; peephole 150-5 removed addition using short jumps in jump-table.
	pop	de
	jp	(hl)
%1:
	jr	%5
	jr	%6
	jr	%7
	jr	%8
	jr	%9
%2:
} if labelJTInRange

replace {
	add	hl,de
	jp	(hl)
%1:
	jp	%5
	jp	%6
	jp	%7
	jp	%8
	jp	%9
%2:
} by {
	; peephole 150-5' removed addition using short jumps in jump-table.
	jp	(hl)
%1:
	jr	%5
	jr	%6
	jr	%7
	jr	%8
	jr	%9
%2:
} if labelJTInRange

replace {
	add	hl,de
	pop	de
	jp	(hl)
%1:
	jp	%5
	jp	%6
	jp	%7
	jp	%8
	jp	%9
	jp	%10
%2:
} by {
	; peephole 150-6 removed addition using short jumps in jump-table.
	pop	de
	jp	(hl)
%1:
	jr	%5
	jr	%6
	jr	%7
	jr	%8
	jr	%9
	jr	%10
%2:
} if labelJTInRange

replace {
	add	hl,de
	jp	(hl)
%1:
	jp	%5
	jp	%6
	jp	%7
	jp	%8
	jp	%9
	jp	%10
%2:
} by {
	; peephole 150-6' removed addition using short jumps in jump-table.
	jp	(hl)
%1:
	jr	%5
	jr	%6
	jr	%7
	jr	%8
	jr	%9
	jr	%10
%2:
} if labelJTInRange

replace {
	add	hl,de
	pop	de
	jp	(hl)
%1:
	jp	%5
	jp	%6
	jp	%7
	jp	%8
	jp	%9
	jp	%10
	jp	%11
%2:
} by {
	; peephole 150-7 removed addition using short jumps in jump-table.
	pop	de
	jp	(hl)
%1:
	jr	%5
	jr	%6
	jr	%7
	jr	%8
	jr	%9
	jr	%10
	jr	%11
%2:
} if labelJTInRange

replace {
	add	hl,de
	jp	(hl)
%1:
	jp	%5
	jp	%6
	jp	%7
	jp	%8
	jp	%9
	jp	%10
	jp	%11
%2:
} by {
	; peephole 150-7' removed addition using short jumps in jump-table.
	jp	(hl)
%1:
	jr	%5
	jr	%6
	jr	%7
	jr	%8
	jr	%9
	jr	%10
	jr	%11
%2:
} if labelJTInRange

barrier

// Do all jump optimizations before replacing by ret.

replace restart {
	jp	%5
} by {
	ret
	; peephole 151 replaced jump by return.
} if labelIsReturnOnly(%5), labelRefCountChange(%5 -1)

replace restart {
	jp	%1,%5
} by {
	ret	%1
	; peephole 152 replaced jump by return.
} if labelIsReturnOnly(%5), labelRefCountChange(%5 -1)

replace {
	jp	%5
} by {
	jr	%5
	; peephole 153 changed absolute to relative unconditional jump.
} if labelInRange(%5)

replace {
	jp	%1,%5
} by {
	jr	%1,%5
	; peephole 154 changed absolute to relative conditional jump.
} if labelInRange(%5)

barrier

// Do not try further peephole optimization after these, as other peepholes e.g. might try to generate add a, (hl-), etc otherwise.

replace {
	ld	a, (hl)
	inc	hl
} by {
	ld	a, (hl+)
	; peephole GB1 used ldi to increment hl after load
}

replace {
	ld	a, (hl)
	dec	hl
} by {
	ld	a, (hl-)
	; peephole GB2 used ldd to decrement hl after load
}

replace {
	ld	(hl), a
	inc	hl
} by {
	ld	(hl+), a
	; peephole GB3 used ldi to increment hl after load
}

replace {
	ld	(hl), a
	dec	hl
} by {
	ld	(hl-), a
	; peephole GB4 used ldd to decrement hl after load
}

replace {
	inc hl
	dec hl
} by {
	; peephole GB5 removed inc hl / dec hl pair
}

replace {
	ld	(hl), a
	inc	de
	ld	a, (de)
	inc	hl
} by {
	ld	(hl+), a
	; peephole GB6 used ldi to increment hl
	inc	de
	ld	a, (de)
}

replace {
	ld	a, (hl)
	ld	(bc), a
	inc	bc
	inc	hl
} by {
	ld	a, (hl+)
	; peephole GB7 used ldi to increment hl
	ld	(bc), a
	inc	bc
}