1 /*****************************************************************************/
5 /* Optimizer subroutines */
9 /* (C) 2001-2003 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 /* Insert the load from ptr1 */
504 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
505 CS_InsertEntry (S, X, I+5);
506 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[2]->LI);
507 CS_InsertEntry (S, X, I+6);
509 /* Insert the store through ptr1 */
510 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
511 CS_InsertEntry (S, X, I+8+K);
513 /* Delete the old code */
514 CS_DelEntry (S, I+9+K); /* jsr spaspidx */
515 CS_DelEntry (S, I+4); /* jsr ldauidx */
516 CS_DelEntry (S, I); /* jsr pushax */
518 /* Remember, we had changes */
528 /* Return the number of changes made */
534 /*****************************************************************************/
535 /* Optimize loads through pointers */
536 /*****************************************************************************/
540 static unsigned OptPtrLoad1 (CodeSeg* S)
541 /* Search for the sequence:
566 unsigned Changes = 0;
568 /* Walk over the entries */
570 while (I < CS_GetEntryCount (S)) {
575 L[0] = CS_GetEntry (S, I);
577 /* Check for the sequence */
578 if (L[0]->OPC == OP65_CLC &&
579 CS_GetEntries (S, L+1, I+1, 8) &&
580 L[1]->OPC == OP65_ADC &&
581 L[2]->OPC == OP65_TAY &&
582 L[3]->OPC == OP65_TXA &&
583 L[4]->OPC == OP65_ADC &&
584 L[5]->OPC == OP65_TAX &&
585 L[6]->OPC == OP65_TYA &&
586 L[7]->OPC == OP65_LDY &&
587 CE_IsCallTo (L[8], "ldauidx") &&
588 !CS_RangeHasLabel (S, I+1, 8)) {
593 /* Track the insertion point */
597 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[2]->LI);
598 CS_InsertEntry (S, X, IP++);
600 /* If the instruction before the clc is a ldx, replace the
601 * txa by an lda with the same location of the ldx. Otherwise
602 * transfer the value in X to A.
604 if ((P = CS_GetPrevEntry (S, I)) != 0 &&
605 P->OPC == OP65_LDX &&
607 X = NewCodeEntry (OP65_LDA, P->AM, P->Arg, 0, P->LI);
609 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, L[3]->LI);
611 CS_InsertEntry (S, X, IP++);
614 X = NewCodeEntry (OP65_ADC, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
615 CS_InsertEntry (S, X, IP++);
618 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[5]->LI);
619 CS_InsertEntry (S, X, IP++);
622 X = NewCodeEntry (OP65_LDY, L[7]->AM, L[7]->Arg, 0, L[7]->LI);
623 CS_InsertEntry (S, X, IP++);
626 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[8]->LI);
627 CS_InsertEntry (S, X, IP++);
630 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[8]->LI);
631 CS_InsertEntry (S, X, IP++);
633 /* Remove the old instructions */
634 CS_DelEntries (S, IP, 7);
636 /* Remember, we had changes */
646 /* Return the number of changes made */
652 static unsigned OptPtrLoad2 (CodeSeg* S)
653 /* Search for the sequence:
692 unsigned Changes = 0;
694 /* Walk over the entries */
696 while (I < CS_GetEntryCount (S)) {
701 L[0] = CS_GetEntry (S, I);
703 /* Check for the sequence */
704 if (L[0]->OPC == OP65_ADC &&
705 CS_GetEntries (S, L+1, I+1, 8) &&
706 L[1]->OPC == OP65_PHA &&
707 L[2]->OPC == OP65_TXA &&
708 L[3]->OPC == OP65_INY &&
709 L[4]->OPC == OP65_ADC &&
710 L[5]->OPC == OP65_TAX &&
711 L[6]->OPC == OP65_PLA &&
712 L[7]->OPC == OP65_LDY &&
713 CE_IsCallTo (L[8], "ldauidx") &&
714 !CS_RangeHasLabel (S, I+1, 8)) {
718 /* Store the low byte and remove the PHA instead */
719 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
720 CS_InsertEntry (S, X, I+1);
722 /* Store the high byte */
723 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
724 CS_InsertEntry (S, X, I+6);
726 /* Load high and low byte */
727 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[6]->LI);
728 CS_InsertEntry (S, X, I+10);
729 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI);
730 CS_InsertEntry (S, X, I+11);
732 /* Delete the old code */
733 CS_DelEntry (S, I+12); /* jsr ldauidx */
734 CS_DelEntry (S, I+8); /* pla */
735 CS_DelEntry (S, I+7); /* tax */
736 CS_DelEntry (S, I+2); /* pha */
738 /* Remember, we had changes */
748 /* Return the number of changes made */
754 static unsigned OptPtrLoad3 (CodeSeg* S)
755 /* Search for the sequence:
773 unsigned Changes = 0;
775 /* Walk over the entries */
777 while (I < CS_GetEntryCount (S)) {
783 L[0] = CS_GetEntry (S, I);
785 /* Check for the sequence */
786 if (L[0]->OPC == OP65_LDA &&
787 L[0]->AM == AM65_IMM &&
788 CS_GetEntries (S, L+1, I+1, 7) &&
789 L[1]->OPC == OP65_LDX &&
790 L[1]->AM == AM65_IMM &&
791 L[2]->OPC == OP65_CLC &&
792 L[3]->OPC == OP65_ADC &&
793 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
794 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
796 L[4]->JumpTo->Owner == L[6] &&
797 L[5]->OPC == OP65_INX &&
798 L[6]->OPC == OP65_LDY &&
799 CE_KnownImm (L[6]) &&
801 CE_IsCallTo (L[7], "ldauidx") &&
802 !CS_RangeHasLabel (S, I+1, 7) &&
803 /* Check the label last because this is quite costly */
804 (Len = strlen (L[0]->Arg)) > 3 &&
805 L[0]->Arg[0] == '<' &&
806 L[0]->Arg[1] == '(' &&
807 strlen (L[1]->Arg) == Len &&
808 L[1]->Arg[0] == '>' &&
809 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
814 /* We will create all the new stuff behind the current one so
815 * we keep the line references.
817 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
818 CS_InsertEntry (S, X, I+8);
820 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
821 CS_InsertEntry (S, X, I+9);
823 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
825 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
826 CS_InsertEntry (S, X, I+10);
829 /* Remove the old code */
830 CS_DelEntries (S, I, 8);
832 /* Remember, we had changes */
842 /* Return the number of changes made */
848 static unsigned OptPtrLoad4 (CodeSeg* S)
849 /* Search for the sequence:
870 unsigned Changes = 0;
872 /* Walk over the entries */
874 while (I < CS_GetEntryCount (S)) {
880 L[0] = CS_GetEntry (S, I);
882 /* Check for the sequence */
883 if (L[0]->OPC == OP65_LDA &&
884 L[0]->AM == AM65_IMM &&
885 CS_GetEntries (S, L+1, I+1, 8) &&
886 L[1]->OPC == OP65_LDX &&
887 L[1]->AM == AM65_IMM &&
888 !CE_HasLabel (L[1]) &&
889 L[2]->OPC == OP65_LDY &&
890 CE_KnownImm (L[2]) &&
891 !CE_HasLabel (L[2]) &&
892 L[3]->OPC == OP65_CLC &&
893 !CE_HasLabel (L[3]) &&
894 L[4]->OPC == OP65_ADC &&
895 L[4]->AM == AM65_ZP_INDY &&
896 !CE_HasLabel (L[4]) &&
897 (L[5]->OPC == OP65_BCC || L[5]->OPC == OP65_JCC) &&
899 L[5]->JumpTo->Owner == L[7] &&
900 !CE_HasLabel (L[5]) &&
901 L[6]->OPC == OP65_INX &&
902 !CE_HasLabel (L[6]) &&
903 L[7]->OPC == OP65_LDY &&
904 CE_KnownImm (L[7]) &&
906 CE_IsCallTo (L[8], "ldauidx") &&
907 !CE_HasLabel (L[8]) &&
908 /* Check the label last because this is quite costly */
909 (Len = strlen (L[0]->Arg)) > 3 &&
910 L[0]->Arg[0] == '<' &&
911 L[0]->Arg[1] == '(' &&
912 strlen (L[1]->Arg) == Len &&
913 L[1]->Arg[0] == '>' &&
914 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
920 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[4]->Arg, 0, L[0]->LI);
921 CS_InsertEntry (S, X, I+3);
924 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[0]->LI);
925 CS_InsertEntry (S, X, I+4);
928 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
929 CS_InsertEntry (S, X, I+5);
932 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
934 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
935 CS_InsertEntry (S, X, I+6);
938 /* Remove the old code */
939 CS_DelEntries (S, I, 2);
940 CS_DelEntries (S, I+5, 6);
942 /* Remember, we had changes */
952 /* Return the number of changes made */
958 static unsigned OptPtrLoad5 (CodeSeg* S)
959 /* Search for the sequence:
973 unsigned Changes = 0;
975 /* Walk over the entries */
977 while (I < CS_GetEntryCount (S)) {
983 L[0] = CS_GetEntry (S, I);
985 /* Check for the sequence */
986 if (L[0]->OPC == OP65_LDA && L[0]->AM == AM65_ZP &&
987 CS_GetEntries (S, L+1, I+1, 3) &&
988 !CS_RangeHasLabel (S, I+1, 3) &&
989 L[1]->OPC == OP65_LDX && L[1]->AM == AM65_ZP &&
990 (Len = strlen (L[0]->Arg)) > 0 &&
991 strncmp (L[0]->Arg, L[1]->Arg, Len) == 0 &&
992 strcmp (L[1]->Arg + Len, "+1") == 0 &&
993 L[2]->OPC == OP65_LDY &&
994 CE_IsCallTo (L[3], "ldauidx")) {
999 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
1000 CS_InsertEntry (S, X, I+3);
1003 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1004 CS_InsertEntry (S, X, I+4);
1006 /* Remove the old code */
1007 CS_DelEntry (S, I+5);
1008 CS_DelEntries (S, I, 2);
1010 /* Remember, we had changes */
1020 /* Return the number of changes made */
1026 static unsigned OptPtrLoad6 (CodeSeg* S)
1027 /* Search for the sequence:
1034 * and replace it by:
1043 unsigned Changes = 0;
1045 /* Walk over the entries */
1047 while (I < CS_GetEntryCount (S)) {
1052 /* Get next entry */
1053 L[0] = CS_GetEntry (S, I);
1055 /* Check for the sequence */
1056 if (L[0]->OPC == OP65_LDA && L[0]->AM == AM65_ZP &&
1057 CS_GetEntries (S, L+1, I+1, 3) &&
1058 !CS_RangeHasLabel (S, I+1, 3) &&
1059 L[1]->OPC == OP65_LDX && L[1]->AM == AM65_ZP &&
1060 (Len = strlen (L[0]->Arg)) > 0 &&
1061 strncmp (L[0]->Arg, L[1]->Arg, Len) == 0 &&
1062 strcmp (L[1]->Arg + Len, "+1") == 0 &&
1063 L[2]->OPC == OP65_LDY &&
1064 CE_IsCallTo (L[3], "ldaxidx")) {
1069 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1070 CS_InsertEntry (S, X, I+4);
1073 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[3]->LI);
1074 CS_InsertEntry (S, X, I+5);
1077 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[3]->LI);
1078 CS_InsertEntry (S, X, I+6);
1081 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1082 CS_InsertEntry (S, X, I+7);
1084 /* Remove the old code */
1085 CS_DelEntry (S, I+3);
1086 CS_DelEntries (S, I, 2);
1088 /* Remember, we had changes */
1098 /* Return the number of changes made */
1104 static unsigned OptPtrLoad7 (CodeSeg* S)
1105 /* Search for the sequence
1110 * and replace it by:
1118 * This step must be execute *after* OptPtrLoad1!
1121 unsigned Changes = 0;
1123 /* Walk over the entries */
1125 while (I < CS_GetEntryCount (S)) {
1129 /* Get next entry */
1130 L[0] = CS_GetEntry (S, I);
1132 /* Check for the sequence */
1133 if (L[0]->OPC == OP65_LDY &&
1134 CS_GetEntries (S, L+1, I+1, 1) &&
1135 CE_IsCallTo (L[1], "ldauidx") &&
1136 !CE_HasLabel (L[1])) {
1140 /* Store the high byte */
1141 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
1142 CS_InsertEntry (S, X, I+1);
1144 /* Store the low byte */
1145 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
1146 CS_InsertEntry (S, X, I+2);
1148 /* Delete the call to ldauidx */
1149 CS_DelEntry (S, I+3);
1151 /* Load the high and low byte */
1152 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1153 CS_InsertEntry (S, X, I+3);
1154 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[0]->LI);
1155 CS_InsertEntry (S, X, I+4);
1157 /* Remember, we had changes */
1167 /* Return the number of changes made */
1173 /*****************************************************************************/
1174 /* Decouple operations */
1175 /*****************************************************************************/
1179 static unsigned OptDecouple (CodeSeg* S)
1180 /* Decouple operations, that is, do the following replacements:
1190 * lda zp -> lda #imm
1191 * ldx zp -> ldx #imm
1192 * ldy zp -> ldy #imm
1194 * Provided that the register values are known of course.
1197 unsigned Changes = 0;
1200 /* Generate register info for the following step */
1203 /* Walk over the entries */
1205 while (I < CS_GetEntryCount (S)) {
1209 /* Get next entry and it's input register values */
1210 CodeEntry* E = CS_GetEntry (S, I);
1211 const RegContents* In = &E->RI->In;
1213 /* Assume we have no replacement */
1216 /* Check the instruction */
1220 if (E->RI->In.RegX >= 0) {
1221 Arg = MakeHexArg ((E->RI->In.RegX - 1) & 0xFF);
1222 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1227 if (E->RI->In.RegY >= 0) {
1228 Arg = MakeHexArg ((E->RI->In.RegY - 1) & 0xFF);
1229 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1234 if (E->RI->In.RegX >= 0) {
1235 Arg = MakeHexArg ((E->RI->In.RegX + 1) & 0xFF);
1236 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1241 if (E->RI->In.RegY >= 0) {
1242 Arg = MakeHexArg ((E->RI->In.RegY + 1) & 0xFF);
1243 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1248 if (E->AM == AM65_ZP) {
1249 switch (GetKnownReg (E->Use, In)) {
1251 Arg = MakeHexArg (In->Tmp1);
1252 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1256 Arg = MakeHexArg (In->SRegLo);
1257 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1261 Arg = MakeHexArg (In->SRegHi);
1262 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1269 if (E->AM == AM65_ZP) {
1270 switch (GetKnownReg (E->Use, In)) {
1272 Arg = MakeHexArg (In->Tmp1);
1273 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1277 Arg = MakeHexArg (In->SRegLo);
1278 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1282 Arg = MakeHexArg (In->SRegHi);
1283 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1290 if (E->AM == AM65_ZP) {
1291 switch (GetKnownReg (E->Use, In)) {
1293 Arg = MakeHexArg (In->Tmp1);
1294 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1298 Arg = MakeHexArg (In->SRegLo);
1299 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1303 Arg = MakeHexArg (In->SRegHi);
1304 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1311 if (E->RI->In.RegA >= 0) {
1312 Arg = MakeHexArg (In->RegA);
1313 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1318 if (E->RI->In.RegA >= 0) {
1319 Arg = MakeHexArg (In->RegA);
1320 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1325 if (E->RI->In.RegX >= 0) {
1326 Arg = MakeHexArg (In->RegX);
1327 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1332 if (E->RI->In.RegY >= 0) {
1333 Arg = MakeHexArg (In->RegY);
1334 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1339 /* Avoid gcc warnings */
1344 /* Insert the replacement if we have one */
1346 CS_InsertEntry (S, X, I+1);
1356 /* Free register info */
1359 /* Return the number of changes made */
1365 /*****************************************************************************/
1366 /* struct OptFunc */
1367 /*****************************************************************************/
1371 typedef struct OptFunc OptFunc;
1373 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1374 const char* Name; /* Name of the function/group */
1375 unsigned CodeSizeFactor; /* Code size factor for this opt func */
1376 unsigned long TotalRuns; /* Total number of runs */
1377 unsigned long LastRuns; /* Last number of runs */
1378 unsigned long TotalChanges; /* Total number of changes */
1379 unsigned long LastChanges; /* Last number of changes */
1380 char Disabled; /* True if function disabled */
1385 /*****************************************************************************/
1387 /*****************************************************************************/
1391 /* A list of all the function descriptions */
1392 static OptFunc DOpt65C02BitOps = { Opt65C02BitOps, "Opt65C02BitOps", 66, 0, 0, 0, 0, 0 };
1393 static OptFunc DOpt65C02Ind = { Opt65C02Ind, "Opt65C02Ind", 100, 0, 0, 0, 0, 0 };
1394 static OptFunc DOpt65C02Stores = { Opt65C02Stores, "Opt65C02Stores", 100, 0, 0, 0, 0, 0 };
1395 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 125, 0, 0, 0, 0, 0 };
1396 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 200, 0, 0, 0, 0, 0 };
1397 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 90, 0, 0, 0, 0, 0 };
1398 static OptFunc DOptAdd4 = { OptAdd4, "OptAdd4", 100, 0, 0, 0, 0, 0 };
1399 static OptFunc DOptAdd5 = { OptAdd5, "OptAdd5", 40, 0, 0, 0, 0, 0 };
1400 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 100, 0, 0, 0, 0, 0 };
1401 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0, 0 };
1402 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 85, 0, 0, 0, 0, 0 };
1403 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 75, 0, 0, 0, 0, 0 };
1404 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 75, 0, 0, 0, 0, 0 };
1405 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 100, 0, 0, 0, 0, 0 };
1406 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 100, 0, 0, 0, 0, 0 };
1407 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 85, 0, 0, 0, 0, 0 };
1408 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 50, 0, 0, 0, 0, 0 };
1409 static OptFunc DOptCondBranches = { OptCondBranches, "OptCondBranches", 80, 0, 0, 0, 0, 0 };
1410 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 100, 0, 0, 0, 0, 0 };
1411 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 100, 0, 0, 0, 0, 0 };
1412 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 100, 0, 0, 0, 0, 0 };
1413 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0, 0 };
1414 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 100, 0, 0, 0, 0, 0 };
1415 static OptFunc DOptJumpTarget = { OptJumpTarget, "OptJumpTarget", 100, 0, 0, 0, 0, 0 };
1416 static OptFunc DOptLoad1 = { OptLoad1, "OptLoad1", 100, 0, 0, 0, 0, 0 };
1417 static OptFunc DOptRTS = { OptRTS, "OptRTS", 100, 0, 0, 0, 0, 0 };
1418 static OptFunc DOptRTSJumps1 = { OptRTSJumps1, "OptRTSJumps1", 100, 0, 0, 0, 0, 0 };
1419 static OptFunc DOptRTSJumps2 = { OptRTSJumps2, "OptRTSJumps2", 100, 0, 0, 0, 0, 0 };
1420 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 100, 0, 0, 0, 0, 0 };
1421 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 100, 0, 0, 0, 0, 0 };
1422 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 100, 0, 0, 0, 0, 0 };
1423 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 100, 0, 0, 0, 0, 0 };
1424 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 100, 0, 0, 0, 0, 0 };
1425 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 100, 0, 0, 0, 0, 0 };
1426 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 100, 0, 0, 0, 0, 0 };
1427 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 };
1428 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 };
1429 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 };
1430 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 65, 0, 0, 0, 0, 0 };
1431 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 86, 0, 0, 0, 0, 0 };
1432 static OptFunc DOptPtrLoad7 = { OptPtrLoad7, "OptPtrLoad7", 100, 0, 0, 0, 0, 0 };
1433 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 100, 0, 0, 0, 0, 0 };
1434 static OptFunc DOptPush1 = { OptPush1, "OptPush1", 65, 0, 0, 0, 0, 0 };
1435 static OptFunc DOptPush2 = { OptPush2, "OptPush2", 50, 0, 0, 0, 0, 0 };
1436 static OptFunc DOptPushPop = { OptPushPop, "OptPushPop", 0, 0, 0, 0, 0, 0 };
1437 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 100, 0, 0, 0, 0, 0 };
1438 static OptFunc DOptShift2 = { OptShift2, "OptShift2", 100, 0, 0, 0, 0, 0 };
1439 static OptFunc DOptShift3 = { OptShift3, "OptShift3", 110, 0, 0, 0, 0, 0 };
1440 static OptFunc DOptSize1 = { OptSize1, "OptSize1", 100, 0, 0, 0, 0, 0 };
1441 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 100, 0, 0, 0, 0, 0 };
1442 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 100, 0, 0, 0, 0, 0 };
1443 static OptFunc DOptStore1 = { OptStore1, "OptStore1", 220, 0, 0, 0, 0, 0 };
1444 static OptFunc DOptStore2 = { OptStore2, "OptStore2", 120, 0, 0, 0, 0, 0 };
1445 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 };
1446 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 };
1447 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 };
1448 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 100, 0, 0, 0, 0, 0 };
1449 static OptFunc DOptTransfers = { OptTransfers, "OptTransfers", 0, 0, 0, 0, 0, 0 };
1450 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0, 0 };
1451 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0, 0 };
1454 /* Table containing all the steps in alphabetical order */
1455 static OptFunc* OptFuncs[] = {
1517 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1521 static int CmpOptStep (const void* Key, const void* Func)
1522 /* Compare function for bsearch */
1524 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1529 static OptFunc* FindOptFunc (const char* Name)
1530 /* Find an optimizer step by name in the table and return a pointer. Return
1531 * NULL if no such step is found.
1534 /* Search for the function in the list */
1535 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1541 static OptFunc* GetOptFunc (const char* Name)
1542 /* Find an optimizer step by name in the table and return a pointer. Print an
1543 * error and call AbEnd if not found.
1546 /* Search for the function in the list */
1547 OptFunc* F = FindOptFunc (Name);
1550 AbEnd ("Optimization step `%s' not found", Name);
1557 void DisableOpt (const char* Name)
1558 /* Disable the optimization with the given name */
1560 if (strcmp (Name, "any") == 0) {
1562 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1563 OptFuncs[I]->Disabled = 1;
1566 GetOptFunc(Name)->Disabled = 1;
1572 void EnableOpt (const char* Name)
1573 /* Enable the optimization with the given name */
1575 if (strcmp (Name, "any") == 0) {
1577 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1578 OptFuncs[I]->Disabled = 0;
1581 GetOptFunc(Name)->Disabled = 0;
1587 void ListOptSteps (FILE* F)
1588 /* List all optimization steps */
1591 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1592 fprintf (F, "%s\n", OptFuncs[I]->Name);
1598 static void ReadOptStats (const char* Name)
1599 /* Read the optimizer statistics file */
1604 /* Try to open the file */
1605 FILE* F = fopen (Name, "r");
1607 /* Ignore the error */
1611 /* Read and parse the lines */
1613 while (fgets (Buf, sizeof (Buf), F) != 0) {
1621 unsigned long TotalRuns;
1622 unsigned long TotalChanges;
1627 /* Remove trailing white space including the line terminator */
1630 while (Len > 0 && IsSpace (B[Len-1])) {
1635 /* Remove leading whitespace */
1636 while (IsSpace (*B)) {
1640 /* Check for empty and comment lines */
1641 if (*B == '\0' || *B == ';' || *B == '#') {
1645 /* Parse the line */
1646 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1651 /* Search for the optimizer step. */
1652 Func = FindOptFunc (Name);
1658 /* Found the step, set the fields */
1659 Func->TotalRuns = TotalRuns;
1660 Func->TotalChanges = TotalChanges;
1664 /* Close the file, ignore errors here. */
1670 static void WriteOptStats (const char* Name)
1671 /* Write the optimizer statistics file */
1675 /* Try to open the file */
1676 FILE* F = fopen (Name, "w");
1678 /* Ignore the error */
1682 /* Write a header */
1684 "; Optimizer Total Last Total Last\n"
1685 "; Step Runs Runs Chg Chg\n");
1688 /* Write the data */
1689 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1690 const OptFunc* O = OptFuncs[I];
1692 "%-20s %6lu %6lu %6lu %6lu\n",
1700 /* Close the file, ignore errors here. */
1706 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1707 /* Run one optimizer function Max times or until there are no more changes */
1709 unsigned Changes, C;
1711 /* Don't run the function if it is disabled or if it is prohibited by the
1714 if (F->Disabled || F->CodeSizeFactor > CodeSizeFactor) {
1718 /* Run this until there are no more changes */
1722 /* Run the function */
1729 F->TotalChanges += C;
1730 F->LastChanges += C;
1732 } while (--Max && C > 0);
1734 /* Return the number of changes */
1740 static unsigned RunOptGroup1 (CodeSeg* S)
1741 /* Run the first group of optimization steps. These steps translate known
1742 * patterns emitted by the code generator into more optimal patterns. Order
1743 * of the steps is important, because some of the steps done earlier cover
1744 * the same patterns as later steps as subpatterns.
1747 unsigned Changes = 0;
1749 Changes += RunOptFunc (S, &DOptPtrStore1, 1);
1750 Changes += RunOptFunc (S, &DOptPtrLoad1, 1);
1751 Changes += RunOptFunc (S, &DOptPtrLoad2, 1);
1752 Changes += RunOptFunc (S, &DOptPtrLoad3, 1);
1753 Changes += RunOptFunc (S, &DOptPtrLoad4, 1);
1754 Changes += RunOptFunc (S, &DOptPtrLoad5, 1);
1755 Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
1756 Changes += RunOptFunc (S, &DOptNegAX1, 1);
1757 Changes += RunOptFunc (S, &DOptNegAX2, 1);
1758 Changes += RunOptFunc (S, &DOptNegAX3, 1);
1759 Changes += RunOptFunc (S, &DOptNegAX4, 1);
1760 Changes += RunOptFunc (S, &DOptAdd1, 1);
1761 Changes += RunOptFunc (S, &DOptAdd2, 1);
1762 Changes += RunOptFunc (S, &DOptAdd3, 1);
1763 Changes += RunOptFunc (S, &DOptShift1, 1);
1764 Changes += RunOptFunc (S, &DOptShift2, 1);
1765 Changes += RunOptFunc (S, &DOptShift3, 1);
1766 Changes += RunOptFunc (S, &DOptStore1, 5);
1767 Changes += RunOptFunc (S, &DOptStore2, 5);
1769 /* Return the number of changes */
1775 static unsigned RunOptGroup2 (CodeSeg* S)
1776 /* Run one group of optimization steps. This step involves just decoupling
1777 * instructions by replacing them by instructions that do not depend on
1778 * previous instructions. This makes it easier to find instructions that
1782 unsigned Changes = 0;
1784 Changes += RunOptFunc (S, &DOptDecouple, 1);
1786 /* Return the number of changes */
1792 static unsigned RunOptGroup3 (CodeSeg* S)
1793 /* Run one group of optimization steps. These steps depend on each other,
1794 * that means that one step may allow another step to do additional work,
1795 * so we will repeat the steps as long as we see any changes.
1798 unsigned Changes, C;
1804 C += RunOptFunc (S, &DOptPtrLoad7, 1);
1805 C += RunOptFunc (S, &DOptNegA1, 1);
1806 C += RunOptFunc (S, &DOptNegA2, 1);
1807 C += RunOptFunc (S, &DOptSub1, 1);
1808 C += RunOptFunc (S, &DOptSub2, 1);
1809 C += RunOptFunc (S, &DOptAdd4, 1);
1810 C += RunOptFunc (S, &DOptAdd5, 1);
1811 C += RunOptFunc (S, &DOptStackOps, 1);
1812 C += RunOptFunc (S, &DOptJumpCascades, 1);
1813 C += RunOptFunc (S, &DOptDeadJumps, 1);
1814 C += RunOptFunc (S, &DOptRTS, 1);
1815 C += RunOptFunc (S, &DOptDeadCode, 1);
1816 C += RunOptFunc (S, &DOptJumpTarget, 1);
1817 C += RunOptFunc (S, &DOptCondBranches, 1);
1818 C += RunOptFunc (S, &DOptRTSJumps1, 1);
1819 C += RunOptFunc (S, &DOptBoolTrans, 1);
1820 C += RunOptFunc (S, &DOptCmp1, 1);
1821 C += RunOptFunc (S, &DOptCmp2, 1);
1822 C += RunOptFunc (S, &DOptCmp3, 1);
1823 C += RunOptFunc (S, &DOptCmp4, 1);
1824 C += RunOptFunc (S, &DOptCmp5, 1);
1825 C += RunOptFunc (S, &DOptCmp6, 1);
1826 C += RunOptFunc (S, &DOptCmp7, 1);
1827 C += RunOptFunc (S, &DOptTest1, 1);
1828 C += RunOptFunc (S, &DOptLoad1, 1);
1829 C += RunOptFunc (S, &DOptUnusedLoads, 1);
1830 C += RunOptFunc (S, &DOptUnusedStores, 1);
1831 C += RunOptFunc (S, &DOptDupLoads, 1);
1832 C += RunOptFunc (S, &DOptStoreLoad, 1);
1833 C += RunOptFunc (S, &DOptTransfers, 1);
1834 C += RunOptFunc (S, &DOptPushPop, 1);
1840 /* Return the number of changes */
1846 static unsigned RunOptGroup4 (CodeSeg* S)
1847 /* 65C02 specific optimizations. */
1849 unsigned Changes = 0;
1851 if (CPU >= CPU_65C02) {
1852 Changes += RunOptFunc (S, &DOpt65C02BitOps, 1);
1853 Changes += RunOptFunc (S, &DOpt65C02Ind, 1);
1854 Changes += RunOptFunc (S, &DOpt65C02Stores, 1);
1856 /* The 65C02 replacement codes do often make the use of a register
1857 * value unnecessary, so if we have changes, run another load
1860 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1864 /* Return the number of changes */
1870 static unsigned RunOptGroup5 (CodeSeg* S)
1871 /* Run another round of pattern replacements. These are done late, since there
1872 * may be better replacements before.
1875 unsigned Changes = 0;
1877 Changes += RunOptFunc (S, &DOptPush1, 1);
1878 Changes += RunOptFunc (S, &DOptPush2, 1);
1879 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1881 /* Return the number of changes */
1887 static unsigned RunOptGroup6 (CodeSeg* S)
1888 /* The last group of optimization steps. Adjust branches, do size optimizations.
1891 unsigned Changes = 0;
1894 if (CodeSizeFactor <= 100) {
1895 /* Optimize for size, that is replace operations by shorter ones, even
1896 * if this does hinder further optimizations (no problem since we're
1899 C = RunOptFunc (S, &DOptSize1, 1);
1902 /* Run some optimization passes again, since the size optimizations
1903 * may have opened new oportunities.
1905 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1906 Changes += RunOptFunc (S, &DOptJumpTarget, 5);
1909 C = RunOptFunc (S, &DOptSize2, 1);
1912 /* Run some optimization passes again, since the size optimizations
1913 * may have opened new oportunities.
1915 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1916 Changes += RunOptFunc (S, &DOptJumpTarget, 5);
1919 /* Adjust branch distances */
1920 Changes += RunOptFunc (S, &DOptBranchDist, 3);
1922 /* Replace conditional branches to RTS. If we had changes, we must run dead
1923 * code elimination again, since the change may have introduced dead code.
1925 C = RunOptFunc (S, &DOptRTSJumps2, 1);
1928 Changes += RunOptFunc (S, &DOptDeadCode, 1);
1931 /* Return the number of changes */
1937 void RunOpt (CodeSeg* S)
1938 /* Run the optimizer */
1940 const char* StatFileName;
1942 /* If we shouldn't run the optimizer, bail out */
1947 /* Check if we are requested to write optimizer statistics */
1948 StatFileName = getenv ("CC65_OPTSTATS");
1950 ReadOptStats (StatFileName);
1953 /* Print the name of the function we are working on */
1955 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
1957 Print (stdout, 1, "Running optimizer for global code segment\n");
1960 /* Run groups of optimizations */
1968 /* Write statistics */
1970 WriteOptStats (StatFileName);