1 /*****************************************************************************/
5 /* Optimizer subroutines */
9 /* (C) 2001-2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
58 #include "coptstore.h"
68 /*****************************************************************************/
70 /*****************************************************************************/
85 /*****************************************************************************/
87 /*****************************************************************************/
91 static unsigned OptShift1 (CodeSeg* S)
92 /* A call to the shlaxN routine may get replaced by one or more asl insns
93 * if the value of X is not used later.
98 /* Walk over the entries */
100 while (I < CS_GetEntryCount (S)) {
103 CodeEntry* E = CS_GetEntry (S, I);
105 /* Check for the sequence */
106 if (E->OPC == OP65_JSR &&
107 (strncmp (E->Arg, "shlax", 5) == 0 ||
108 strncmp (E->Arg, "aslax", 5) == 0) &&
109 strlen (E->Arg) == 6 &&
110 IsDigit (E->Arg[5]) &&
111 !RegXUsed (S, I+1)) {
113 /* Insert shift insns */
114 unsigned Count = E->Arg[5] - '0';
116 CodeEntry* X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
117 CS_InsertEntry (S, X, I+1);
120 /* Delete the call to shlax */
123 /* Remember, we had changes */
133 /* Return the number of changes made */
139 static unsigned OptShift2 (CodeSeg* S)
140 /* A call to the shraxN routine may get replaced by one or more lsr insns
141 * if the value of X is not used later, or if the value of X is known and
145 unsigned Changes = 0;
148 /* Generate register info */
151 /* Walk over the entries */
153 while (I < CS_GetEntryCount (S)) {
156 CodeEntry* E = CS_GetEntry (S, I);
158 /* Check for the sequence */
159 if (E->OPC == OP65_JSR &&
160 strncmp (E->Arg, "shrax", 5) == 0 &&
161 strlen (E->Arg) == 6 &&
162 IsDigit (E->Arg[5]) &&
163 (E->RI->In.RegX == 0 || !RegXUsed (S, I+1))) {
165 /* Insert shift insns */
166 unsigned Count = E->Arg[5] - '0';
168 CodeEntry* X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, E->LI);
169 CS_InsertEntry (S, X, I+1);
172 /* Delete the call to shlax */
175 /* Remember, we had changes */
185 /* Free the register info */
188 /* Return the number of changes made */
194 static unsigned GetShiftType (const char* Sub)
195 /* Helper function for OptShift3 */
198 if (strcmp (Sub+1, "slax1") == 0) {
200 } else if (strcmp (Sub+1, "srax1") == 0) {
203 } else if (*Sub == 's') {
204 if (strcmp (Sub+1, "hlax1") == 0) {
206 } else if (strcmp (Sub+1, "hrax1") == 0) {
215 static unsigned OptShift3 (CodeSeg* S)
216 /* Search for the sequence
220 * jsr aslax1/asrax1/shlax1/shrax1
233 * or similar, provided that a/x is not used later
236 unsigned Changes = 0;
238 /* Walk over the entries */
240 while (I < CS_GetEntryCount (S)) {
246 L[0] = CS_GetEntry (S, I);
248 /* Check for the sequence */
249 if (L[0]->OPC == OP65_LDA &&
250 (L[0]->AM == AM65_ABS || L[0]->AM == AM65_ZP) &&
251 CS_GetEntries (S, L+1, I+1, 4) &&
252 !CS_RangeHasLabel (S, I+1, 4) &&
253 L[1]->OPC == OP65_LDX &&
254 (L[1]->AM == AM65_ABS || L[1]->AM == AM65_ZP) &&
255 L[2]->OPC == OP65_JSR &&
256 (ShiftType = GetShiftType (L[2]->Arg)) != SHIFT_NONE&&
257 L[3]->OPC == OP65_STA &&
258 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
259 L[4]->OPC == OP65_STX &&
260 (L[4]->AM == AM65_ABS || L[4]->AM == AM65_ZP) &&
261 !RegAXUsed (S, I+5)) {
265 /* Handle the four shift types differently */
269 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
270 CS_InsertEntry (S, X, I+5);
271 X = NewCodeEntry (OP65_CMP, AM65_IMM, "$80", 0, L[2]->LI);
272 CS_InsertEntry (S, X, I+6);
273 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
274 CS_InsertEntry (S, X, I+7);
275 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
276 CS_InsertEntry (S, X, I+8);
277 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
278 CS_InsertEntry (S, X, I+9);
279 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
280 CS_InsertEntry (S, X, I+10);
281 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
282 CS_InsertEntry (S, X, I+11);
283 CS_DelEntries (S, I, 5);
287 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
288 CS_InsertEntry (S, X, I+5);
289 X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, L[2]->LI);
290 CS_InsertEntry (S, X, I+6);
291 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
292 CS_InsertEntry (S, X, I+7);
293 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
294 CS_InsertEntry (S, X, I+8);
295 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
296 CS_InsertEntry (S, X, I+9);
297 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
298 CS_InsertEntry (S, X, I+10);
299 CS_DelEntries (S, I, 5);
304 /* These two are identical */
305 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, L[2]->LI);
306 CS_InsertEntry (S, X, I+1);
307 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
308 CS_InsertEntry (S, X, I+2);
309 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
310 CS_InsertEntry (S, X, I+3);
311 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, L[2]->LI);
312 CS_InsertEntry (S, X, I+4);
313 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
314 CS_InsertEntry (S, X, I+5);
315 CS_DelEntries (S, I+6, 4);
320 /* Remember, we had changes */
330 /* Return the number of changes made */
336 /*****************************************************************************/
338 /*****************************************************************************/
342 static unsigned OptLoad1 (CodeSeg* S)
343 /* Search for a call to ldaxysp where X is not used later and replace it by
344 * a load of just the A register.
348 unsigned Changes = 0;
350 /* Generate register info */
353 /* Walk over the entries */
355 while (I < CS_GetEntryCount (S)) {
360 E = CS_GetEntry (S, I);
362 /* Check for the sequence */
363 if (CE_IsCallTo (E, "ldaxysp") &&
364 RegValIsKnown (E->RI->In.RegY) &&
365 !RegXUsed (S, I+1)) {
369 /* Reload the Y register */
370 const char* Arg = MakeHexArg (E->RI->In.RegY - 1);
371 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
372 CS_InsertEntry (S, X, I+1);
374 /* Load from stack */
375 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, E->LI);
376 CS_InsertEntry (S, X, I+2);
378 /* Now remove the call to the subroutine */
381 /* Remember, we had changes */
391 /* Free the register info */
394 /* Return the number of changes made */
400 /*****************************************************************************/
401 /* Optimize stores through pointers */
402 /*****************************************************************************/
406 static unsigned OptPtrStore1Sub (CodeSeg* S, unsigned I, CodeEntry** const L)
407 /* Check if this is one of the allowed suboperation for OptPtrStore1 */
409 /* Check for a label attached to the entry */
410 if (CE_HasLabel (L[0])) {
414 /* Check for single insn sub ops */
415 if (L[0]->OPC == OP65_AND ||
416 L[0]->OPC == OP65_EOR ||
417 L[0]->OPC == OP65_ORA ||
418 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shlax", 5) == 0) ||
419 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shrax", 5) == 0)) {
424 } else if (L[0]->OPC == OP65_CLC &&
425 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
426 L[1]->OPC == OP65_ADC &&
427 !CE_HasLabel (L[1])) {
429 } else if (L[0]->OPC == OP65_SEC &&
430 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
431 L[1]->OPC == OP65_SBC &&
432 !CE_HasLabel (L[1])) {
444 static unsigned OptPtrStore1 (CodeSeg* S)
445 /* Search for the sequence:
466 unsigned Changes = 0;
468 /* Walk over the entries */
470 while (I < CS_GetEntryCount (S)) {
476 L[0] = CS_GetEntry (S, I);
478 /* Check for the sequence */
479 if (CE_IsCallTo (L[0], "pushax") &&
480 CS_GetEntries (S, L+1, I+1, 3) &&
481 L[1]->OPC == OP65_LDY &&
482 CE_KnownImm (L[1]) &&
483 !CE_HasLabel (L[1]) &&
484 CE_IsCallTo (L[2], "ldauidx") &&
485 !CE_HasLabel (L[2]) &&
486 (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
487 CS_GetEntries (S, L+3+K, I+3+K, 2) &&
488 L[3+K]->OPC == OP65_LDY &&
489 CE_KnownImm (L[3+K]) &&
490 !CE_HasLabel (L[3+K]) &&
491 CE_IsCallTo (L[4+K], "staspidx") &&
492 !CE_HasLabel (L[4+K])) {
496 /* Create and insert the stores */
497 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
498 CS_InsertEntry (S, X, I+1);
500 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
501 CS_InsertEntry (S, X, I+2);
503 /* Delete the call to pushax */
506 /* Delete the call to ldauidx */
507 CS_DelEntry (S, I+3);
509 /* Insert the load from ptr1 */
510 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
511 CS_InsertEntry (S, X, I+3);
512 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[2]->LI);
513 CS_InsertEntry (S, X, I+4);
515 /* Insert the store through ptr1 */
516 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
517 CS_InsertEntry (S, X, I+6+K);
519 /* Delete the call to staspidx */
520 CS_DelEntry (S, I+7+K);
522 /* Remember, we had changes */
532 /* Return the number of changes made */
538 static unsigned OptPtrStore2 (CodeSeg* S)
539 /* Search for the sequence:
555 unsigned Changes = 0;
557 /* Walk over the entries */
559 while (I < CS_GetEntryCount (S)) {
564 L[0] = CS_GetEntry (S, I);
566 /* Check for the sequence */
567 if (CE_IsCallTo (L[0], "pushax") &&
568 CS_GetEntries (S, L+1, I+1, 3) &&
569 L[1]->OPC == OP65_LDA &&
570 !CE_HasLabel (L[1]) &&
571 L[2]->OPC == OP65_LDY &&
572 !CE_HasLabel (L[2]) &&
573 CE_IsCallTo (L[3], "staspidx") &&
574 !CE_HasLabel (L[3])) {
578 /* Create and insert the stores */
579 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
580 CS_InsertEntry (S, X, I+1);
582 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
583 CS_InsertEntry (S, X, I+2);
585 /* Delete the call to pushax */
588 /* Insert the store through ptr1 */
589 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
590 CS_InsertEntry (S, X, I+4);
592 /* Delete the call to staspidx */
593 CS_DelEntry (S, I+5);
595 /* Remember, we had changes */
605 /* Return the number of changes made */
611 /*****************************************************************************/
612 /* Optimize loads through pointers */
613 /*****************************************************************************/
617 static unsigned OptPtrLoad1 (CodeSeg* S)
618 /* Search for the sequence:
643 unsigned Changes = 0;
645 /* Walk over the entries */
647 while (I < CS_GetEntryCount (S)) {
652 L[0] = CS_GetEntry (S, I);
654 /* Check for the sequence */
655 if (L[0]->OPC == OP65_CLC &&
656 CS_GetEntries (S, L+1, I+1, 8) &&
657 L[1]->OPC == OP65_ADC &&
658 !CE_HasLabel (L[1]) &&
659 L[2]->OPC == OP65_TAY &&
660 !CE_HasLabel (L[2]) &&
661 L[3]->OPC == OP65_TXA &&
662 !CE_HasLabel (L[3]) &&
663 L[4]->OPC == OP65_ADC &&
664 !CE_HasLabel (L[4]) &&
665 L[5]->OPC == OP65_TAX &&
666 !CE_HasLabel (L[5]) &&
667 L[6]->OPC == OP65_TYA &&
668 !CE_HasLabel (L[6]) &&
669 L[7]->OPC == OP65_LDY &&
670 !CE_HasLabel (L[7]) &&
671 CE_IsCallTo (L[8], "ldauidx") &&
672 !CE_HasLabel (L[8])) {
677 /* Store the low byte and remove the TAY instead */
678 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[1]->LI);
679 CS_InsertEntry (S, X, I+2);
680 CS_DelEntry (S, I+3);
682 /* Store the high byte */
683 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
684 CS_InsertEntry (S, X, I+5);
686 /* If the instruction before the adc is a ldx, replace the
687 * txa by and lda with the same location of the ldx.
689 if ((P = CS_GetPrevEntry (S, I)) != 0 &&
690 P->OPC == OP65_LDX &&
693 X = NewCodeEntry (OP65_LDA, P->AM, P->Arg, 0, P->LI);
694 CS_InsertEntry (S, X, I+4);
695 CS_DelEntry (S, I+3);
698 /* Delete more transfer insns */
699 CS_DelEntry (S, I+7);
700 CS_DelEntry (S, I+6);
702 /* Delete the call to ldauidx */
703 CS_DelEntry (S, I+7);
705 /* Load high and low byte */
706 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[7]->LI);
707 CS_InsertEntry (S, X, I+7);
708 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[7]->LI);
709 CS_InsertEntry (S, X, I+8);
711 /* Remember, we had changes */
721 /* Return the number of changes made */
727 static unsigned OptPtrLoad2 (CodeSeg* S)
728 /* Search for the sequence:
753 unsigned Changes = 0;
755 /* Walk over the entries */
757 while (I < CS_GetEntryCount (S)) {
762 L[0] = CS_GetEntry (S, I);
764 /* Check for the sequence */
765 if (L[0]->OPC == OP65_ADC &&
766 CS_GetEntries (S, L+1, I+1, 8) &&
767 L[1]->OPC == OP65_PHA &&
768 !CE_HasLabel (L[1]) &&
769 L[2]->OPC == OP65_TXA &&
770 !CE_HasLabel (L[2]) &&
771 L[3]->OPC == OP65_INY &&
772 !CE_HasLabel (L[3]) &&
773 L[4]->OPC == OP65_ADC &&
774 !CE_HasLabel (L[4]) &&
775 L[5]->OPC == OP65_TAX &&
776 !CE_HasLabel (L[5]) &&
777 L[6]->OPC == OP65_PLA &&
778 !CE_HasLabel (L[6]) &&
779 L[7]->OPC == OP65_LDY &&
780 !CE_HasLabel (L[7]) &&
781 CE_IsCallTo (L[8], "ldauidx") &&
782 !CE_HasLabel (L[8])) {
786 /* Store the low byte and remove the PHA instead */
787 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
788 CS_InsertEntry (S, X, I+1);
789 CS_DelEntry (S, I+2);
791 /* Store the high byte */
792 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
793 CS_InsertEntry (S, X, I+5);
795 /* Delete more transfer and PLA insns */
796 CS_DelEntry (S, I+7);
797 CS_DelEntry (S, I+6);
799 /* Delete the call to ldauidx */
800 CS_DelEntry (S, I+7);
802 /* Load high and low byte */
803 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[6]->LI);
804 CS_InsertEntry (S, X, I+7);
805 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI);
806 CS_InsertEntry (S, X, I+8);
808 /* Remember, we had changes */
818 /* Return the number of changes made */
824 static unsigned OptPtrLoad3 (CodeSeg* S)
825 /* Search for the sequence:
843 unsigned Changes = 0;
845 /* Walk over the entries */
847 while (I < CS_GetEntryCount (S)) {
853 L[0] = CS_GetEntry (S, I);
855 /* Check for the sequence */
856 if (L[0]->OPC == OP65_LDA &&
857 L[0]->AM == AM65_IMM &&
858 CS_GetEntries (S, L+1, I+1, 7) &&
859 L[1]->OPC == OP65_LDX &&
860 L[1]->AM == AM65_IMM &&
861 !CE_HasLabel (L[1]) &&
862 L[2]->OPC == OP65_CLC &&
863 !CE_HasLabel (L[2]) &&
864 L[3]->OPC == OP65_ADC &&
865 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
866 !CE_HasLabel (L[3]) &&
867 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
869 L[4]->JumpTo->Owner == L[6] &&
870 !CE_HasLabel (L[4]) &&
871 L[5]->OPC == OP65_INX &&
872 !CE_HasLabel (L[5]) &&
873 L[6]->OPC == OP65_LDY &&
874 CE_KnownImm (L[6]) &&
876 CE_IsCallTo (L[7], "ldauidx") &&
877 !CE_HasLabel (L[7]) &&
878 /* Check the label last because this is quite costly */
879 (Len = strlen (L[0]->Arg)) > 3 &&
880 L[0]->Arg[0] == '<' &&
881 L[0]->Arg[1] == '(' &&
882 strlen (L[1]->Arg) == Len &&
883 L[1]->Arg[0] == '>' &&
884 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
889 /* We will create all the new stuff behind the current one so
890 * we keep the line references.
892 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
893 CS_InsertEntry (S, X, I+8);
895 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
896 CS_InsertEntry (S, X, I+9);
898 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
900 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
901 CS_InsertEntry (S, X, I+10);
904 /* Remove the old code */
905 CS_DelEntries (S, I, 8);
907 /* Remember, we had changes */
917 /* Return the number of changes made */
923 static unsigned OptPtrLoad4 (CodeSeg* S)
924 /* Search for the sequence:
945 unsigned Changes = 0;
947 /* Walk over the entries */
949 while (I < CS_GetEntryCount (S)) {
955 L[0] = CS_GetEntry (S, I);
957 /* Check for the sequence */
958 if (L[0]->OPC == OP65_LDA &&
959 L[0]->AM == AM65_IMM &&
960 CS_GetEntries (S, L+1, I+1, 8) &&
961 L[1]->OPC == OP65_LDX &&
962 L[1]->AM == AM65_IMM &&
963 !CE_HasLabel (L[1]) &&
964 L[2]->OPC == OP65_LDY &&
965 CE_KnownImm (L[2]) &&
966 !CE_HasLabel (L[2]) &&
967 L[3]->OPC == OP65_CLC &&
968 !CE_HasLabel (L[3]) &&
969 L[4]->OPC == OP65_ADC &&
970 L[4]->AM == AM65_ZP_INDY &&
971 !CE_HasLabel (L[4]) &&
972 (L[5]->OPC == OP65_BCC || L[5]->OPC == OP65_JCC) &&
974 L[5]->JumpTo->Owner == L[7] &&
975 !CE_HasLabel (L[5]) &&
976 L[6]->OPC == OP65_INX &&
977 !CE_HasLabel (L[6]) &&
978 L[7]->OPC == OP65_LDY &&
979 CE_KnownImm (L[7]) &&
981 CE_IsCallTo (L[8], "ldauidx") &&
982 !CE_HasLabel (L[8]) &&
983 /* Check the label last because this is quite costly */
984 (Len = strlen (L[0]->Arg)) > 3 &&
985 L[0]->Arg[0] == '<' &&
986 L[0]->Arg[1] == '(' &&
987 strlen (L[1]->Arg) == Len &&
988 L[1]->Arg[0] == '>' &&
989 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
995 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[4]->Arg, 0, L[0]->LI);
996 CS_InsertEntry (S, X, I+3);
999 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[0]->LI);
1000 CS_InsertEntry (S, X, I+4);
1003 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1004 CS_InsertEntry (S, X, I+5);
1007 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
1008 Label[Len-3] = '\0';
1009 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
1010 CS_InsertEntry (S, X, I+6);
1013 /* Remove the old code */
1014 CS_DelEntries (S, I, 2);
1015 CS_DelEntries (S, I+5, 6);
1017 /* Remember, we had changes */
1027 /* Return the number of changes made */
1033 static unsigned OptPtrLoad5 (CodeSeg* S)
1034 /* Search for the sequence:
1041 * and replace it by:
1048 unsigned Changes = 0;
1050 /* Walk over the entries */
1052 while (I < CS_GetEntryCount (S)) {
1057 /* Get next entry */
1058 L[0] = CS_GetEntry (S, I);
1060 /* Check for the sequence */
1061 if (L[0]->OPC == OP65_LDA && L[0]->AM == AM65_ZP &&
1062 CS_GetEntries (S, L+1, I+1, 3) &&
1063 !CS_RangeHasLabel (S, I+1, 3) &&
1064 L[1]->OPC == OP65_LDX && L[1]->AM == AM65_ZP &&
1065 (Len = strlen (L[0]->Arg)) > 0 &&
1066 strncmp (L[0]->Arg, L[1]->Arg, Len) == 0 &&
1067 strcmp (L[1]->Arg + Len, "+1") == 0 &&
1068 L[2]->OPC == OP65_LDY &&
1069 CE_IsCallTo (L[3], "ldauidx")) {
1074 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
1075 CS_InsertEntry (S, X, I+3);
1078 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1079 CS_InsertEntry (S, X, I+4);
1081 /* Remove the old code */
1082 CS_DelEntry (S, I+5);
1083 CS_DelEntries (S, I, 2);
1085 /* Remember, we had changes */
1095 /* Return the number of changes made */
1101 static unsigned OptPtrLoad6 (CodeSeg* S)
1102 /* Search for the sequence:
1109 * and replace it by:
1118 unsigned Changes = 0;
1120 /* Walk over the entries */
1122 while (I < CS_GetEntryCount (S)) {
1127 /* Get next entry */
1128 L[0] = CS_GetEntry (S, I);
1130 /* Check for the sequence */
1131 if (L[0]->OPC == OP65_LDA && L[0]->AM == AM65_ZP &&
1132 CS_GetEntries (S, L+1, I+1, 3) &&
1133 !CS_RangeHasLabel (S, I+1, 3) &&
1134 L[1]->OPC == OP65_LDX && L[1]->AM == AM65_ZP &&
1135 (Len = strlen (L[0]->Arg)) > 0 &&
1136 strncmp (L[0]->Arg, L[1]->Arg, Len) == 0 &&
1137 strcmp (L[1]->Arg + Len, "+1") == 0 &&
1138 L[2]->OPC == OP65_LDY &&
1139 CE_IsCallTo (L[3], "ldaxidx")) {
1144 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1145 CS_InsertEntry (S, X, I+4);
1148 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[3]->LI);
1149 CS_InsertEntry (S, X, I+5);
1152 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[3]->LI);
1153 CS_InsertEntry (S, X, I+6);
1156 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1157 CS_InsertEntry (S, X, I+7);
1159 /* Remove the old code */
1160 CS_DelEntry (S, I+3);
1161 CS_DelEntries (S, I, 2);
1163 /* Remember, we had changes */
1173 /* Return the number of changes made */
1179 static unsigned OptPtrLoad7 (CodeSeg* S)
1180 /* Search for the sequence
1185 * and replace it by:
1193 * This step must be execute *after* OptPtrLoad1!
1196 unsigned Changes = 0;
1198 /* Walk over the entries */
1200 while (I < CS_GetEntryCount (S)) {
1204 /* Get next entry */
1205 L[0] = CS_GetEntry (S, I);
1207 /* Check for the sequence */
1208 if (L[0]->OPC == OP65_LDY &&
1209 CS_GetEntries (S, L+1, I+1, 1) &&
1210 CE_IsCallTo (L[1], "ldauidx") &&
1211 !CE_HasLabel (L[1])) {
1215 /* Store the high byte */
1216 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
1217 CS_InsertEntry (S, X, I+1);
1219 /* Store the low byte */
1220 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
1221 CS_InsertEntry (S, X, I+2);
1223 /* Delete the call to ldauidx */
1224 CS_DelEntry (S, I+3);
1226 /* Load the high and low byte */
1227 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1228 CS_InsertEntry (S, X, I+3);
1229 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[0]->LI);
1230 CS_InsertEntry (S, X, I+4);
1232 /* Remember, we had changes */
1242 /* Return the number of changes made */
1248 /*****************************************************************************/
1249 /* Decouple operations */
1250 /*****************************************************************************/
1254 static unsigned OptDecouple (CodeSeg* S)
1255 /* Decouple operations, that is, do the following replacements:
1265 * lda zp -> lda #imm
1266 * ldx zp -> ldx #imm
1267 * ldy zp -> ldy #imm
1269 * Provided that the register values are known of course.
1272 unsigned Changes = 0;
1275 /* Generate register info for the following step */
1278 /* Walk over the entries */
1280 while (I < CS_GetEntryCount (S)) {
1284 /* Get next entry and it's input register values */
1285 CodeEntry* E = CS_GetEntry (S, I);
1286 const RegContents* In = &E->RI->In;
1288 /* Assume we have no replacement */
1291 /* Check the instruction */
1295 if (E->RI->In.RegX >= 0) {
1296 Arg = MakeHexArg ((E->RI->In.RegX - 1) & 0xFF);
1297 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1302 if (E->RI->In.RegY >= 0) {
1303 Arg = MakeHexArg ((E->RI->In.RegY - 1) & 0xFF);
1304 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1309 if (E->RI->In.RegX >= 0) {
1310 Arg = MakeHexArg ((E->RI->In.RegX + 1) & 0xFF);
1311 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1316 if (E->RI->In.RegY >= 0) {
1317 Arg = MakeHexArg ((E->RI->In.RegY + 1) & 0xFF);
1318 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1323 if (E->AM == AM65_ZP) {
1324 switch (GetKnownReg (E->Use, In)) {
1326 Arg = MakeHexArg (In->Tmp1);
1327 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1331 Arg = MakeHexArg (In->SRegLo);
1332 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1336 Arg = MakeHexArg (In->SRegHi);
1337 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1344 if (E->AM == AM65_ZP) {
1345 switch (GetKnownReg (E->Use, In)) {
1347 Arg = MakeHexArg (In->Tmp1);
1348 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1352 Arg = MakeHexArg (In->SRegLo);
1353 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1357 Arg = MakeHexArg (In->SRegHi);
1358 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1365 if (E->AM == AM65_ZP) {
1366 switch (GetKnownReg (E->Use, In)) {
1368 Arg = MakeHexArg (In->Tmp1);
1369 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1373 Arg = MakeHexArg (In->SRegLo);
1374 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1378 Arg = MakeHexArg (In->SRegHi);
1379 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1386 if (E->RI->In.RegA >= 0) {
1387 Arg = MakeHexArg (In->RegA);
1388 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1393 if (E->RI->In.RegA >= 0) {
1394 Arg = MakeHexArg (In->RegA);
1395 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1400 if (E->RI->In.RegX >= 0) {
1401 Arg = MakeHexArg (In->RegX);
1402 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1407 if (E->RI->In.RegY >= 0) {
1408 Arg = MakeHexArg (In->RegY);
1409 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1414 /* Avoid gcc warnings */
1419 /* Insert the replacement if we have one */
1421 CS_InsertEntry (S, X, I+1);
1431 /* Free register info */
1434 /* Return the number of changes made */
1440 /*****************************************************************************/
1441 /* struct OptFunc */
1442 /*****************************************************************************/
1446 typedef struct OptFunc OptFunc;
1448 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1449 const char* Name; /* Name of the function/group */
1450 unsigned CodeSizeFactor; /* Code size factor for this opt func */
1451 unsigned long TotalRuns; /* Total number of runs */
1452 unsigned long LastRuns; /* Last number of runs */
1453 unsigned long TotalChanges; /* Total number of changes */
1454 unsigned long LastChanges; /* Last number of changes */
1455 char Disabled; /* True if function disabled */
1460 /*****************************************************************************/
1462 /*****************************************************************************/
1466 /* A list of all the function descriptions */
1467 static OptFunc DOpt65C02BitOps = { Opt65C02BitOps, "Opt65C02BitOps", 66, 0, 0, 0, 0, 0 };
1468 static OptFunc DOpt65C02Ind = { Opt65C02Ind, "Opt65C02Ind", 100, 0, 0, 0, 0, 0 };
1469 static OptFunc DOpt65C02Stores = { Opt65C02Stores, "Opt65C02Stores", 100, 0, 0, 0, 0, 0 };
1470 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 125, 0, 0, 0, 0, 0 };
1471 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 200, 0, 0, 0, 0, 0 };
1472 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 90, 0, 0, 0, 0, 0 };
1473 static OptFunc DOptAdd4 = { OptAdd4, "OptAdd4", 100, 0, 0, 0, 0, 0 };
1474 static OptFunc DOptAdd5 = { OptAdd5, "OptAdd5", 40, 0, 0, 0, 0, 0 };
1475 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 100, 0, 0, 0, 0, 0 };
1476 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0, 0 };
1477 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 85, 0, 0, 0, 0, 0 };
1478 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 75, 0, 0, 0, 0, 0 };
1479 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 75, 0, 0, 0, 0, 0 };
1480 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 100, 0, 0, 0, 0, 0 };
1481 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 100, 0, 0, 0, 0, 0 };
1482 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 85, 0, 0, 0, 0, 0 };
1483 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 50, 0, 0, 0, 0, 0 };
1484 static OptFunc DOptCondBranches = { OptCondBranches, "OptCondBranches", 80, 0, 0, 0, 0, 0 };
1485 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 100, 0, 0, 0, 0, 0 };
1486 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 100, 0, 0, 0, 0, 0 };
1487 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 100, 0, 0, 0, 0, 0 };
1488 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0, 0 };
1489 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 100, 0, 0, 0, 0, 0 };
1490 static OptFunc DOptJumpTarget = { OptJumpTarget, "OptJumpTarget", 100, 0, 0, 0, 0, 0 };
1491 static OptFunc DOptLoad1 = { OptLoad1, "OptLoad1", 100, 0, 0, 0, 0, 0 };
1492 static OptFunc DOptRTS = { OptRTS, "OptRTS", 100, 0, 0, 0, 0, 0 };
1493 static OptFunc DOptRTSJumps1 = { OptRTSJumps1, "OptRTSJumps1", 100, 0, 0, 0, 0, 0 };
1494 static OptFunc DOptRTSJumps2 = { OptRTSJumps2, "OptRTSJumps2", 100, 0, 0, 0, 0, 0 };
1495 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 100, 0, 0, 0, 0, 0 };
1496 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 100, 0, 0, 0, 0, 0 };
1497 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 100, 0, 0, 0, 0, 0 };
1498 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 100, 0, 0, 0, 0, 0 };
1499 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 100, 0, 0, 0, 0, 0 };
1500 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 100, 0, 0, 0, 0, 0 };
1501 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 100, 0, 0, 0, 0, 0 };
1502 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 };
1503 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 };
1504 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 };
1505 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 65, 0, 0, 0, 0, 0 };
1506 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 86, 0, 0, 0, 0, 0 };
1507 static OptFunc DOptPtrLoad7 = { OptPtrLoad7, "OptPtrLoad7", 100, 0, 0, 0, 0, 0 };
1508 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 100, 0, 0, 0, 0, 0 };
1509 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 100, 0, 0, 0, 0, 0 };
1510 static OptFunc DOptPush1 = { OptPush1, "OptPush1", 65, 0, 0, 0, 0, 0 };
1511 static OptFunc DOptPush2 = { OptPush2, "OptPush2", 50, 0, 0, 0, 0, 0 };
1512 static OptFunc DOptPushPop = { OptPushPop, "OptPushPop", 0, 0, 0, 0, 0, 0 };
1513 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 100, 0, 0, 0, 0, 0 };
1514 static OptFunc DOptShift2 = { OptShift2, "OptShift2", 100, 0, 0, 0, 0, 0 };
1515 static OptFunc DOptShift3 = { OptShift3, "OptShift3", 110, 0, 0, 0, 0, 0 };
1516 static OptFunc DOptSize1 = { OptSize1, "OptSize1", 100, 0, 0, 0, 0, 0 };
1517 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 100, 0, 0, 0, 0, 0 };
1518 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 100, 0, 0, 0, 0, 0 };
1519 static OptFunc DOptStore1 = { OptStore1, "OptStore1", 220, 0, 0, 0, 0, 0 };
1520 static OptFunc DOptStore2 = { OptStore2, "OptStore2", 120, 0, 0, 0, 0, 0 };
1521 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 };
1522 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 };
1523 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 };
1524 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 100, 0, 0, 0, 0, 0 };
1525 static OptFunc DOptTransfers = { OptTransfers, "OptTransfers", 0, 0, 0, 0, 0, 0 };
1526 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0, 0 };
1527 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0, 0 };
1530 /* Table containing all the steps in alphabetical order */
1531 static OptFunc* OptFuncs[] = {
1594 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1598 static int CmpOptStep (const void* Key, const void* Func)
1599 /* Compare function for bsearch */
1601 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1606 static OptFunc* FindOptFunc (const char* Name)
1607 /* Find an optimizer step by name in the table and return a pointer. Return
1608 * NULL if no such step is found.
1611 /* Search for the function in the list */
1612 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1618 static OptFunc* GetOptFunc (const char* Name)
1619 /* Find an optimizer step by name in the table and return a pointer. Print an
1620 * error and call AbEnd if not found.
1623 /* Search for the function in the list */
1624 OptFunc* F = FindOptFunc (Name);
1627 AbEnd ("Optimization step `%s' not found", Name);
1634 void DisableOpt (const char* Name)
1635 /* Disable the optimization with the given name */
1637 if (strcmp (Name, "any") == 0) {
1639 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1640 OptFuncs[I]->Disabled = 1;
1643 GetOptFunc(Name)->Disabled = 1;
1649 void EnableOpt (const char* Name)
1650 /* Enable the optimization with the given name */
1652 if (strcmp (Name, "any") == 0) {
1654 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1655 OptFuncs[I]->Disabled = 0;
1658 GetOptFunc(Name)->Disabled = 0;
1664 void ListOptSteps (FILE* F)
1665 /* List all optimization steps */
1668 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1669 fprintf (F, "%s\n", OptFuncs[I]->Name);
1675 static void ReadOptStats (const char* Name)
1676 /* Read the optimizer statistics file */
1681 /* Try to open the file */
1682 FILE* F = fopen (Name, "r");
1684 /* Ignore the error */
1688 /* Read and parse the lines */
1690 while (fgets (Buf, sizeof (Buf), F) != 0) {
1698 unsigned long TotalRuns;
1699 unsigned long TotalChanges;
1704 /* Remove trailing white space including the line terminator */
1707 while (Len > 0 && IsSpace (B[Len-1])) {
1712 /* Remove leading whitespace */
1713 while (IsSpace (*B)) {
1717 /* Check for empty and comment lines */
1718 if (*B == '\0' || *B == ';' || *B == '#') {
1722 /* Parse the line */
1723 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1728 /* Search for the optimizer step. */
1729 Func = FindOptFunc (Name);
1735 /* Found the step, set the fields */
1736 Func->TotalRuns = TotalRuns;
1737 Func->TotalChanges = TotalChanges;
1741 /* Close the file, ignore errors here. */
1747 static void WriteOptStats (const char* Name)
1748 /* Write the optimizer statistics file */
1752 /* Try to open the file */
1753 FILE* F = fopen (Name, "w");
1755 /* Ignore the error */
1759 /* Write a header */
1761 "; Optimizer Total Last Total Last\n"
1762 "; Step Runs Runs Chg Chg\n");
1765 /* Write the data */
1766 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1767 const OptFunc* O = OptFuncs[I];
1769 "%-20s %6lu %6lu %6lu %6lu\n",
1777 /* Close the file, ignore errors here. */
1783 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1784 /* Run one optimizer function Max times or until there are no more changes */
1786 unsigned Changes, C;
1788 /* Don't run the function if it is disabled or if it is prohibited by the
1791 if (F->Disabled || F->CodeSizeFactor > CodeSizeFactor) {
1795 /* Run this until there are no more changes */
1799 /* Run the function */
1806 F->TotalChanges += C;
1807 F->LastChanges += C;
1809 } while (--Max && C > 0);
1811 /* Return the number of changes */
1817 static unsigned RunOptGroup1 (CodeSeg* S)
1818 /* Run the first group of optimization steps. These steps translate known
1819 * patterns emitted by the code generator into more optimal patterns. Order
1820 * of the steps is important, because some of the steps done earlier cover
1821 * the same patterns as later steps as subpatterns.
1824 unsigned Changes = 0;
1826 Changes += RunOptFunc (S, &DOptPtrStore1, 1);
1827 Changes += RunOptFunc (S, &DOptPtrStore2, 1);
1828 Changes += RunOptFunc (S, &DOptPtrLoad1, 1);
1829 Changes += RunOptFunc (S, &DOptPtrLoad2, 1);
1830 Changes += RunOptFunc (S, &DOptPtrLoad3, 1);
1831 Changes += RunOptFunc (S, &DOptPtrLoad4, 1);
1832 Changes += RunOptFunc (S, &DOptPtrLoad5, 1);
1833 Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
1834 Changes += RunOptFunc (S, &DOptNegAX1, 1);
1835 Changes += RunOptFunc (S, &DOptNegAX2, 1);
1836 Changes += RunOptFunc (S, &DOptNegAX3, 1);
1837 Changes += RunOptFunc (S, &DOptNegAX4, 1);
1838 Changes += RunOptFunc (S, &DOptAdd1, 1);
1839 Changes += RunOptFunc (S, &DOptAdd2, 1);
1840 Changes += RunOptFunc (S, &DOptAdd3, 1);
1841 Changes += RunOptFunc (S, &DOptShift1, 1);
1842 Changes += RunOptFunc (S, &DOptShift2, 1);
1843 Changes += RunOptFunc (S, &DOptShift3, 1);
1844 Changes += RunOptFunc (S, &DOptStore1, 5);
1845 Changes += RunOptFunc (S, &DOptStore2, 5);
1847 /* Return the number of changes */
1853 static unsigned RunOptGroup2 (CodeSeg* S)
1854 /* Run one group of optimization steps. This step involves just decoupling
1855 * instructions by replacing them by instructions that do not depend on
1856 * previous instructions. This makes it easier to find instructions that
1860 unsigned Changes = 0;
1862 Changes += RunOptFunc (S, &DOptDecouple, 1);
1864 /* Return the number of changes */
1870 static unsigned RunOptGroup3 (CodeSeg* S)
1871 /* Run one group of optimization steps. These steps depend on each other,
1872 * that means that one step may allow another step to do additional work,
1873 * so we will repeat the steps as long as we see any changes.
1876 unsigned Changes, C;
1882 C += RunOptFunc (S, &DOptPtrLoad7, 1);
1883 C += RunOptFunc (S, &DOptNegA1, 1);
1884 C += RunOptFunc (S, &DOptNegA2, 1);
1885 C += RunOptFunc (S, &DOptSub1, 1);
1886 C += RunOptFunc (S, &DOptSub2, 1);
1887 C += RunOptFunc (S, &DOptAdd4, 1);
1888 C += RunOptFunc (S, &DOptAdd5, 1);
1889 C += RunOptFunc (S, &DOptStackOps, 1);
1890 C += RunOptFunc (S, &DOptJumpCascades, 1);
1891 C += RunOptFunc (S, &DOptDeadJumps, 1);
1892 C += RunOptFunc (S, &DOptRTS, 1);
1893 C += RunOptFunc (S, &DOptDeadCode, 1);
1894 C += RunOptFunc (S, &DOptJumpTarget, 1);
1895 C += RunOptFunc (S, &DOptCondBranches, 1);
1896 C += RunOptFunc (S, &DOptRTSJumps1, 1);
1897 C += RunOptFunc (S, &DOptBoolTrans, 1);
1898 C += RunOptFunc (S, &DOptCmp1, 1);
1899 C += RunOptFunc (S, &DOptCmp2, 1);
1900 C += RunOptFunc (S, &DOptCmp3, 1);
1901 C += RunOptFunc (S, &DOptCmp4, 1);
1902 C += RunOptFunc (S, &DOptCmp5, 1);
1903 C += RunOptFunc (S, &DOptCmp6, 1);
1904 C += RunOptFunc (S, &DOptCmp7, 1);
1905 C += RunOptFunc (S, &DOptTest1, 1);
1906 C += RunOptFunc (S, &DOptLoad1, 1);
1907 C += RunOptFunc (S, &DOptUnusedLoads, 1);
1908 C += RunOptFunc (S, &DOptUnusedStores, 1);
1909 C += RunOptFunc (S, &DOptDupLoads, 1);
1910 C += RunOptFunc (S, &DOptStoreLoad, 1);
1911 C += RunOptFunc (S, &DOptTransfers, 1);
1912 C += RunOptFunc (S, &DOptPushPop, 1);
1918 /* Return the number of changes */
1924 static unsigned RunOptGroup4 (CodeSeg* S)
1925 /* 65C02 specific optimizations. */
1927 unsigned Changes = 0;
1929 if (CPU >= CPU_65C02) {
1930 Changes += RunOptFunc (S, &DOpt65C02BitOps, 1);
1931 Changes += RunOptFunc (S, &DOpt65C02Ind, 1);
1932 Changes += RunOptFunc (S, &DOpt65C02Stores, 1);
1934 /* The 65C02 replacement codes do often make the use of a register
1935 * value unnecessary, so if we have changes, run another load
1938 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1942 /* Return the number of changes */
1948 static unsigned RunOptGroup5 (CodeSeg* S)
1949 /* Run another round of pattern replacements. These are done late, since there
1950 * may be better replacements before.
1953 unsigned Changes = 0;
1955 Changes += RunOptFunc (S, &DOptPush1, 1);
1956 Changes += RunOptFunc (S, &DOptPush2, 1);
1957 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1959 /* Return the number of changes */
1965 static unsigned RunOptGroup6 (CodeSeg* S)
1966 /* The last group of optimization steps. Adjust branches, do size optimizations.
1969 unsigned Changes = 0;
1972 if (CodeSizeFactor <= 100) {
1973 /* Optimize for size, that is replace operations by shorter ones, even
1974 * if this does hinder further optimizations (no problem since we're
1977 C = RunOptFunc (S, &DOptSize1, 1);
1980 /* Run some optimization passes again, since the size optimizations
1981 * may have opened new oportunities.
1983 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1984 Changes += RunOptFunc (S, &DOptJumpTarget, 5);
1987 C = RunOptFunc (S, &DOptSize2, 1);
1990 /* Run some optimization passes again, since the size optimizations
1991 * may have opened new oportunities.
1993 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1994 Changes += RunOptFunc (S, &DOptJumpTarget, 5);
1997 /* Adjust branch distances */
1998 Changes += RunOptFunc (S, &DOptBranchDist, 3);
2000 /* Replace conditional branches to RTS. If we had changes, we must run dead
2001 * code elimination again, since the change may have introduced dead code.
2003 C = RunOptFunc (S, &DOptRTSJumps2, 1);
2006 Changes += RunOptFunc (S, &DOptDeadCode, 1);
2009 /* Return the number of changes */
2015 void RunOpt (CodeSeg* S)
2016 /* Run the optimizer */
2018 const char* StatFileName;
2020 /* If we shouldn't run the optimizer, bail out */
2025 /* Check if we are requested to write optimizer statistics */
2026 StatFileName = getenv ("CC65_OPTSTATS");
2028 ReadOptStats (StatFileName);
2031 /* Print the name of the function we are working on */
2033 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
2035 Print (stdout, 1, "Running optimizer for global code segment\n");
2038 /* Run groups of optimizations */
2046 /* Write statistics */
2048 WriteOptStats (StatFileName);