1 /*****************************************************************************/
5 /* Optimizer subroutines */
9 /* (C) 2001-2010, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
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 /*****************************************************************************/
56 #include "coptptrload.h"
60 #include "coptstore.h"
69 /*****************************************************************************/
71 /*****************************************************************************/
86 /*****************************************************************************/
88 /*****************************************************************************/
92 static unsigned OptShift1 (CodeSeg* S)
93 /* A call to the shlaxN routine may get replaced by one or more asl insns
94 * if the value of X is not used later. If X is used later, but it is zero
95 * on entry and it's a shift by one, it may get replaced by:
104 unsigned Changes = 0;
107 /* Generate register info */
110 /* Walk over the entries */
112 while (I < CS_GetEntryCount (S)) {
119 CodeEntry* E = CS_GetEntry (S, I);
121 /* Check for the sequence */
122 if (E->OPC == OP65_JSR &&
123 (strncmp (E->Arg, "shlax", 5) == 0 ||
124 strncmp (E->Arg, "aslax", 5) == 0) &&
125 strlen (E->Arg) == 6 &&
126 IsDigit (E->Arg[5])) {
128 if (!RegXUsed (S, I+1)) {
130 /* Insert shift insns */
131 unsigned Count = E->Arg[5] - '0';
133 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
134 CS_InsertEntry (S, X, I+1);
137 /* Delete the call to shlax */
140 /* Remember, we had changes */
143 } else if (E->RI->In.RegX == 0 &&
145 (N = CS_GetNextEntry (S, I)) != 0) {
148 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
149 CS_InsertEntry (S, X, I+1);
152 L = CS_GenLabel (S, N);
153 X = NewCodeEntry (OP65_BCC, AM65_BRA, L->Name, L, E->LI);
154 CS_InsertEntry (S, X, I+2);
157 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, E->LI);
158 CS_InsertEntry (S, X, I+3);
160 /* Delete the call to shlax */
163 /* Remember, we had changes */
174 /* Free the register info */
177 /* Return the number of changes made */
183 static unsigned OptShift2(CodeSeg* S)
184 /* A call to the asrax1 routines may get replaced by something simpler, if
185 * X is not used later:
192 unsigned Changes = 0;
195 /* Generate register info */
198 /* Walk over the entries */
200 while (I < CS_GetEntryCount (S)) {
205 CodeEntry* E = CS_GetEntry (S, I);
207 /* Check for the sequence */
208 if (E->OPC == OP65_JSR &&
209 strncmp (E->Arg, "asrax", 5) == 0 &&
210 strlen (E->Arg) == 6 &&
211 IsDigit (E->Arg[5]) &&
212 (Count = (E->Arg[5] - '0')) >= 1 &&
213 Count * 100 <= S->CodeSizeFactor &&
214 !RegXUsed (S, I+1)) {
219 /* Generate the replacement sequence */
222 X = NewCodeEntry (OP65_CMP, AM65_IMM, "$80", 0, E->LI);
223 CS_InsertEntry (S, X, J++);
226 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, E->LI);
227 CS_InsertEntry (S, X, J++);
230 /* Delete the call to asrax */
233 /* Remember, we had changes */
242 /* Free the register info */
245 /* Return the number of changes made */
251 static unsigned OptShift3 (CodeSeg* S)
252 /* A call to the shraxN routine may get replaced by one or more lsr insns
253 * if the value of X is zero.
256 unsigned Changes = 0;
259 /* Generate register info */
262 /* Walk over the entries */
264 while (I < CS_GetEntryCount (S)) {
267 CodeEntry* E = CS_GetEntry (S, I);
269 /* Check for the sequence */
270 if (E->OPC == OP65_JSR &&
271 strncmp (E->Arg, "shrax", 5) == 0 &&
272 strlen (E->Arg) == 6 &&
273 IsDigit (E->Arg[5]) &&
274 E->RI->In.RegX == 0) {
276 /* Insert shift insns */
277 unsigned Count = E->Arg[5] - '0';
279 CodeEntry* X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, E->LI);
280 CS_InsertEntry (S, X, I+1);
283 /* Delete the call to shrax */
286 /* Remember, we had changes */
296 /* Free the register info */
299 /* Return the number of changes made */
305 static unsigned GetShiftType (const char* Sub)
306 /* Helper function for OptShift3 */
309 if (strcmp (Sub+1, "slax1") == 0) {
311 } else if (strcmp (Sub+1, "srax1") == 0) {
314 } else if (*Sub == 's') {
315 if (strcmp (Sub+1, "hlax1") == 0) {
317 } else if (strcmp (Sub+1, "hrax1") == 0) {
326 static unsigned OptShift4 (CodeSeg* S)
327 /* Search for the sequence
331 * jsr aslax1/asrax1/shlax1/shrax1
344 * or similar, provided that a/x is not used later
347 unsigned Changes = 0;
349 /* Walk over the entries */
351 while (I < CS_GetEntryCount (S)) {
357 L[0] = CS_GetEntry (S, I);
359 /* Check for the sequence */
360 if (L[0]->OPC == OP65_LDA &&
361 (L[0]->AM == AM65_ABS || L[0]->AM == AM65_ZP) &&
362 CS_GetEntries (S, L+1, I+1, 4) &&
363 !CS_RangeHasLabel (S, I+1, 4) &&
364 L[1]->OPC == OP65_LDX &&
365 (L[1]->AM == AM65_ABS || L[1]->AM == AM65_ZP) &&
366 L[2]->OPC == OP65_JSR &&
367 (ShiftType = GetShiftType (L[2]->Arg)) != SHIFT_NONE&&
368 L[3]->OPC == OP65_STA &&
369 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
370 L[4]->OPC == OP65_STX &&
371 (L[4]->AM == AM65_ABS || L[4]->AM == AM65_ZP) &&
372 !RegAXUsed (S, I+5)) {
376 /* Handle the four shift types differently */
380 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
381 CS_InsertEntry (S, X, I+5);
382 X = NewCodeEntry (OP65_CMP, AM65_IMM, "$80", 0, L[2]->LI);
383 CS_InsertEntry (S, X, I+6);
384 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
385 CS_InsertEntry (S, X, I+7);
386 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
387 CS_InsertEntry (S, X, I+8);
388 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
389 CS_InsertEntry (S, X, I+9);
390 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
391 CS_InsertEntry (S, X, I+10);
392 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
393 CS_InsertEntry (S, X, I+11);
394 CS_DelEntries (S, I, 5);
398 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
399 CS_InsertEntry (S, X, I+5);
400 X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, L[2]->LI);
401 CS_InsertEntry (S, X, I+6);
402 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
403 CS_InsertEntry (S, X, I+7);
404 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
405 CS_InsertEntry (S, X, I+8);
406 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
407 CS_InsertEntry (S, X, I+9);
408 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
409 CS_InsertEntry (S, X, I+10);
410 CS_DelEntries (S, I, 5);
415 /* These two are identical */
416 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, L[2]->LI);
417 CS_InsertEntry (S, X, I+1);
418 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
419 CS_InsertEntry (S, X, I+2);
420 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
421 CS_InsertEntry (S, X, I+3);
422 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, L[2]->LI);
423 CS_InsertEntry (S, X, I+4);
424 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
425 CS_InsertEntry (S, X, I+5);
426 CS_DelEntries (S, I+6, 4);
431 /* Remember, we had changes */
441 /* Return the number of changes made */
447 static unsigned OptShift5 (CodeSeg* S)
448 /* Inline the shift subroutines. */
450 unsigned Changes = 0;
452 /* Walk over the entries */
454 while (I < CS_GetEntryCount (S)) {
460 CodeEntry* E = CS_GetEntry (S, I);
462 /* Check for a call to one of the shift routine */
463 if (E->OPC == OP65_JSR &&
464 (strncmp (E->Arg, "shlax", 5) == 0 ||
465 strncmp (E->Arg, "aslax", 5) == 0) &&
466 strlen (E->Arg) == 6 &&
467 IsDigit (E->Arg[5])) {
469 /* Get number of shifts */
470 unsigned ShiftCount = (E->Arg[5] - '0');
477 * (repeat ShiftCount-1 times)
480 * which makes 4 + 3 * ShiftCount bytes, compared to the original
481 * 3 bytes for the subroutine call. However, in most cases, the
482 * final load of the X register gets merged with some other insn
483 * and replaces a txa, so for a shift count of 1, we get a factor
484 * of 200, which matches nicely the CodeSizeFactor enabled with -Oi
486 if (ShiftCount > 1 || S->CodeSizeFactor > 200) {
487 unsigned Size = 4 + 3 * ShiftCount;
488 if ((Size * 100 / 3) > S->CodeSizeFactor) {
494 /* Inline the code. Insertion point is behind the subroutine call */
498 X = NewCodeEntry (OP65_STX, AM65_ZP, "tmp1", 0, E->LI);
499 CS_InsertEntry (S, X, IP++);
501 while (ShiftCount--) {
503 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
504 CS_InsertEntry (S, X, IP++);
507 X = NewCodeEntry (OP65_ROL, AM65_ZP, "tmp1", 0, E->LI);
508 CS_InsertEntry (S, X, IP++);
512 X = NewCodeEntry (OP65_LDX, AM65_ZP, "tmp1", 0, E->LI);
513 CS_InsertEntry (S, X, IP++);
515 /* Remove the subroutine call */
518 /* Remember, we had changes */
528 /* Return the number of changes made */
534 /*****************************************************************************/
536 /*****************************************************************************/
540 static unsigned OptLoad1 (CodeSeg* S)
541 /* Search for a call to ldaxysp where X is not used later and replace it by
542 * a load of just the A register.
546 unsigned Changes = 0;
548 /* Generate register info */
551 /* Walk over the entries */
553 while (I < CS_GetEntryCount (S)) {
558 E = CS_GetEntry (S, I);
560 /* Check for the sequence */
561 if (CE_IsCallTo (E, "ldaxysp") &&
562 RegValIsKnown (E->RI->In.RegY) &&
563 !RegXUsed (S, I+1)) {
567 /* Reload the Y register */
568 const char* Arg = MakeHexArg (E->RI->In.RegY - 1);
569 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
570 CS_InsertEntry (S, X, I+1);
572 /* Load from stack */
573 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, E->LI);
574 CS_InsertEntry (S, X, I+2);
576 /* Now remove the call to the subroutine */
579 /* Remember, we had changes */
589 /* Free the register info */
592 /* Return the number of changes made */
598 static unsigned OptLoad2 (CodeSeg* S)
599 /* Replace calls to ldaxysp by inline code */
602 unsigned Changes = 0;
604 /* Walk over the entries */
606 while (I < CS_GetEntryCount (S)) {
611 E = CS_GetEntry (S, I);
613 /* Check for the sequence */
614 if (CE_IsCallTo (E, "ldaxysp")) {
619 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, E->LI);
620 CS_InsertEntry (S, X, I+1);
623 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, E->LI);
624 CS_InsertEntry (S, X, I+2);
627 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, E->LI);
628 CS_InsertEntry (S, X, I+3);
631 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, E->LI);
632 CS_InsertEntry (S, X, I+4);
634 /* Now remove the call to the subroutine */
637 /* Remember, we had changes */
646 /* Return the number of changes made */
652 /*****************************************************************************/
653 /* Optimize stores through pointers */
654 /*****************************************************************************/
658 static unsigned OptPtrStore1Sub (CodeSeg* S, unsigned I, CodeEntry** const L)
659 /* Check if this is one of the allowed suboperation for OptPtrStore1 */
661 /* Check for a label attached to the entry */
662 if (CE_HasLabel (L[0])) {
666 /* Check for single insn sub ops */
667 if (L[0]->OPC == OP65_AND ||
668 L[0]->OPC == OP65_EOR ||
669 L[0]->OPC == OP65_ORA ||
670 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shlax", 5) == 0) ||
671 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shrax", 5) == 0)) {
676 } else if (L[0]->OPC == OP65_CLC &&
677 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
678 L[1]->OPC == OP65_ADC &&
679 !CE_HasLabel (L[1])) {
681 } else if (L[0]->OPC == OP65_SEC &&
682 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
683 L[1]->OPC == OP65_SBC &&
684 !CE_HasLabel (L[1])) {
696 static unsigned OptPtrStore1 (CodeSeg* S)
697 /* Search for the sequence:
717 * In case a/x is loaded from the register bank before the pushax, we can even
718 * use the register bank instead of ptr1.
732 unsigned Changes = 0;
734 /* Walk over the entries */
736 while (I < CS_GetEntryCount (S)) {
742 L[0] = CS_GetEntry (S, I);
744 /* Check for the sequence */
745 if (CE_IsCallTo (L[0], "pushax") &&
746 CS_GetEntries (S, L+1, I+1, 3) &&
747 L[1]->OPC == OP65_LDY &&
748 CE_IsConstImm (L[1]) &&
749 !CE_HasLabel (L[1]) &&
750 CE_IsCallTo (L[2], "ldauidx") &&
751 !CE_HasLabel (L[2]) &&
752 (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
753 CS_GetEntries (S, L+3+K, I+3+K, 2) &&
754 L[3+K]->OPC == OP65_LDY &&
755 CE_IsConstImm (L[3+K]) &&
756 !CE_HasLabel (L[3+K]) &&
757 CE_IsCallTo (L[4+K], "staspidx") &&
758 !CE_HasLabel (L[4+K])) {
761 const char* RegBank = 0;
762 const char* ZPLoc = "ptr1";
766 /* Get the preceeding two instructions and check them. We check
773 P[0] = CS_GetEntry (S, I-2);
774 P[1] = CS_GetEntry (S, I-1);
775 if (P[0]->OPC == OP65_LDA &&
776 P[0]->AM == AM65_ZP &&
777 P[1]->OPC == OP65_LDX &&
778 P[1]->AM == AM65_ZP &&
779 !CE_HasLabel (P[1]) &&
780 strncmp (P[0]->Arg, "regbank+", 8) == 0) {
782 unsigned Len = strlen (P[0]->Arg);
784 if (strncmp (P[0]->Arg, P[1]->Arg, Len) == 0 &&
785 P[1]->Arg[Len+0] == '+' &&
786 P[1]->Arg[Len+1] == '1' &&
787 P[1]->Arg[Len+2] == '\0') {
789 /* Ok, found. Use the name of the register bank */
790 RegBank = ZPLoc = P[0]->Arg;
795 /* Insert the load via the zp pointer */
796 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
797 CS_InsertEntry (S, X, I+3);
798 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, ZPLoc, 0, L[2]->LI);
799 CS_InsertEntry (S, X, I+4);
801 /* Insert the store through the zp pointer */
802 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, ZPLoc, 0, L[3]->LI);
803 CS_InsertEntry (S, X, I+6+K);
805 /* Delete the old code */
806 CS_DelEntry (S, I+7+K); /* jsr spaspidx */
807 CS_DelEntry (S, I+2); /* jsr ldauidx */
809 /* Create and insert the stores into the zp pointer if needed */
811 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
812 CS_InsertEntry (S, X, I+1);
813 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
814 CS_InsertEntry (S, X, I+2);
817 /* Delete more old code. Do it here to keep a label attached to
820 CS_DelEntry (S, I); /* jsr pushax */
822 /* Remember, we had changes */
832 /* Return the number of changes made */
838 static unsigned OptPtrStore2 (CodeSeg* S)
839 /* Search for the sequence:
861 unsigned Changes = 0;
863 /* Walk over the entries */
865 while (I < CS_GetEntryCount (S)) {
871 L[0] = CS_GetEntry (S, I);
873 /* Check for the sequence */
874 if (L[0]->OPC == OP65_LDA &&
875 L[0]->AM == AM65_IMM &&
876 CS_GetEntries (S, L+1, I+1, 10) &&
877 L[1]->OPC == OP65_LDX &&
878 L[1]->AM == AM65_IMM &&
879 L[2]->OPC == OP65_CLC &&
880 L[3]->OPC == OP65_ADC &&
881 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
882 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
884 L[4]->JumpTo->Owner == L[6] &&
885 L[5]->OPC == OP65_INX &&
886 CE_IsCallTo (L[6], "pushax") &&
887 L[7]->OPC == OP65_LDX &&
888 L[8]->OPC == OP65_LDA &&
889 L[9]->OPC == OP65_LDY &&
890 CE_IsKnownImm (L[9], 0) &&
891 CE_IsCallTo (L[10], "staspidx") &&
892 !CS_RangeHasLabel (S, I+1, 5) &&
893 !CS_RangeHasLabel (S, I+7, 4) &&
894 /* Check the label last because this is quite costly */
895 (Len = strlen (L[0]->Arg)) > 3 &&
896 L[0]->Arg[0] == '<' &&
897 L[0]->Arg[1] == '(' &&
898 strlen (L[1]->Arg) == Len &&
899 L[1]->Arg[0] == '>' &&
900 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
905 /* We will create all the new stuff behind the current one so
906 * we keep the line references.
908 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
909 CS_InsertEntry (S, X, I+11);
911 X = NewCodeEntry (OP65_LDX, L[7]->AM, L[7]->Arg, 0, L[7]->LI);
912 CS_InsertEntry (S, X, I+12);
914 X = NewCodeEntry (OP65_LDA, L[8]->AM, L[8]->Arg, 0, L[8]->LI);
915 CS_InsertEntry (S, X, I+13);
917 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
919 X = NewCodeEntry (OP65_STA, AM65_ABSY, Label, 0, L[10]->LI);
920 CS_InsertEntry (S, X, I+14);
923 /* Remove the old code */
924 CS_DelEntries (S, I, 11);
926 /* Remember, we had changes */
936 /* Return the number of changes made */
942 static unsigned OptPtrStore3 (CodeSeg* S)
943 /* Search for the sequence:
968 unsigned Changes = 0;
970 /* Walk over the entries */
972 while (I < CS_GetEntryCount (S)) {
978 L[0] = CS_GetEntry (S, I);
980 /* Check for the sequence */
981 if (L[0]->OPC == OP65_LDA &&
982 L[0]->AM == AM65_IMM &&
983 CS_GetEntries (S, L+1, I+1, 11) &&
984 L[1]->OPC == OP65_LDX &&
985 L[1]->AM == AM65_IMM &&
986 L[2]->OPC == OP65_LDY &&
987 L[3]->OPC == OP65_CLC &&
988 L[4]->OPC == OP65_ADC &&
989 L[4]->AM == AM65_ZP_INDY &&
990 strcmp (L[4]->Arg, "sp") == 0 &&
991 (L[5]->OPC == OP65_BCC || L[5]->OPC == OP65_JCC) &&
993 L[5]->JumpTo->Owner == L[7] &&
994 L[6]->OPC == OP65_INX &&
995 CE_IsCallTo (L[7], "pushax") &&
996 L[8]->OPC == OP65_LDX &&
997 L[9]->OPC == OP65_LDA &&
998 L[10]->OPC == OP65_LDY &&
999 CE_IsKnownImm (L[10], 0) &&
1000 CE_IsCallTo (L[11], "staspidx") &&
1001 !CS_RangeHasLabel (S, I+1, 6) &&
1002 !CS_RangeHasLabel (S, I+8, 4) &&
1003 /* Check the label last because this is quite costly */
1004 (Len = strlen (L[0]->Arg)) > 3 &&
1005 L[0]->Arg[0] == '<' &&
1006 L[0]->Arg[1] == '(' &&
1007 strlen (L[1]->Arg) == Len &&
1008 L[1]->Arg[0] == '>' &&
1009 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
1014 /* We will create all the new stuff behind the current one so
1015 * we keep the line references.
1017 X = NewCodeEntry (OP65_LDY, L[2]->AM, L[2]->Arg, 0, L[2]->LI);
1018 CS_InsertEntry (S, X, I+12);
1020 X = NewCodeEntry (OP65_LDX, L[8]->AM, L[8]->Arg, 0, L[8]->LI);
1021 CS_InsertEntry (S, X, I+13);
1023 X = NewCodeEntry (OP65_LDA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
1024 CS_InsertEntry (S, X, I+14);
1026 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[4]->LI);
1027 CS_InsertEntry (S, X, I+15);
1029 X = NewCodeEntry (OP65_LDA, L[9]->AM, L[9]->Arg, 0, L[9]->LI);
1030 CS_InsertEntry (S, X, I+16);
1032 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
1033 Label[Len-3] = '\0';
1034 X = NewCodeEntry (OP65_STA, AM65_ABSY, Label, 0, L[11]->LI);
1035 CS_InsertEntry (S, X, I+17);
1038 /* Remove the old code */
1039 CS_DelEntries (S, I, 12);
1041 /* Remember, we had changes */
1051 /* Return the number of changes made */
1057 /*****************************************************************************/
1058 /* Decouple operations */
1059 /*****************************************************************************/
1063 static unsigned OptDecouple (CodeSeg* S)
1064 /* Decouple operations, that is, do the following replacements:
1074 * lda zp -> lda #imm
1075 * ldx zp -> ldx #imm
1076 * ldy zp -> ldy #imm
1078 * Provided that the register values are known of course.
1081 unsigned Changes = 0;
1084 /* Generate register info for the following step */
1087 /* Walk over the entries */
1089 while (I < CS_GetEntryCount (S)) {
1093 /* Get next entry and it's input register values */
1094 CodeEntry* E = CS_GetEntry (S, I);
1095 const RegContents* In = &E->RI->In;
1097 /* Assume we have no replacement */
1100 /* Check the instruction */
1104 if (RegValIsKnown (In->RegA)) {
1105 Arg = MakeHexArg ((In->RegA - 1) & 0xFF);
1106 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1111 if (RegValIsKnown (In->RegX)) {
1112 Arg = MakeHexArg ((In->RegX - 1) & 0xFF);
1113 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1118 if (RegValIsKnown (In->RegY)) {
1119 Arg = MakeHexArg ((In->RegY - 1) & 0xFF);
1120 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1125 if (RegValIsKnown (In->RegA)) {
1126 Arg = MakeHexArg ((In->RegA + 1) & 0xFF);
1127 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1132 if (RegValIsKnown (In->RegX)) {
1133 Arg = MakeHexArg ((In->RegX + 1) & 0xFF);
1134 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1139 if (RegValIsKnown (In->RegY)) {
1140 Arg = MakeHexArg ((In->RegY + 1) & 0xFF);
1141 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1146 if (E->AM == AM65_ZP) {
1147 switch (GetKnownReg (E->Use & REG_ZP, In)) {
1149 Arg = MakeHexArg (In->Tmp1);
1150 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1154 Arg = MakeHexArg (In->Ptr1Lo);
1155 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1159 Arg = MakeHexArg (In->Ptr1Hi);
1160 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1164 Arg = MakeHexArg (In->SRegLo);
1165 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1169 Arg = MakeHexArg (In->SRegHi);
1170 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1177 if (E->AM == AM65_ZP) {
1178 switch (GetKnownReg (E->Use & REG_ZP, In)) {
1180 Arg = MakeHexArg (In->Tmp1);
1181 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1185 Arg = MakeHexArg (In->Ptr1Lo);
1186 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1190 Arg = MakeHexArg (In->Ptr1Hi);
1191 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1195 Arg = MakeHexArg (In->SRegLo);
1196 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1200 Arg = MakeHexArg (In->SRegHi);
1201 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1208 if (E->AM == AM65_ZP) {
1209 switch (GetKnownReg (E->Use, In)) {
1211 Arg = MakeHexArg (In->Tmp1);
1212 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1216 Arg = MakeHexArg (In->Ptr1Lo);
1217 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1221 Arg = MakeHexArg (In->Ptr1Hi);
1222 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1226 Arg = MakeHexArg (In->SRegLo);
1227 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1231 Arg = MakeHexArg (In->SRegHi);
1232 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1239 if (E->RI->In.RegA >= 0) {
1240 Arg = MakeHexArg (In->RegA);
1241 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1246 if (E->RI->In.RegA >= 0) {
1247 Arg = MakeHexArg (In->RegA);
1248 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1253 if (E->RI->In.RegX >= 0) {
1254 Arg = MakeHexArg (In->RegX);
1255 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1260 if (E->RI->In.RegY >= 0) {
1261 Arg = MakeHexArg (In->RegY);
1262 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1267 /* Avoid gcc warnings */
1272 /* Insert the replacement if we have one */
1274 CS_InsertEntry (S, X, I+1);
1284 /* Free register info */
1287 /* Return the number of changes made */
1293 /*****************************************************************************/
1294 /* Optimize stack pointer ops */
1295 /*****************************************************************************/
1299 static unsigned IsDecSP (const CodeEntry* E)
1300 /* Check if this is an insn that decrements the stack pointer. If so, return
1301 * the decrement. If not, return zero.
1302 * The function expects E to be a subroutine call.
1305 if (strncmp (E->Arg, "decsp", 5) == 0) {
1306 if (E->Arg[5] >= '1' && E->Arg[5] <= '8') {
1307 return (E->Arg[5] - '0');
1309 } else if (strcmp (E->Arg, "subysp") == 0 && RegValIsKnown (E->RI->In.RegY)) {
1310 return E->RI->In.RegY;
1313 /* If we come here, it's not a decsp op */
1319 static unsigned OptStackPtrOps (CodeSeg* S)
1320 /* Merge adjacent calls to decsp into one. NOTE: This function won't merge all
1324 unsigned Changes = 0;
1327 /* Generate register info for the following step */
1330 /* Walk over the entries */
1332 while (I < CS_GetEntryCount (S)) {
1338 /* Get the next entry */
1339 const CodeEntry* E = CS_GetEntry (S, I);
1341 /* Check for decspn or subysp */
1342 if (E->OPC == OP65_JSR &&
1343 (Dec1 = IsDecSP (E)) > 0 &&
1344 (N = CS_GetNextEntry (S, I)) != 0 &&
1345 (Dec2 = IsDecSP (N)) > 0 &&
1346 (Dec1 += Dec2) <= 255 &&
1352 /* We can combine the two */
1354 /* Insert a call to decsp */
1355 xsprintf (Buf, sizeof (Buf), "decsp%u", Dec1);
1356 X = NewCodeEntry (OP65_JSR, AM65_ABS, Buf, 0, N->LI);
1357 CS_InsertEntry (S, X, I+2);
1359 /* Insert a call to subysp */
1360 const char* Arg = MakeHexArg (Dec1);
1361 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, N->LI);
1362 CS_InsertEntry (S, X, I+2);
1363 X = NewCodeEntry (OP65_JSR, AM65_ABS, "subysp", 0, N->LI);
1364 CS_InsertEntry (S, X, I+3);
1367 /* Delete the old code */
1368 CS_DelEntries (S, I, 2);
1370 /* Regenerate register info */
1373 /* Remember we had changes */
1384 /* Free register info */
1387 /* Return the number of changes made */
1393 /*****************************************************************************/
1394 /* struct OptFunc */
1395 /*****************************************************************************/
1399 typedef struct OptFunc OptFunc;
1401 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1402 const char* Name; /* Name of the function/group */
1403 unsigned CodeSizeFactor; /* Code size factor for this opt func */
1404 unsigned long TotalRuns; /* Total number of runs */
1405 unsigned long LastRuns; /* Last number of runs */
1406 unsigned long TotalChanges; /* Total number of changes */
1407 unsigned long LastChanges; /* Last number of changes */
1408 char Disabled; /* True if function disabled */
1413 /*****************************************************************************/
1415 /*****************************************************************************/
1419 /* A list of all the function descriptions */
1420 static OptFunc DOpt65C02BitOps = { Opt65C02BitOps, "Opt65C02BitOps", 66, 0, 0, 0, 0, 0 };
1421 static OptFunc DOpt65C02Ind = { Opt65C02Ind, "Opt65C02Ind", 100, 0, 0, 0, 0, 0 };
1422 static OptFunc DOpt65C02Stores = { Opt65C02Stores, "Opt65C02Stores", 100, 0, 0, 0, 0, 0 };
1423 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 125, 0, 0, 0, 0, 0 };
1424 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 200, 0, 0, 0, 0, 0 };
1425 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 65, 0, 0, 0, 0, 0 };
1426 static OptFunc DOptAdd4 = { OptAdd4, "OptAdd4", 90, 0, 0, 0, 0, 0 };
1427 static OptFunc DOptAdd5 = { OptAdd5, "OptAdd5", 100, 0, 0, 0, 0, 0 };
1428 static OptFunc DOptAdd6 = { OptAdd6, "OptAdd6", 40, 0, 0, 0, 0, 0 };
1429 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 100, 0, 0, 0, 0, 0 };
1430 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0, 0 };
1431 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 42, 0, 0, 0, 0, 0 };
1432 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 85, 0, 0, 0, 0, 0 };
1433 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 75, 0, 0, 0, 0, 0 };
1434 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 75, 0, 0, 0, 0, 0 };
1435 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 100, 0, 0, 0, 0, 0 };
1436 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 100, 0, 0, 0, 0, 0 };
1437 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 85, 0, 0, 0, 0, 0 };
1438 static OptFunc DOptCmp8 = { OptCmp8, "OptCmp8", 50, 0, 0, 0, 0, 0 };
1439 static OptFunc DOptCmp9 = { OptCmp9, "OptCmp9", 85, 0, 0, 0, 0, 0 };
1440 static OptFunc DOptCondBranches1= { OptCondBranches1,"OptCondBranches1", 80, 0, 0, 0, 0, 0 };
1441 static OptFunc DOptCondBranches2= { OptCondBranches2,"OptCondBranches2", 0, 0, 0, 0, 0, 0 };
1442 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 100, 0, 0, 0, 0, 0 };
1443 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 100, 0, 0, 0, 0, 0 };
1444 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 100, 0, 0, 0, 0, 0 };
1445 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0, 0 };
1446 static OptFunc DOptIndLoads1 = { OptIndLoads1, "OptIndLoads1", 0, 0, 0, 0, 0, 0 };
1447 static OptFunc DOptIndLoads2 = { OptIndLoads2, "OptIndLoads2", 0, 0, 0, 0, 0, 0 };
1448 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 100, 0, 0, 0, 0, 0 };
1449 static OptFunc DOptJumpTarget1 = { OptJumpTarget1, "OptJumpTarget1", 100, 0, 0, 0, 0, 0 };
1450 static OptFunc DOptJumpTarget2 = { OptJumpTarget2, "OptJumpTarget2", 100, 0, 0, 0, 0, 0 };
1451 static OptFunc DOptJumpTarget3 = { OptJumpTarget3, "OptJumpTarget3", 100, 0, 0, 0, 0, 0 };
1452 static OptFunc DOptLoad1 = { OptLoad1, "OptLoad1", 100, 0, 0, 0, 0, 0 };
1453 static OptFunc DOptLoad2 = { OptLoad2, "OptLoad2", 200, 0, 0, 0, 0, 0 };
1454 static OptFunc DOptRTS = { OptRTS, "OptRTS", 100, 0, 0, 0, 0, 0 };
1455 static OptFunc DOptRTSJumps1 = { OptRTSJumps1, "OptRTSJumps1", 100, 0, 0, 0, 0, 0 };
1456 static OptFunc DOptRTSJumps2 = { OptRTSJumps2, "OptRTSJumps2", 100, 0, 0, 0, 0, 0 };
1457 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 100, 0, 0, 0, 0, 0 };
1458 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 100, 0, 0, 0, 0, 0 };
1459 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 100, 0, 0, 0, 0, 0 };
1460 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 100, 0, 0, 0, 0, 0 };
1461 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 100, 0, 0, 0, 0, 0 };
1462 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 100, 0, 0, 0, 0, 0 };
1463 static OptFunc DOptPrecalc = { OptPrecalc, "OptPrecalc", 100, 0, 0, 0, 0, 0 };
1464 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 100, 0, 0, 0, 0, 0 };
1465 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 };
1466 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 };
1467 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 };
1468 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 50, 0, 0, 0, 0, 0 };
1469 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 60, 0, 0, 0, 0, 0 };
1470 static OptFunc DOptPtrLoad7 = { OptPtrLoad7, "OptPtrLoad7", 140, 0, 0, 0, 0, 0 };
1471 static OptFunc DOptPtrLoad11 = { OptPtrLoad11, "OptPtrLoad11", 92, 0, 0, 0, 0, 0 };
1472 static OptFunc DOptPtrLoad12 = { OptPtrLoad12, "OptPtrLoad12", 50, 0, 0, 0, 0, 0 };
1473 static OptFunc DOptPtrLoad13 = { OptPtrLoad13, "OptPtrLoad13", 65, 0, 0, 0, 0, 0 };
1474 static OptFunc DOptPtrLoad14 = { OptPtrLoad14, "OptPtrLoad14", 108, 0, 0, 0, 0, 0 };
1475 static OptFunc DOptPtrLoad15 = { OptPtrLoad15, "OptPtrLoad15", 86, 0, 0, 0, 0, 0 };
1476 static OptFunc DOptPtrLoad16 = { OptPtrLoad16, "OptPtrLoad16", 100, 0, 0, 0, 0, 0 };
1477 static OptFunc DOptPtrLoad17 = { OptPtrLoad17, "OptPtrLoad17", 190, 0, 0, 0, 0, 0 };
1478 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 100, 0, 0, 0, 0, 0 };
1479 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 40, 0, 0, 0, 0, 0 };
1480 static OptFunc DOptPtrStore3 = { OptPtrStore3, "OptPtrStore3", 50, 0, 0, 0, 0, 0 };
1481 static OptFunc DOptPush1 = { OptPush1, "OptPush1", 65, 0, 0, 0, 0, 0 };
1482 static OptFunc DOptPush2 = { OptPush2, "OptPush2", 50, 0, 0, 0, 0, 0 };
1483 static OptFunc DOptPushPop = { OptPushPop, "OptPushPop", 0, 0, 0, 0, 0, 0 };
1484 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 100, 0, 0, 0, 0, 0 };
1485 static OptFunc DOptShift2 = { OptShift2, "OptShift2", 100, 0, 0, 0, 0, 0 };
1486 static OptFunc DOptShift3 = { OptShift3, "OptShift3", 100, 0, 0, 0, 0, 0 };
1487 static OptFunc DOptShift4 = { OptShift4, "OptShift4", 110, 0, 0, 0, 0, 0 };
1488 static OptFunc DOptShift5 = { OptShift5, "OptShift5", 200, 0, 0, 0, 0, 0 };
1489 static OptFunc DOptSize1 = { OptSize1, "OptSize1", 100, 0, 0, 0, 0, 0 };
1490 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 100, 0, 0, 0, 0, 0 };
1491 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 100, 0, 0, 0, 0, 0 };
1492 static OptFunc DOptStackPtrOps = { OptStackPtrOps, "OptStackPtrOps", 50, 0, 0, 0, 0, 0 };
1493 static OptFunc DOptStore1 = { OptStore1, "OptStore1", 70, 0, 0, 0, 0, 0 };
1494 static OptFunc DOptStore2 = { OptStore2, "OptStore2", 115, 0, 0, 0, 0, 0 };
1495 static OptFunc DOptStore3 = { OptStore3, "OptStore3", 120, 0, 0, 0, 0, 0 };
1496 static OptFunc DOptStore4 = { OptStore4, "OptStore4", 50, 0, 0, 0, 0, 0 };
1497 static OptFunc DOptStore5 = { OptStore5, "OptStore5", 100, 0, 0, 0, 0, 0 };
1498 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 };
1499 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 };
1500 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 };
1501 static OptFunc DOptSub3 = { OptSub3, "OptSub3", 100, 0, 0, 0, 0, 0 };
1502 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 65, 0, 0, 0, 0, 0 };
1503 static OptFunc DOptTest2 = { OptTest2, "OptTest2", 50, 0, 0, 0, 0, 0 };
1504 static OptFunc DOptTransfers1 = { OptTransfers1, "OptTransfers1", 0, 0, 0, 0, 0, 0 };
1505 static OptFunc DOptTransfers2 = { OptTransfers2, "OptTransfers2", 60, 0, 0, 0, 0, 0 };
1506 static OptFunc DOptTransfers3 = { OptTransfers3, "OptTransfers3", 65, 0, 0, 0, 0, 0 };
1507 static OptFunc DOptTransfers4 = { OptTransfers4, "OptTransfers4", 65, 0, 0, 0, 0, 0 };
1508 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0, 0 };
1509 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0, 0 };
1512 /* Table containing all the steps in alphabetical order */
1513 static OptFunc* OptFuncs[] = {
1605 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1609 static int CmpOptStep (const void* Key, const void* Func)
1610 /* Compare function for bsearch */
1612 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1617 static OptFunc* FindOptFunc (const char* Name)
1618 /* Find an optimizer step by name in the table and return a pointer. Return
1619 * NULL if no such step is found.
1622 /* Search for the function in the list */
1623 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1629 static OptFunc* GetOptFunc (const char* Name)
1630 /* Find an optimizer step by name in the table and return a pointer. Print an
1631 * error and call AbEnd if not found.
1634 /* Search for the function in the list */
1635 OptFunc* F = FindOptFunc (Name);
1638 AbEnd ("Optimization step `%s' not found", Name);
1645 void DisableOpt (const char* Name)
1646 /* Disable the optimization with the given name */
1648 if (strcmp (Name, "any") == 0) {
1650 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1651 OptFuncs[I]->Disabled = 1;
1654 GetOptFunc(Name)->Disabled = 1;
1660 void EnableOpt (const char* Name)
1661 /* Enable the optimization with the given name */
1663 if (strcmp (Name, "any") == 0) {
1665 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1666 OptFuncs[I]->Disabled = 0;
1669 GetOptFunc(Name)->Disabled = 0;
1675 void ListOptSteps (FILE* F)
1676 /* List all optimization steps */
1679 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1680 fprintf (F, "%s\n", OptFuncs[I]->Name);
1686 static void ReadOptStats (const char* Name)
1687 /* Read the optimizer statistics file */
1692 /* Try to open the file */
1693 FILE* F = fopen (Name, "r");
1695 /* Ignore the error */
1699 /* Read and parse the lines */
1701 while (fgets (Buf, sizeof (Buf), F) != 0) {
1709 unsigned long TotalRuns;
1710 unsigned long TotalChanges;
1715 /* Remove trailing white space including the line terminator */
1718 while (Len > 0 && IsSpace (B[Len-1])) {
1723 /* Remove leading whitespace */
1724 while (IsSpace (*B)) {
1728 /* Check for empty and comment lines */
1729 if (*B == '\0' || *B == ';' || *B == '#') {
1733 /* Parse the line */
1734 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1739 /* Search for the optimizer step. */
1740 Func = FindOptFunc (Name);
1746 /* Found the step, set the fields */
1747 Func->TotalRuns = TotalRuns;
1748 Func->TotalChanges = TotalChanges;
1752 /* Close the file, ignore errors here. */
1758 static void WriteOptStats (const char* Name)
1759 /* Write the optimizer statistics file */
1763 /* Try to open the file */
1764 FILE* F = fopen (Name, "w");
1766 /* Ignore the error */
1770 /* Write a header */
1772 "; Optimizer Total Last Total Last\n"
1773 "; Step Runs Runs Chg Chg\n");
1776 /* Write the data */
1777 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1778 const OptFunc* O = OptFuncs[I];
1780 "%-20s %10lu %10lu %10lu %10lu\n",
1788 /* Close the file, ignore errors here. */
1794 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1795 /* Run one optimizer function Max times or until there are no more changes */
1797 unsigned Changes, C;
1799 /* Don't run the function if it is disabled or if it is prohibited by the
1802 if (F->Disabled || F->CodeSizeFactor > S->CodeSizeFactor) {
1806 /* Run this until there are no more changes */
1810 /* Run the function */
1817 F->TotalChanges += C;
1818 F->LastChanges += C;
1820 } while (--Max && C > 0);
1822 /* Return the number of changes */
1828 static unsigned RunOptGroup1 (CodeSeg* S)
1829 /* Run the first group of optimization steps. These steps translate known
1830 * patterns emitted by the code generator into more optimal patterns. Order
1831 * of the steps is important, because some of the steps done earlier cover
1832 * the same patterns as later steps as subpatterns.
1835 unsigned Changes = 0;
1837 Changes += RunOptFunc (S, &DOptStackPtrOps, 5);
1838 Changes += RunOptFunc (S, &DOptPtrStore1, 1);
1839 Changes += RunOptFunc (S, &DOptPtrStore2, 1);
1840 Changes += RunOptFunc (S, &DOptPtrStore3, 1);
1841 Changes += RunOptFunc (S, &DOptAdd3, 1); /* Before OptPtrLoad5! */
1842 Changes += RunOptFunc (S, &DOptPtrLoad1, 1);
1843 Changes += RunOptFunc (S, &DOptPtrLoad2, 1);
1844 Changes += RunOptFunc (S, &DOptPtrLoad3, 1);
1845 Changes += RunOptFunc (S, &DOptPtrLoad4, 1);
1846 Changes += RunOptFunc (S, &DOptPtrLoad5, 1);
1847 Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
1848 Changes += RunOptFunc (S, &DOptPtrLoad7, 1);
1849 Changes += RunOptFunc (S, &DOptPtrLoad11, 1);
1850 Changes += RunOptFunc (S, &DOptPtrLoad12, 1);
1851 Changes += RunOptFunc (S, &DOptPtrLoad13, 1);
1852 Changes += RunOptFunc (S, &DOptPtrLoad14, 1);
1853 Changes += RunOptFunc (S, &DOptPtrLoad15, 1);
1854 Changes += RunOptFunc (S, &DOptPtrLoad16, 1);
1855 Changes += RunOptFunc (S, &DOptPtrLoad17, 1);
1856 Changes += RunOptFunc (S, &DOptNegAX1, 1);
1857 Changes += RunOptFunc (S, &DOptNegAX2, 1);
1858 Changes += RunOptFunc (S, &DOptNegAX3, 1);
1859 Changes += RunOptFunc (S, &DOptNegAX4, 1);
1860 Changes += RunOptFunc (S, &DOptAdd1, 1);
1861 Changes += RunOptFunc (S, &DOptAdd2, 1);
1862 Changes += RunOptFunc (S, &DOptAdd4, 1);
1863 Changes += RunOptFunc (S, &DOptAdd5, 1);
1864 Changes += RunOptFunc (S, &DOptAdd6, 1);
1865 Changes += RunOptFunc (S, &DOptAdd6, 1);
1866 Changes += RunOptFunc (S, &DOptSub1, 1);
1867 Changes += RunOptFunc (S, &DOptSub3, 1);
1868 Changes += RunOptFunc (S, &DOptStore4, 1);
1869 Changes += RunOptFunc (S, &DOptStore5, 1);
1870 Changes += RunOptFunc (S, &DOptShift1, 1);
1871 Changes += RunOptFunc (S, &DOptShift2, 1);
1872 Changes += RunOptFunc (S, &DOptShift3, 1);
1873 Changes += RunOptFunc (S, &DOptShift4, 1);
1874 Changes += RunOptFunc (S, &DOptStore1, 1);
1875 Changes += RunOptFunc (S, &DOptStore2, 5);
1876 Changes += RunOptFunc (S, &DOptStore3, 5);
1878 /* Return the number of changes */
1884 static unsigned RunOptGroup2 (CodeSeg* S)
1885 /* Run one group of optimization steps. This step involves just decoupling
1886 * instructions by replacing them by instructions that do not depend on
1887 * previous instructions. This makes it easier to find instructions that
1891 unsigned Changes = 0;
1893 Changes += RunOptFunc (S, &DOptDecouple, 1);
1895 /* Return the number of changes */
1901 static unsigned RunOptGroup3 (CodeSeg* S)
1902 /* Run one group of optimization steps. These steps depend on each other,
1903 * that means that one step may allow another step to do additional work,
1904 * so we will repeat the steps as long as we see any changes.
1907 unsigned Changes, C;
1913 C += RunOptFunc (S, &DOptNegA1, 1);
1914 C += RunOptFunc (S, &DOptNegA2, 1);
1915 C += RunOptFunc (S, &DOptStackOps, 1);
1916 C += RunOptFunc (S, &DOptSub1, 1);
1917 C += RunOptFunc (S, &DOptSub2, 1);
1918 C += RunOptFunc (S, &DOptSub3, 1);
1919 C += RunOptFunc (S, &DOptAdd5, 1);
1920 C += RunOptFunc (S, &DOptAdd6, 1);
1921 C += RunOptFunc (S, &DOptJumpCascades, 1);
1922 C += RunOptFunc (S, &DOptDeadJumps, 1);
1923 C += RunOptFunc (S, &DOptRTS, 1);
1924 C += RunOptFunc (S, &DOptDeadCode, 1);
1925 C += RunOptFunc (S, &DOptBoolTrans, 1);
1926 C += RunOptFunc (S, &DOptJumpTarget1, 1);
1927 C += RunOptFunc (S, &DOptJumpTarget2, 1);
1928 C += RunOptFunc (S, &DOptCondBranches1, 1);
1929 C += RunOptFunc (S, &DOptCondBranches2, 1);
1930 C += RunOptFunc (S, &DOptRTSJumps1, 1);
1931 C += RunOptFunc (S, &DOptCmp1, 1);
1932 C += RunOptFunc (S, &DOptCmp2, 1);
1933 C += RunOptFunc (S, &DOptCmp3, 1);
1934 C += RunOptFunc (S, &DOptCmp4, 1);
1935 C += RunOptFunc (S, &DOptCmp5, 1);
1936 C += RunOptFunc (S, &DOptCmp6, 1);
1937 C += RunOptFunc (S, &DOptCmp7, 1);
1938 C += RunOptFunc (S, &DOptCmp8, 1);
1939 C += RunOptFunc (S, &DOptCmp9, 1);
1940 C += RunOptFunc (S, &DOptTest1, 1);
1941 C += RunOptFunc (S, &DOptLoad1, 1);
1942 C += RunOptFunc (S, &DOptJumpTarget3, 1); /* After OptCondBranches2 */
1943 C += RunOptFunc (S, &DOptUnusedLoads, 1);
1944 C += RunOptFunc (S, &DOptUnusedStores, 1);
1945 C += RunOptFunc (S, &DOptDupLoads, 1);
1946 C += RunOptFunc (S, &DOptStoreLoad, 1);
1947 C += RunOptFunc (S, &DOptTransfers1, 1);
1948 C += RunOptFunc (S, &DOptTransfers3, 1);
1949 C += RunOptFunc (S, &DOptTransfers4, 1);
1950 C += RunOptFunc (S, &DOptStore1, 1);
1951 C += RunOptFunc (S, &DOptStore5, 1);
1952 C += RunOptFunc (S, &DOptPushPop, 1);
1953 C += RunOptFunc (S, &DOptPrecalc, 1);
1959 /* Return the number of changes */
1965 static unsigned RunOptGroup4 (CodeSeg* S)
1966 /* Run another round of pattern replacements. These are done late, since there
1967 * may be better replacements before.
1970 unsigned Changes = 0;
1972 /* Repeat some of the steps here */
1973 Changes += RunOptFunc (S, &DOptPush1, 1);
1974 Changes += RunOptFunc (S, &DOptPush2, 1);
1975 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1976 Changes += RunOptFunc (S, &DOptTest2, 1);
1977 Changes += RunOptFunc (S, &DOptTransfers2, 1);
1978 Changes += RunOptFunc (S, &DOptLoad2, 1);
1980 /* Return the number of changes */
1986 static unsigned RunOptGroup5 (CodeSeg* S)
1987 /* 65C02 specific optimizations. */
1989 unsigned Changes = 0;
1991 if (CPUIsets[CPU] & CPU_ISET_65SC02) {
1992 Changes += RunOptFunc (S, &DOpt65C02BitOps, 1);
1993 Changes += RunOptFunc (S, &DOpt65C02Ind, 1);
1994 Changes += RunOptFunc (S, &DOpt65C02Stores, 1);
1996 /* The 65C02 replacement codes do often make the use of a register
1997 * value unnecessary, so if we have changes, run another load
2000 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
2004 /* Return the number of changes */
2010 static unsigned RunOptGroup6 (CodeSeg* S)
2011 /* This one is quite special. It tries to replace "lda (sp),y" by "lda (sp,x)".
2012 * The latter is ony cycle slower, but if we're able to remove the necessary
2013 * load of the Y register, because X is zero anyway, we gain 1 cycle and
2014 * shorten the code by one (transfer) or two bytes (load). So what we do is
2015 * to replace the insns, remove unused loads, and then change back all insns
2016 * where Y is still zero (meaning that the load has not been removed).
2019 unsigned Changes = 0;
2021 /* This group will only run for a standard 6502, because the 65C02 has a
2022 * better addressing mode that covers this case.
2024 if ((CPUIsets[CPU] & CPU_ISET_65SC02) == 0) {
2025 Changes += RunOptFunc (S, &DOptIndLoads1, 1);
2026 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
2027 Changes += RunOptFunc (S, &DOptIndLoads2, 1);
2030 /* Return the number of changes */
2036 static unsigned RunOptGroup7 (CodeSeg* S)
2037 /* The last group of optimization steps. Adjust branches, do size optimizations.
2040 unsigned Changes = 0;
2043 /* Optimize for size, that is replace operations by shorter ones, even
2044 * if this does hinder further optimizations (no problem since we're
2047 C = RunOptFunc (S, &DOptSize1, 1);
2050 /* Run some optimization passes again, since the size optimizations
2051 * may have opened new oportunities.
2053 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
2054 Changes += RunOptFunc (S, &DOptUnusedStores, 1);
2055 Changes += RunOptFunc (S, &DOptJumpTarget1, 5);
2056 Changes += RunOptFunc (S, &DOptStore5, 1);
2059 C = RunOptFunc (S, &DOptSize2, 1);
2062 /* Run some optimization passes again, since the size optimizations
2063 * may have opened new oportunities.
2065 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
2066 Changes += RunOptFunc (S, &DOptJumpTarget1, 5);
2067 Changes += RunOptFunc (S, &DOptStore5, 1);
2068 Changes += RunOptFunc (S, &DOptTransfers3, 1);
2071 /* Adjust branch distances */
2072 Changes += RunOptFunc (S, &DOptBranchDist, 3);
2074 /* Replace conditional branches to RTS. If we had changes, we must run dead
2075 * code elimination again, since the change may have introduced dead code.
2077 C = RunOptFunc (S, &DOptRTSJumps2, 1);
2080 Changes += RunOptFunc (S, &DOptDeadCode, 1);
2083 /* Return the number of changes */
2089 void RunOpt (CodeSeg* S)
2090 /* Run the optimizer */
2092 const char* StatFileName;
2094 /* If we shouldn't run the optimizer, bail out */
2099 /* Check if we are requested to write optimizer statistics */
2100 StatFileName = getenv ("CC65_OPTSTATS");
2102 ReadOptStats (StatFileName);
2105 /* Print the name of the function we are working on */
2107 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
2109 Print (stdout, 1, "Running optimizer for global code segment\n");
2112 /* Run groups of optimizations */
2121 /* Write statistics */
2123 WriteOptStats (StatFileName);