1 // peeph.def - Common Z80 and gbz80 peephole rules
3 // These peepholes could be potentially moved to peeph.def, but a GBZ80 expert
4 // Should have a look at them before.
6 // (c) Philipp Klaus Krause (pkk@spth.de, philipp@colecovision.eu) 2006 - 2008
8 // This program is free software; you can redistribute it and/or modify it
9 // under the terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option) any
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 ; peephole -1 removed redundant load.
28 // This one doesn't work currently since the peephole optimizer can't match lines generated by multiline macros: Bug #1570701
33 // ; peephole -1a eleminated dead pop.
40 ; peephole 0a removed dead load from %2 into %1.
41 } if notVolatile(%1), notUsed(%1)
42 // Should probably check for notVolatile(%2), too, but gives many false positives and no regression tests fail.
48 ; peephole 0b loaded %2 into a directly instead of going through %1.
50 } if notVolatile(%1), notUsed(%1)
56 ; peephole 0c loaded %2 (%3) into a directly instead of going through %1.
58 } if notVolatile(%1), notUsed(%1)
64 ; peephole 0d loaded %2 into a directly instead of going through %1.
73 ; peephole 0e shifted in a instead of %1.
75 } if notVolatile(%1), notUsed(%1)
83 ; peephole 0f used (hl) in sub directly instead of going through %1.
85 } if notVolatile(%1), notUsed(%1)
94 ; peephole 0g incremented in hl instead of bc.
98 // Catch double and triple incs before later peepholes introduce an ex de,hl in here.
107 ; peephole 0h' incremented in hl instead of de.
119 ; peephole 0h incremented in hl instead of de.
129 ; peephole 0i loaded %2%1 into hl directly instead of going through de.
139 ; peephole 0j used hl for #%2 + %3 instead of bc, not going through a.
142 } if notUsed('a'), notUsed('hl')
149 ; peephole 0j' used hl for #%2 + %3 instead of de, not going through a.
152 } if notUsed('a'), notUsed('hl')
158 ; peephole 0k pushed de directly instead of going through hl.
160 } if notUsed('de'), notUsed('hl')
166 ; peephole 0k' pushed hl directly instead of going through de.
168 } if notUsed('de'), notUsed('hl')
175 ; peephole 0k'' pushed de instead of hl removing a load.
178 } if notUsed('hl'), notUsed('e')
185 ; peephole 0l pushed hl directly instead of going through de.
188 } if notUsed('de'), notUsed('hl')
195 ; peephole 0m pushed bc directly instead of going through hl.
204 ; peephole 0m' pushed bc instead of hl removing a load.
207 } if notUsed('hl'), notUsed('c')
214 ; peephole 0n removed redundant load of a through c.
223 ; peephole 0o used de directly instead of going through bc.
232 ; peephole 0p popped hl directly instead of going through de.
240 ; peephole 0q loaded a into %2 (%3) directly instead of going through %1.
242 } if notVolatile(%1), notUsed(%1)
248 ; peephole 0r loaded %3 from %2 instead of going through %1 (ix).
252 // Don't need to check for volatile, since ix is used for the stack.
258 ; peephole 0s loaded b from %2 (%3) instead of going through %1.
260 } if notVolatile(%1), notUsed(%1)
268 // ; peephole 0t loaded %4 from a instead of going through %1.
272 //} if notVolatile(%1), operandsNotRelated(%1 %3), operandsNotRelated(%1 %2)
281 ; peephole 0t' loaded %4 from a instead of going through %1.
285 } if notVolatile(%1), operandsNotRelated(%1 %2), operandsNotRelated(%4 %2)
294 ; peephole 0t'' loaded %4 from a instead of going through %1.
304 ; peephole 0u loaded e from (hl) instead of going through %1.
312 ; peephole 0v loaded %2 (%3) from l instead of going through %1.
327 ; peephole 0w shifted (hl) in place.
331 // Don't check for volatile since ix points to the stack.
340 ; peephole 0x pushed de instead of pushing a twice.
343 } if notUsed('d'), notUsed('a')
352 ; peephole 0y pushed bc instead of pushing a twice.
355 } if notUsed('b'), notUsed('a')
361 ; peephole 0z loaded #%1 into d directly instead of going through a.
373 ; peephole 0za pushed %1 (ix), %2(ix) through hl instead of af.
377 } if notUsed('a'), notUsed('hl')
384 ; peephole 0zb pushed hl instead of bc.
388 // Doesn'T work due to bug #1947081
393 // ; peephole 0zc eleminated dead pop/push pair.
400 ; peephole 0zd used hl instead of iy.
403 } if notUsed('iy'), notUsed('hl')
409 ; peephole 0ze used hl instead of iy.
412 } if notUsed('iy'), notUsed('hl')
418 ; peephole 0zf used hl instead of iy.
421 } if notUsed('iy'), notUsed('hl'), operandsNotRelated(%4 'h'), operandsNotRelated(%4 'l')
428 ; peephole 0zg loaded %1 from (hl) directly instead of going through (de).
437 ; peephole 0zh loaded %1 from (hl) directly instead of going through (bc).
446 ; peephole 0zi incremented in hl instead of bc.
456 ; peephole 0zj tested bit of %1 (%2) directly instead of going through a.
465 ; peephole 0zk stored constant #%1 + %2 into hl directly instead of going through bc.
475 ; peephole 0zk' stored %1 (%2) %3 (%4) into hl directly instead of going through bc.
486 ; peephole 3 removed jp by using inverse jump logic
488 } if labelRefCountChange(%1 -1)
496 ; peephole 4 removed jp by using inverse jump logic
498 } if labelRefCountChange(%1 -1)
506 ; peephole 5 removed jp by using inverse jump logic
508 } if labelRefCountChange(%1 -1)
516 ; peephole 6 removed jp by using inverse jump logic
518 } if labelRefCountChange(%1 -1)
524 ; peephole 7 redirected jump-to-jump at %5 by jump to %6
525 } if labelIsUncondJump(), labelRefCountChange(%5 -1), labelRefCountChange(%6 +1)
531 ; peephole 8 redirected jump-to-jump at %5 by jump to %6
532 } if labelIsUncondJump(), labelRefCountChange(%5 -1), labelRefCountChange(%6 +1)
539 ; peephole 10 removed redundant load of 0 into a.
547 ; peephole 11 combined constant loads into register pair.
555 ; peephole 12 combined constant loads into register pair.
563 ; peephole 13 combined constant loads into register pair.
571 ; peephole 14 removed redundant load from %1 into a.
573 // This gives many false negatives and without the test no problems are encountered in the regression tests
574 // Maybe we can try this after 2.7.0 release
581 ; peephole 15 removed redundant load from a into %1.
583 // This gives many false negatives and without the test no problems are encountered in the regression tests
584 // Maybe we can try this after 2.7.0 release
593 ; peephole 17a removed load by reordering or arguments.
603 ; peephole 17b removed load by reordering or arguments.
613 ; peephole 18 used value still in a instead of loading it from %1.
621 ; peephole 19 removed redundant or after or.
629 ; peephole 19a removed redundant or after or.
637 ; peephole 20 removed redundant or after and.
645 ; peephole 20a removed redundant or after and.
653 ; peephole 21 removed redundant or after xor.
661 ; peephole 21a removed redundant or after xor.
670 ; peephole 23 load value in a first and use it next
671 } if notVolatile(%1 %2)
681 ; peephole 24 removed redundant load from %3%1 into %4%2
682 } if notVolatile(%1 %2 %3 %4)
696 ; peephole 31 moved and pushed arguments c and #%2 through bc instead of pushing them individually.
712 ; peephole 32 moved and pushed arguments e and #%2 through de instead of pushing them individually.
725 ; peephole 33 removed load by replacing sub with cp
737 ; peephole 34 removed load by replacing sub with cp
751 ; peephole 35 replaced sub a,#0xFF by inc a.
760 ; peephole 36 replaced sub a,#0xFF by inc a.
777 ; peephole 37 directly used (%1 + %2) in calculation instead of placing it in bc first.
795 ; peephole 38 directly used (%1 + %2) in calculation instead of placing it in de first.
807 ; peephole 39 replaced zero load, rla by and since rlca writes the same value to carry bit and least significant bit.
817 ; peephole 40 moved %2 directly into de instead of going through %1.
827 ; peephole 41 removed lower part of multibyte addition.
838 ; peephole 42 removed loads by exploiting commutativity of addition.
840 } if notVolatile(%1), notUsed(%1), operandsNotRelated(%2 '(bc)' '(de)')
848 ; peephole 42a removed loads by exploiting commutativity of addition.
851 // Don't need to check for volatile, since ix is used to access the stack.
853 // sdcc does not use the H flag. sla resets it, while add sets it.
854 // To ensure that the state of the H flag is not changed by this
855 // peephole uncomment the add %3, %4 at the end (since it overwrite the H flag).
863 ; peephole 42b shifted in accumulator insted of %1
869 // sdcc does not use the H flag. sla resets it, while add sets it.
870 // To ensure that the state of the H flag is not changed by this
871 // peephole uncomment the add %3, %4 at the end (since it overwrite the H flag).
881 ; peephole 42b' shifted in accumulator insted of %1
899 ; peephole 42c incremented in (hl) instead of going through a.
914 ; peephole 42d decremented in (hl) instead of going through a.
923 ; peephole 43 removed load by exploiting commutativity of addition.
925 } if operandsNotRelated(%2 '(bc)' '(de)')
933 ; peephole 43a removed loads by exploiting commutativity of addition.
944 ; peephole 43b removed loads by exploiting commutativity of addition.
955 ; peephole 43c removed loads by exploiting commutativity of addition.
968 ; peephole 44 removed redundant zeroing of a (which has just been tested to be #0x00).
980 ; peephole 45 replaced constant #0x00 by a (which has just been tested to be #0x00).
991 ; peephole 46 replaced constant #0x00 by a (which has just been tested to be #0x00).
1002 ; peephole 47 replaced constant #0x00 by a (which has just been tested to be #0x00).
1013 ; peephole 48 replaced constant #0x00 by a (which has just been tested to be #0x00).
1025 // ; peephole 49 shortened or using a (which has just been tested to be #0x00).
1026 //} if operandsNotSame3(%3 '(bc)' '(de)')
1037 // ; peephole 50 shortened or using a (which has just been tested to be #0x00).
1038 //} if operandsNotSame3(%3 '(bc)' '(de)')
1040 // Commented out sine it triggers for %3 od the form (#...)
1050 // ; peephole 51 shortened or using a (which has just been tested to be #0x00).
1052 //if operandsNotSame3(%3 '(bc)' '(de)')
1063 ; peephole 52 shortened or using a (which has just been tested to be #0x00).
1065 //if operandsNotSame3(%2 '(bc)' '(de)')
1077 ; peephole 53 replaced constant #0x00 by a (which has just been tested to be #0x00).
1090 ; peephole 54 replaced constant #0x00 by a (which has just been tested to be #0x00).
1103 ; peephole 55 replaced constant #0x00 by a (which has just been tested to be #0x00).
1116 ; peephole 56 replaced constant #0x00 by a (which has just been tested to be #0x00).
1124 ; peephole 59 moved increment of de to constant.
1132 ; peephole 62 moved increment of bc to constant.
1145 ; peephole 63 moved addition of constant 0x%3%2 to bc to constant.
1153 ; peephole 65 replaced call at end of function by jump.
1164 ; peephole 66 replaced call at end of function by jump moving call beyond pop ix.
1172 ; peephole 67 loaded %2, %3 from a instead of %1.
1176 } if notVolatile(%1)
1185 ; peephole 68 loaded %3 from a instead of %1.
1187 } if notVolatile(%1)
1196 ; peephole 69 used l in or instead of %1.
1198 } if notVolatile(%1)
1207 ; peephole 70 removed load of #%2 into %1 since it's still there.
1208 } if notVolatile(%1), operandsNotRelated(%3 %1)
1214 ; peephole 70a used #%1 from hl for load into de.
1228 ; peephole 71 used ex to get de into hl.
1235 // Lets' hope this is never applied to a jump table. It's fine for calls.
1237 // Should be after 0z.
1246 ; peephole 72 used hl instead of %2 (ix), %1 (ix) to load %4%3.
1249 } if operandsNotRelated('h' %3)
1250 // Don't check for volatile since ix points to the stack.
1261 ; peephole 76 loaded l from a directly instead of going through %1.
1273 ; peephole 77 loaded l from a directly instead of going through %1.
1285 ; peephole 78 removed addition and loads exploiting commutativity of addition.
1293 ; peephole 78a removed ex exploiting commutativity of addition.
1305 ; peephole 79 moved increment to constant.
1310 // These ex-generating rules should be among the last ones since ex counts as a read from both hl and de for notUsed().
1315 ; peephole 80 used ex to load hl into de.
1323 ; peephole 81 used ex to load hl into de.
1331 ; peephole 82 used ex to load de into hl.
1335 // peeph-z80.def - Z80 peephole rules
1338 // (c) Philipp Klaus Krause (pkk@spth.de, philipp@colecovision.eu) 2006 - 2007
1340 // This program is free software; you can redistribute it and/or modify it
1341 // under the terms of the GNU General Public License as published by the
1342 // Free Software Foundation; either version 2, or (at your option) any
1345 // This program is distributed in the hope that it will be useful,
1346 // but WITHOUT ANY WARRANTY; without even the implied warranty of
1347 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1348 // GNU General Public License for more details.
1350 // You should have received a copy of the GNU General Public License
1351 // along with this program; if not, write to the Free Software
1352 // Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1361 ; peephole z1 used #%2 directly instead of going through %1 using indirect addressing.
1373 ; peephole z2 directly used #%2 instead of going through %1 using indirect addressing.
1386 ; peephole z3 used #%2 directly instead of going through %1 using indirect addressing.
1389 } if operandsNotRelated(%3 'a')
1398 ; peephole z4 directly used address #%1 + %2 instead of placing it in bc first.
1407 ld (#%1 + %2 + %6),a
1408 ; peephole z5 directly used address #%1 + %2 + %6 instead of placing it in bc first.
1417 ; peephole z8 moved %1 directly into l instead of going through c.
1429 ; peephole z9 moved hl directly to the stack instead of going through bc.
1441 ; peephole z10 moved hl directly to the stack instead of going through de.
1449 ; peephole z11 replaced jump by return.
1450 } if labelIsReturnOnly(), labelRefCountChange(%5 -1)
1456 ; peephole z11a replaced jump by return.
1457 } if labelIsReturnOnly(), labelRefCountChange(%5 -1)
1459 // Should be one of the last ones. Opens the code to further peephole optimization.
1463 ; peephole z12 removed unused label %1.
1464 } if labelRefCount(%1 0)
1466 // Applying z11 or z11a followed by z12 will often leave a dead ret at the end of the function. Remove it.
1472 ; peephole z13 removed unused ret.
1475 // These four need two be here since the peephole optimizer continues to apply rules further down the file even for replace restart rules.
1482 ; peephole 5' removed jp by using inverse jump logic
1484 } if labelRefCountChange(%1 -1)
1492 ; peephole 5' removed jp by using inverse jump logic
1494 } if labelRefCountChange(%1 -1)
1502 ; peephole 5' removed jp by using inverse jump logic
1504 } if labelRefCountChange(%1 -1)
1512 ; peephole 6' removed jp by using inverse jump logic
1514 } if labelRefCountChange(%1 -1)
1516 // These should be the last rules, so that the peepholes above need to look at jp only.
1521 ; peephole z14 changed absolute to relative unconditional jump.
1528 ; peephole z15 changed absolute to relative conditional jump.
1535 ; peephole z16 changed absolute to relative conditional jump.
1542 ; peephole z17 changed absolute to relative conditional jump.
1549 ; peephole z18 changed absolute to relative conditional jump.