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 shraxN routine may get replaced by one or more lsr insns
185 * if the value of X is zero.
188 unsigned Changes = 0;
191 /* Generate register info */
194 /* Walk over the entries */
196 while (I < CS_GetEntryCount (S)) {
199 CodeEntry* E = CS_GetEntry (S, I);
201 /* Check for the sequence */
202 if (E->OPC == OP65_JSR &&
203 strncmp (E->Arg, "shrax", 5) == 0 &&
204 strlen (E->Arg) == 6 &&
205 IsDigit (E->Arg[5]) &&
206 E->RI->In.RegX == 0) {
208 /* Insert shift insns */
209 unsigned Count = E->Arg[5] - '0';
211 CodeEntry* X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, E->LI);
212 CS_InsertEntry (S, X, I+1);
215 /* Delete the call to shrax */
218 /* Remember, we had changes */
228 /* Free the register info */
231 /* Return the number of changes made */
237 static unsigned GetShiftType (const char* Sub)
238 /* Helper function for OptShift3 */
241 if (strcmp (Sub+1, "slax1") == 0) {
243 } else if (strcmp (Sub+1, "srax1") == 0) {
246 } else if (*Sub == 's') {
247 if (strcmp (Sub+1, "hlax1") == 0) {
249 } else if (strcmp (Sub+1, "hrax1") == 0) {
258 static unsigned OptShift3 (CodeSeg* S)
259 /* Search for the sequence
263 * jsr aslax1/asrax1/shlax1/shrax1
276 * or similar, provided that a/x is not used later
279 unsigned Changes = 0;
281 /* Walk over the entries */
283 while (I < CS_GetEntryCount (S)) {
289 L[0] = CS_GetEntry (S, I);
291 /* Check for the sequence */
292 if (L[0]->OPC == OP65_LDA &&
293 (L[0]->AM == AM65_ABS || L[0]->AM == AM65_ZP) &&
294 CS_GetEntries (S, L+1, I+1, 4) &&
295 !CS_RangeHasLabel (S, I+1, 4) &&
296 L[1]->OPC == OP65_LDX &&
297 (L[1]->AM == AM65_ABS || L[1]->AM == AM65_ZP) &&
298 L[2]->OPC == OP65_JSR &&
299 (ShiftType = GetShiftType (L[2]->Arg)) != SHIFT_NONE&&
300 L[3]->OPC == OP65_STA &&
301 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
302 L[4]->OPC == OP65_STX &&
303 (L[4]->AM == AM65_ABS || L[4]->AM == AM65_ZP) &&
304 !RegAXUsed (S, I+5)) {
308 /* Handle the four shift types differently */
312 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
313 CS_InsertEntry (S, X, I+5);
314 X = NewCodeEntry (OP65_CMP, AM65_IMM, "$80", 0, L[2]->LI);
315 CS_InsertEntry (S, X, I+6);
316 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
317 CS_InsertEntry (S, X, I+7);
318 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
319 CS_InsertEntry (S, X, I+8);
320 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
321 CS_InsertEntry (S, X, I+9);
322 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
323 CS_InsertEntry (S, X, I+10);
324 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
325 CS_InsertEntry (S, X, I+11);
326 CS_DelEntries (S, I, 5);
330 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
331 CS_InsertEntry (S, X, I+5);
332 X = NewCodeEntry (OP65_LSR, AM65_ACC, "a", 0, L[2]->LI);
333 CS_InsertEntry (S, X, I+6);
334 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
335 CS_InsertEntry (S, X, I+7);
336 X = NewCodeEntry (OP65_LDA, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
337 CS_InsertEntry (S, X, I+8);
338 X = NewCodeEntry (OP65_ROR, AM65_ACC, "a", 0, L[2]->LI);
339 CS_InsertEntry (S, X, I+9);
340 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
341 CS_InsertEntry (S, X, I+10);
342 CS_DelEntries (S, I, 5);
347 /* These two are identical */
348 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, L[2]->LI);
349 CS_InsertEntry (S, X, I+1);
350 X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
351 CS_InsertEntry (S, X, I+2);
352 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
353 CS_InsertEntry (S, X, I+3);
354 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, L[2]->LI);
355 CS_InsertEntry (S, X, I+4);
356 X = NewCodeEntry (OP65_STA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
357 CS_InsertEntry (S, X, I+5);
358 CS_DelEntries (S, I+6, 4);
363 /* Remember, we had changes */
373 /* Return the number of changes made */
379 static unsigned OptShift4 (CodeSeg* S)
380 /* Inline the shift subroutines. */
382 unsigned Changes = 0;
384 /* Walk over the entries */
386 while (I < CS_GetEntryCount (S)) {
392 CodeEntry* E = CS_GetEntry (S, I);
394 /* Check for a call to one of the shift routine */
395 if (E->OPC == OP65_JSR &&
396 (strncmp (E->Arg, "shlax", 5) == 0 ||
397 strncmp (E->Arg, "aslax", 5) == 0) &&
398 strlen (E->Arg) == 6 &&
399 IsDigit (E->Arg[5])) {
401 /* Get number of shifts */
402 unsigned ShiftCount = (E->Arg[5] - '0');
409 * (repeat ShiftCount-1 times)
412 * which makes 4 + 3 * ShiftCount bytes, compared to the original
413 * 3 bytes for the subroutine call. However, in most cases, the
414 * final load of the X register gets merged with some other insn
415 * and replaces a txa, so for a shift count of 1, we get a factor
416 * of 200, which matches nicely the CodeSizeFactor enabled with -Oi
418 if (ShiftCount > 1 || S->CodeSizeFactor > 200) {
419 unsigned Size = 4 + 3 * ShiftCount;
420 if ((Size * 100 / 3) > S->CodeSizeFactor) {
426 /* Inline the code. Insertion point is behind the subroutine call */
430 X = NewCodeEntry (OP65_STX, AM65_ZP, "tmp1", 0, E->LI);
431 CS_InsertEntry (S, X, IP++);
433 while (ShiftCount--) {
435 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, E->LI);
436 CS_InsertEntry (S, X, IP++);
439 X = NewCodeEntry (OP65_ROL, AM65_ZP, "tmp1", 0, E->LI);
440 CS_InsertEntry (S, X, IP++);
444 X = NewCodeEntry (OP65_LDX, AM65_ZP, "tmp1", 0, E->LI);
445 CS_InsertEntry (S, X, IP++);
447 /* Remove the subroutine call */
450 /* Remember, we had changes */
460 /* Return the number of changes made */
466 /*****************************************************************************/
468 /*****************************************************************************/
472 static unsigned OptLoad1 (CodeSeg* S)
473 /* Search for a call to ldaxysp where X is not used later and replace it by
474 * a load of just the A register.
478 unsigned Changes = 0;
480 /* Generate register info */
483 /* Walk over the entries */
485 while (I < CS_GetEntryCount (S)) {
490 E = CS_GetEntry (S, I);
492 /* Check for the sequence */
493 if (CE_IsCallTo (E, "ldaxysp") &&
494 RegValIsKnown (E->RI->In.RegY) &&
495 !RegXUsed (S, I+1)) {
499 /* Reload the Y register */
500 const char* Arg = MakeHexArg (E->RI->In.RegY - 1);
501 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
502 CS_InsertEntry (S, X, I+1);
504 /* Load from stack */
505 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, E->LI);
506 CS_InsertEntry (S, X, I+2);
508 /* Now remove the call to the subroutine */
511 /* Remember, we had changes */
521 /* Free the register info */
524 /* Return the number of changes made */
530 static unsigned OptLoad2 (CodeSeg* S)
531 /* Replace calls to ldaxysp by inline code */
534 unsigned Changes = 0;
536 /* Walk over the entries */
538 while (I < CS_GetEntryCount (S)) {
543 E = CS_GetEntry (S, I);
545 /* Check for the sequence */
546 if (CE_IsCallTo (E, "ldaxysp")) {
551 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, E->LI);
552 CS_InsertEntry (S, X, I+1);
555 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, E->LI);
556 CS_InsertEntry (S, X, I+2);
559 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, E->LI);
560 CS_InsertEntry (S, X, I+3);
563 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, E->LI);
564 CS_InsertEntry (S, X, I+4);
566 /* Now remove the call to the subroutine */
569 /* Remember, we had changes */
578 /* Return the number of changes made */
584 /*****************************************************************************/
585 /* Optimize stores through pointers */
586 /*****************************************************************************/
590 static unsigned OptPtrStore1Sub (CodeSeg* S, unsigned I, CodeEntry** const L)
591 /* Check if this is one of the allowed suboperation for OptPtrStore1 */
593 /* Check for a label attached to the entry */
594 if (CE_HasLabel (L[0])) {
598 /* Check for single insn sub ops */
599 if (L[0]->OPC == OP65_AND ||
600 L[0]->OPC == OP65_EOR ||
601 L[0]->OPC == OP65_ORA ||
602 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shlax", 5) == 0) ||
603 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shrax", 5) == 0)) {
608 } else if (L[0]->OPC == OP65_CLC &&
609 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
610 L[1]->OPC == OP65_ADC &&
611 !CE_HasLabel (L[1])) {
613 } else if (L[0]->OPC == OP65_SEC &&
614 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
615 L[1]->OPC == OP65_SBC &&
616 !CE_HasLabel (L[1])) {
628 static unsigned OptPtrStore1 (CodeSeg* S)
629 /* Search for the sequence:
649 * In case a/x is loaded from the register bank before the pushax, we can even
650 * use the register bank instead of ptr1.
664 unsigned Changes = 0;
666 /* Walk over the entries */
668 while (I < CS_GetEntryCount (S)) {
674 L[0] = CS_GetEntry (S, I);
676 /* Check for the sequence */
677 if (CE_IsCallTo (L[0], "pushax") &&
678 CS_GetEntries (S, L+1, I+1, 3) &&
679 L[1]->OPC == OP65_LDY &&
680 CE_IsConstImm (L[1]) &&
681 !CE_HasLabel (L[1]) &&
682 CE_IsCallTo (L[2], "ldauidx") &&
683 !CE_HasLabel (L[2]) &&
684 (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
685 CS_GetEntries (S, L+3+K, I+3+K, 2) &&
686 L[3+K]->OPC == OP65_LDY &&
687 CE_IsConstImm (L[3+K]) &&
688 !CE_HasLabel (L[3+K]) &&
689 CE_IsCallTo (L[4+K], "staspidx") &&
690 !CE_HasLabel (L[4+K])) {
693 const char* RegBank = 0;
694 const char* ZPLoc = "ptr1";
698 /* Get the preceeding two instructions and check them. We check
705 P[0] = CS_GetEntry (S, I-2);
706 P[1] = CS_GetEntry (S, I-1);
707 if (P[0]->OPC == OP65_LDA &&
708 P[0]->AM == AM65_ZP &&
709 P[1]->OPC == OP65_LDX &&
710 P[1]->AM == AM65_ZP &&
711 !CE_HasLabel (P[1]) &&
712 strncmp (P[0]->Arg, "regbank+", 8) == 0) {
714 unsigned Len = strlen (P[0]->Arg);
716 if (strncmp (P[0]->Arg, P[1]->Arg, Len) == 0 &&
717 P[1]->Arg[Len+0] == '+' &&
718 P[1]->Arg[Len+1] == '1' &&
719 P[1]->Arg[Len+2] == '\0') {
721 /* Ok, found. Use the name of the register bank */
722 RegBank = ZPLoc = P[0]->Arg;
727 /* Insert the load via the zp pointer */
728 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
729 CS_InsertEntry (S, X, I+3);
730 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, ZPLoc, 0, L[2]->LI);
731 CS_InsertEntry (S, X, I+4);
733 /* Insert the store through the zp pointer */
734 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, ZPLoc, 0, L[3]->LI);
735 CS_InsertEntry (S, X, I+6+K);
737 /* Delete the old code */
738 CS_DelEntry (S, I+7+K); /* jsr spaspidx */
739 CS_DelEntry (S, I+2); /* jsr ldauidx */
741 /* Create and insert the stores into the zp pointer if needed */
743 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
744 CS_InsertEntry (S, X, I+1);
745 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
746 CS_InsertEntry (S, X, I+2);
749 /* Delete more old code. Do it here to keep a label attached to
752 CS_DelEntry (S, I); /* jsr pushax */
754 /* Remember, we had changes */
764 /* Return the number of changes made */
770 static unsigned OptPtrStore2 (CodeSeg* S)
771 /* Search for the sequence:
793 unsigned Changes = 0;
795 /* Walk over the entries */
797 while (I < CS_GetEntryCount (S)) {
803 L[0] = CS_GetEntry (S, I);
805 /* Check for the sequence */
806 if (L[0]->OPC == OP65_LDA &&
807 L[0]->AM == AM65_IMM &&
808 CS_GetEntries (S, L+1, I+1, 10) &&
809 L[1]->OPC == OP65_LDX &&
810 L[1]->AM == AM65_IMM &&
811 L[2]->OPC == OP65_CLC &&
812 L[3]->OPC == OP65_ADC &&
813 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
814 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
816 L[4]->JumpTo->Owner == L[6] &&
817 L[5]->OPC == OP65_INX &&
818 CE_IsCallTo (L[6], "pushax") &&
819 L[7]->OPC == OP65_LDX &&
820 L[8]->OPC == OP65_LDA &&
821 L[9]->OPC == OP65_LDY &&
822 CE_IsKnownImm (L[9], 0) &&
823 CE_IsCallTo (L[10], "staspidx") &&
824 !CS_RangeHasLabel (S, I+1, 5) &&
825 !CS_RangeHasLabel (S, I+7, 4) &&
826 /* Check the label last because this is quite costly */
827 (Len = strlen (L[0]->Arg)) > 3 &&
828 L[0]->Arg[0] == '<' &&
829 L[0]->Arg[1] == '(' &&
830 strlen (L[1]->Arg) == Len &&
831 L[1]->Arg[0] == '>' &&
832 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
837 /* We will create all the new stuff behind the current one so
838 * we keep the line references.
840 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
841 CS_InsertEntry (S, X, I+11);
843 X = NewCodeEntry (OP65_LDX, L[7]->AM, L[7]->Arg, 0, L[7]->LI);
844 CS_InsertEntry (S, X, I+12);
846 X = NewCodeEntry (OP65_LDA, L[8]->AM, L[8]->Arg, 0, L[8]->LI);
847 CS_InsertEntry (S, X, I+13);
849 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
851 X = NewCodeEntry (OP65_STA, AM65_ABSY, Label, 0, L[10]->LI);
852 CS_InsertEntry (S, X, I+14);
855 /* Remove the old code */
856 CS_DelEntries (S, I, 11);
858 /* Remember, we had changes */
868 /* Return the number of changes made */
874 static unsigned OptPtrStore3 (CodeSeg* S)
875 /* Search for the sequence:
900 unsigned Changes = 0;
902 /* Walk over the entries */
904 while (I < CS_GetEntryCount (S)) {
910 L[0] = CS_GetEntry (S, I);
912 /* Check for the sequence */
913 if (L[0]->OPC == OP65_LDA &&
914 L[0]->AM == AM65_IMM &&
915 CS_GetEntries (S, L+1, I+1, 11) &&
916 L[1]->OPC == OP65_LDX &&
917 L[1]->AM == AM65_IMM &&
918 L[2]->OPC == OP65_LDY &&
919 L[3]->OPC == OP65_CLC &&
920 L[4]->OPC == OP65_ADC &&
921 L[4]->AM == AM65_ZP_INDY &&
922 strcmp (L[4]->Arg, "sp") == 0 &&
923 (L[5]->OPC == OP65_BCC || L[5]->OPC == OP65_JCC) &&
925 L[5]->JumpTo->Owner == L[7] &&
926 L[6]->OPC == OP65_INX &&
927 CE_IsCallTo (L[7], "pushax") &&
928 L[8]->OPC == OP65_LDX &&
929 L[9]->OPC == OP65_LDA &&
930 L[10]->OPC == OP65_LDY &&
931 CE_IsKnownImm (L[10], 0) &&
932 CE_IsCallTo (L[11], "staspidx") &&
933 !CS_RangeHasLabel (S, I+1, 6) &&
934 !CS_RangeHasLabel (S, I+8, 4) &&
935 /* Check the label last because this is quite costly */
936 (Len = strlen (L[0]->Arg)) > 3 &&
937 L[0]->Arg[0] == '<' &&
938 L[0]->Arg[1] == '(' &&
939 strlen (L[1]->Arg) == Len &&
940 L[1]->Arg[0] == '>' &&
941 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
946 /* We will create all the new stuff behind the current one so
947 * we keep the line references.
949 X = NewCodeEntry (OP65_LDY, L[2]->AM, L[2]->Arg, 0, L[2]->LI);
950 CS_InsertEntry (S, X, I+12);
952 X = NewCodeEntry (OP65_LDX, L[8]->AM, L[8]->Arg, 0, L[8]->LI);
953 CS_InsertEntry (S, X, I+13);
955 X = NewCodeEntry (OP65_LDA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
956 CS_InsertEntry (S, X, I+14);
958 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[4]->LI);
959 CS_InsertEntry (S, X, I+15);
961 X = NewCodeEntry (OP65_LDA, L[9]->AM, L[9]->Arg, 0, L[9]->LI);
962 CS_InsertEntry (S, X, I+16);
964 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
966 X = NewCodeEntry (OP65_STA, AM65_ABSY, Label, 0, L[11]->LI);
967 CS_InsertEntry (S, X, I+17);
970 /* Remove the old code */
971 CS_DelEntries (S, I, 12);
973 /* Remember, we had changes */
983 /* Return the number of changes made */
989 /*****************************************************************************/
990 /* Decouple operations */
991 /*****************************************************************************/
995 static unsigned OptDecouple (CodeSeg* S)
996 /* Decouple operations, that is, do the following replacements:
1006 * lda zp -> lda #imm
1007 * ldx zp -> ldx #imm
1008 * ldy zp -> ldy #imm
1010 * Provided that the register values are known of course.
1013 unsigned Changes = 0;
1016 /* Generate register info for the following step */
1019 /* Walk over the entries */
1021 while (I < CS_GetEntryCount (S)) {
1025 /* Get next entry and it's input register values */
1026 CodeEntry* E = CS_GetEntry (S, I);
1027 const RegContents* In = &E->RI->In;
1029 /* Assume we have no replacement */
1032 /* Check the instruction */
1036 if (RegValIsKnown (In->RegA)) {
1037 Arg = MakeHexArg ((In->RegA - 1) & 0xFF);
1038 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1043 if (RegValIsKnown (In->RegX)) {
1044 Arg = MakeHexArg ((In->RegX - 1) & 0xFF);
1045 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1050 if (RegValIsKnown (In->RegY)) {
1051 Arg = MakeHexArg ((In->RegY - 1) & 0xFF);
1052 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1057 if (RegValIsKnown (In->RegA)) {
1058 Arg = MakeHexArg ((In->RegA + 1) & 0xFF);
1059 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1064 if (RegValIsKnown (In->RegX)) {
1065 Arg = MakeHexArg ((In->RegX + 1) & 0xFF);
1066 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1071 if (RegValIsKnown (In->RegY)) {
1072 Arg = MakeHexArg ((In->RegY + 1) & 0xFF);
1073 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1078 if (E->AM == AM65_ZP) {
1079 switch (GetKnownReg (E->Use & REG_ZP, In)) {
1081 Arg = MakeHexArg (In->Tmp1);
1082 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1086 Arg = MakeHexArg (In->Ptr1Lo);
1087 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1091 Arg = MakeHexArg (In->Ptr1Hi);
1092 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1096 Arg = MakeHexArg (In->SRegLo);
1097 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1101 Arg = MakeHexArg (In->SRegHi);
1102 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1109 if (E->AM == AM65_ZP) {
1110 switch (GetKnownReg (E->Use & REG_ZP, In)) {
1112 Arg = MakeHexArg (In->Tmp1);
1113 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1117 Arg = MakeHexArg (In->Ptr1Lo);
1118 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1122 Arg = MakeHexArg (In->Ptr1Hi);
1123 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1127 Arg = MakeHexArg (In->SRegLo);
1128 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1132 Arg = MakeHexArg (In->SRegHi);
1133 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1140 if (E->AM == AM65_ZP) {
1141 switch (GetKnownReg (E->Use, In)) {
1143 Arg = MakeHexArg (In->Tmp1);
1144 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1148 Arg = MakeHexArg (In->Ptr1Lo);
1149 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1153 Arg = MakeHexArg (In->Ptr1Hi);
1154 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1158 Arg = MakeHexArg (In->SRegLo);
1159 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1163 Arg = MakeHexArg (In->SRegHi);
1164 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1171 if (E->RI->In.RegA >= 0) {
1172 Arg = MakeHexArg (In->RegA);
1173 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1178 if (E->RI->In.RegA >= 0) {
1179 Arg = MakeHexArg (In->RegA);
1180 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1185 if (E->RI->In.RegX >= 0) {
1186 Arg = MakeHexArg (In->RegX);
1187 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1192 if (E->RI->In.RegY >= 0) {
1193 Arg = MakeHexArg (In->RegY);
1194 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1199 /* Avoid gcc warnings */
1204 /* Insert the replacement if we have one */
1206 CS_InsertEntry (S, X, I+1);
1216 /* Free register info */
1219 /* Return the number of changes made */
1225 /*****************************************************************************/
1226 /* Optimize stack pointer ops */
1227 /*****************************************************************************/
1231 static unsigned IsDecSP (const CodeEntry* E)
1232 /* Check if this is an insn that decrements the stack pointer. If so, return
1233 * the decrement. If not, return zero.
1234 * The function expects E to be a subroutine call.
1237 if (strncmp (E->Arg, "decsp", 5) == 0) {
1238 if (E->Arg[5] >= '1' && E->Arg[5] <= '8') {
1239 return (E->Arg[5] - '0');
1241 } else if (strcmp (E->Arg, "subysp") == 0 && RegValIsKnown (E->RI->In.RegY)) {
1242 return E->RI->In.RegY;
1245 /* If we come here, it's not a decsp op */
1251 static unsigned OptStackPtrOps (CodeSeg* S)
1252 /* Merge adjacent calls to decsp into one. NOTE: This function won't merge all
1256 unsigned Changes = 0;
1259 /* Generate register info for the following step */
1262 /* Walk over the entries */
1264 while (I < CS_GetEntryCount (S)) {
1270 /* Get the next entry */
1271 const CodeEntry* E = CS_GetEntry (S, I);
1273 /* Check for decspn or subysp */
1274 if (E->OPC == OP65_JSR &&
1275 (Dec1 = IsDecSP (E)) > 0 &&
1276 (N = CS_GetNextEntry (S, I)) != 0 &&
1277 (Dec2 = IsDecSP (N)) > 0 &&
1278 (Dec1 += Dec2) <= 255 &&
1284 /* We can combine the two */
1286 /* Insert a call to decsp */
1287 xsprintf (Buf, sizeof (Buf), "decsp%u", Dec1);
1288 X = NewCodeEntry (OP65_JSR, AM65_ABS, Buf, 0, N->LI);
1289 CS_InsertEntry (S, X, I+2);
1291 /* Insert a call to subysp */
1292 const char* Arg = MakeHexArg (Dec1);
1293 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, N->LI);
1294 CS_InsertEntry (S, X, I+2);
1295 X = NewCodeEntry (OP65_JSR, AM65_ABS, "subysp", 0, N->LI);
1296 CS_InsertEntry (S, X, I+3);
1299 /* Delete the old code */
1300 CS_DelEntries (S, I, 2);
1302 /* Regenerate register info */
1305 /* Remember we had changes */
1316 /* Free register info */
1319 /* Return the number of changes made */
1325 /*****************************************************************************/
1326 /* struct OptFunc */
1327 /*****************************************************************************/
1331 typedef struct OptFunc OptFunc;
1333 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1334 const char* Name; /* Name of the function/group */
1335 unsigned CodeSizeFactor; /* Code size factor for this opt func */
1336 unsigned long TotalRuns; /* Total number of runs */
1337 unsigned long LastRuns; /* Last number of runs */
1338 unsigned long TotalChanges; /* Total number of changes */
1339 unsigned long LastChanges; /* Last number of changes */
1340 char Disabled; /* True if function disabled */
1345 /*****************************************************************************/
1347 /*****************************************************************************/
1351 /* A list of all the function descriptions */
1352 static OptFunc DOpt65C02BitOps = { Opt65C02BitOps, "Opt65C02BitOps", 66, 0, 0, 0, 0, 0 };
1353 static OptFunc DOpt65C02Ind = { Opt65C02Ind, "Opt65C02Ind", 100, 0, 0, 0, 0, 0 };
1354 static OptFunc DOpt65C02Stores = { Opt65C02Stores, "Opt65C02Stores", 100, 0, 0, 0, 0, 0 };
1355 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 125, 0, 0, 0, 0, 0 };
1356 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 200, 0, 0, 0, 0, 0 };
1357 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 65, 0, 0, 0, 0, 0 };
1358 static OptFunc DOptAdd4 = { OptAdd4, "OptAdd4", 90, 0, 0, 0, 0, 0 };
1359 static OptFunc DOptAdd5 = { OptAdd5, "OptAdd5", 100, 0, 0, 0, 0, 0 };
1360 static OptFunc DOptAdd6 = { OptAdd6, "OptAdd6", 40, 0, 0, 0, 0, 0 };
1361 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 100, 0, 0, 0, 0, 0 };
1362 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0, 0 };
1363 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 42, 0, 0, 0, 0, 0 };
1364 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 85, 0, 0, 0, 0, 0 };
1365 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 75, 0, 0, 0, 0, 0 };
1366 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 75, 0, 0, 0, 0, 0 };
1367 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 100, 0, 0, 0, 0, 0 };
1368 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 100, 0, 0, 0, 0, 0 };
1369 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 85, 0, 0, 0, 0, 0 };
1370 static OptFunc DOptCmp8 = { OptCmp8, "OptCmp8", 50, 0, 0, 0, 0, 0 };
1371 static OptFunc DOptCmp9 = { OptCmp9, "OptCmp9", 85, 0, 0, 0, 0, 0 };
1372 static OptFunc DOptCondBranches1= { OptCondBranches1,"OptCondBranches1", 80, 0, 0, 0, 0, 0 };
1373 static OptFunc DOptCondBranches2= { OptCondBranches2,"OptCondBranches2", 0, 0, 0, 0, 0, 0 };
1374 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 100, 0, 0, 0, 0, 0 };
1375 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 100, 0, 0, 0, 0, 0 };
1376 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 100, 0, 0, 0, 0, 0 };
1377 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0, 0 };
1378 static OptFunc DOptIndLoads1 = { OptIndLoads1, "OptIndLoads1", 0, 0, 0, 0, 0, 0 };
1379 static OptFunc DOptIndLoads2 = { OptIndLoads2, "OptIndLoads2", 0, 0, 0, 0, 0, 0 };
1380 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 100, 0, 0, 0, 0, 0 };
1381 static OptFunc DOptJumpTarget1 = { OptJumpTarget1, "OptJumpTarget1", 100, 0, 0, 0, 0, 0 };
1382 static OptFunc DOptJumpTarget2 = { OptJumpTarget2, "OptJumpTarget2", 100, 0, 0, 0, 0, 0 };
1383 static OptFunc DOptJumpTarget3 = { OptJumpTarget3, "OptJumpTarget3", 100, 0, 0, 0, 0, 0 };
1384 static OptFunc DOptLoad1 = { OptLoad1, "OptLoad1", 100, 0, 0, 0, 0, 0 };
1385 static OptFunc DOptLoad2 = { OptLoad2, "OptLoad2", 200, 0, 0, 0, 0, 0 };
1386 static OptFunc DOptRTS = { OptRTS, "OptRTS", 100, 0, 0, 0, 0, 0 };
1387 static OptFunc DOptRTSJumps1 = { OptRTSJumps1, "OptRTSJumps1", 100, 0, 0, 0, 0, 0 };
1388 static OptFunc DOptRTSJumps2 = { OptRTSJumps2, "OptRTSJumps2", 100, 0, 0, 0, 0, 0 };
1389 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 100, 0, 0, 0, 0, 0 };
1390 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 100, 0, 0, 0, 0, 0 };
1391 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 100, 0, 0, 0, 0, 0 };
1392 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 100, 0, 0, 0, 0, 0 };
1393 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 100, 0, 0, 0, 0, 0 };
1394 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 100, 0, 0, 0, 0, 0 };
1395 static OptFunc DOptPrecalc = { OptPrecalc, "OptPrecalc", 100, 0, 0, 0, 0, 0 };
1396 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 100, 0, 0, 0, 0, 0 };
1397 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 };
1398 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 };
1399 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 };
1400 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 50, 0, 0, 0, 0, 0 };
1401 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 60, 0, 0, 0, 0, 0 };
1402 static OptFunc DOptPtrLoad7 = { OptPtrLoad7, "OptPtrLoad7", 140, 0, 0, 0, 0, 0 };
1403 static OptFunc DOptPtrLoad11 = { OptPtrLoad11, "OptPtrLoad11", 92, 0, 0, 0, 0, 0 };
1404 static OptFunc DOptPtrLoad12 = { OptPtrLoad12, "OptPtrLoad12", 50, 0, 0, 0, 0, 0 };
1405 static OptFunc DOptPtrLoad13 = { OptPtrLoad13, "OptPtrLoad13", 65, 0, 0, 0, 0, 0 };
1406 static OptFunc DOptPtrLoad14 = { OptPtrLoad14, "OptPtrLoad14", 108, 0, 0, 0, 0, 0 };
1407 static OptFunc DOptPtrLoad15 = { OptPtrLoad15, "OptPtrLoad15", 86, 0, 0, 0, 0, 0 };
1408 static OptFunc DOptPtrLoad16 = { OptPtrLoad16, "OptPtrLoad16", 100, 0, 0, 0, 0, 0 };
1409 static OptFunc DOptPtrLoad17 = { OptPtrLoad17, "OptPtrLoad17", 190, 0, 0, 0, 0, 0 };
1410 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 100, 0, 0, 0, 0, 0 };
1411 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 40, 0, 0, 0, 0, 0 };
1412 static OptFunc DOptPtrStore3 = { OptPtrStore3, "OptPtrStore3", 50, 0, 0, 0, 0, 0 };
1413 static OptFunc DOptPush1 = { OptPush1, "OptPush1", 65, 0, 0, 0, 0, 0 };
1414 static OptFunc DOptPush2 = { OptPush2, "OptPush2", 50, 0, 0, 0, 0, 0 };
1415 static OptFunc DOptPushPop = { OptPushPop, "OptPushPop", 0, 0, 0, 0, 0, 0 };
1416 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 100, 0, 0, 0, 0, 0 };
1417 static OptFunc DOptShift2 = { OptShift2, "OptShift2", 100, 0, 0, 0, 0, 0 };
1418 static OptFunc DOptShift3 = { OptShift3, "OptShift3", 110, 0, 0, 0, 0, 0 };
1419 static OptFunc DOptShift4 = { OptShift4, "OptShift4", 200, 0, 0, 0, 0, 0 };
1420 static OptFunc DOptSize1 = { OptSize1, "OptSize1", 100, 0, 0, 0, 0, 0 };
1421 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 100, 0, 0, 0, 0, 0 };
1422 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 100, 0, 0, 0, 0, 0 };
1423 static OptFunc DOptStackPtrOps = { OptStackPtrOps, "OptStackPtrOps", 50, 0, 0, 0, 0, 0 };
1424 static OptFunc DOptStore1 = { OptStore1, "OptStore1", 70, 0, 0, 0, 0, 0 };
1425 static OptFunc DOptStore2 = { OptStore2, "OptStore2", 115, 0, 0, 0, 0, 0 };
1426 static OptFunc DOptStore3 = { OptStore3, "OptStore3", 120, 0, 0, 0, 0, 0 };
1427 static OptFunc DOptStore4 = { OptStore4, "OptStore4", 50, 0, 0, 0, 0, 0 };
1428 static OptFunc DOptStore5 = { OptStore5, "OptStore5", 100, 0, 0, 0, 0, 0 };
1429 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 };
1430 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 };
1431 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 };
1432 static OptFunc DOptSub3 = { OptSub3, "OptSub3", 100, 0, 0, 0, 0, 0 };
1433 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 65, 0, 0, 0, 0, 0 };
1434 static OptFunc DOptTest2 = { OptTest2, "OptTest2", 50, 0, 0, 0, 0, 0 };
1435 static OptFunc DOptTransfers1 = { OptTransfers1, "OptTransfers1", 0, 0, 0, 0, 0, 0 };
1436 static OptFunc DOptTransfers2 = { OptTransfers2, "OptTransfers2", 60, 0, 0, 0, 0, 0 };
1437 static OptFunc DOptTransfers3 = { OptTransfers3, "OptTransfers3", 65, 0, 0, 0, 0, 0 };
1438 static OptFunc DOptTransfers4 = { OptTransfers4, "OptTransfers4", 65, 0, 0, 0, 0, 0 };
1439 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0, 0 };
1440 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0, 0 };
1443 /* Table containing all the steps in alphabetical order */
1444 static OptFunc* OptFuncs[] = {
1535 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1539 static int CmpOptStep (const void* Key, const void* Func)
1540 /* Compare function for bsearch */
1542 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1547 static OptFunc* FindOptFunc (const char* Name)
1548 /* Find an optimizer step by name in the table and return a pointer. Return
1549 * NULL if no such step is found.
1552 /* Search for the function in the list */
1553 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1559 static OptFunc* GetOptFunc (const char* Name)
1560 /* Find an optimizer step by name in the table and return a pointer. Print an
1561 * error and call AbEnd if not found.
1564 /* Search for the function in the list */
1565 OptFunc* F = FindOptFunc (Name);
1568 AbEnd ("Optimization step `%s' not found", Name);
1575 void DisableOpt (const char* Name)
1576 /* Disable the optimization with the given name */
1578 if (strcmp (Name, "any") == 0) {
1580 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1581 OptFuncs[I]->Disabled = 1;
1584 GetOptFunc(Name)->Disabled = 1;
1590 void EnableOpt (const char* Name)
1591 /* Enable the optimization with the given name */
1593 if (strcmp (Name, "any") == 0) {
1595 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1596 OptFuncs[I]->Disabled = 0;
1599 GetOptFunc(Name)->Disabled = 0;
1605 void ListOptSteps (FILE* F)
1606 /* List all optimization steps */
1609 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1610 fprintf (F, "%s\n", OptFuncs[I]->Name);
1616 static void ReadOptStats (const char* Name)
1617 /* Read the optimizer statistics file */
1622 /* Try to open the file */
1623 FILE* F = fopen (Name, "r");
1625 /* Ignore the error */
1629 /* Read and parse the lines */
1631 while (fgets (Buf, sizeof (Buf), F) != 0) {
1639 unsigned long TotalRuns;
1640 unsigned long TotalChanges;
1645 /* Remove trailing white space including the line terminator */
1648 while (Len > 0 && IsSpace (B[Len-1])) {
1653 /* Remove leading whitespace */
1654 while (IsSpace (*B)) {
1658 /* Check for empty and comment lines */
1659 if (*B == '\0' || *B == ';' || *B == '#') {
1663 /* Parse the line */
1664 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1669 /* Search for the optimizer step. */
1670 Func = FindOptFunc (Name);
1676 /* Found the step, set the fields */
1677 Func->TotalRuns = TotalRuns;
1678 Func->TotalChanges = TotalChanges;
1682 /* Close the file, ignore errors here. */
1688 static void WriteOptStats (const char* Name)
1689 /* Write the optimizer statistics file */
1693 /* Try to open the file */
1694 FILE* F = fopen (Name, "w");
1696 /* Ignore the error */
1700 /* Write a header */
1702 "; Optimizer Total Last Total Last\n"
1703 "; Step Runs Runs Chg Chg\n");
1706 /* Write the data */
1707 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1708 const OptFunc* O = OptFuncs[I];
1710 "%-20s %10lu %10lu %10lu %10lu\n",
1718 /* Close the file, ignore errors here. */
1724 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1725 /* Run one optimizer function Max times or until there are no more changes */
1727 unsigned Changes, C;
1729 /* Don't run the function if it is disabled or if it is prohibited by the
1732 if (F->Disabled || F->CodeSizeFactor > S->CodeSizeFactor) {
1736 /* Run this until there are no more changes */
1740 /* Run the function */
1747 F->TotalChanges += C;
1748 F->LastChanges += C;
1750 } while (--Max && C > 0);
1752 /* Return the number of changes */
1758 static unsigned RunOptGroup1 (CodeSeg* S)
1759 /* Run the first group of optimization steps. These steps translate known
1760 * patterns emitted by the code generator into more optimal patterns. Order
1761 * of the steps is important, because some of the steps done earlier cover
1762 * the same patterns as later steps as subpatterns.
1765 unsigned Changes = 0;
1767 Changes += RunOptFunc (S, &DOptStackPtrOps, 5);
1768 Changes += RunOptFunc (S, &DOptPtrStore1, 1);
1769 Changes += RunOptFunc (S, &DOptPtrStore2, 1);
1770 Changes += RunOptFunc (S, &DOptPtrStore3, 1);
1771 Changes += RunOptFunc (S, &DOptAdd3, 1); /* Before OptPtrLoad5! */
1772 Changes += RunOptFunc (S, &DOptPtrLoad1, 1);
1773 Changes += RunOptFunc (S, &DOptPtrLoad2, 1);
1774 Changes += RunOptFunc (S, &DOptPtrLoad3, 1);
1775 Changes += RunOptFunc (S, &DOptPtrLoad4, 1);
1776 Changes += RunOptFunc (S, &DOptPtrLoad5, 1);
1777 Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
1778 Changes += RunOptFunc (S, &DOptPtrLoad7, 1);
1779 Changes += RunOptFunc (S, &DOptPtrLoad11, 1);
1780 Changes += RunOptFunc (S, &DOptPtrLoad12, 1);
1781 Changes += RunOptFunc (S, &DOptPtrLoad13, 1);
1782 Changes += RunOptFunc (S, &DOptPtrLoad14, 1);
1783 Changes += RunOptFunc (S, &DOptPtrLoad15, 1);
1784 Changes += RunOptFunc (S, &DOptPtrLoad16, 1);
1785 Changes += RunOptFunc (S, &DOptPtrLoad17, 1);
1786 Changes += RunOptFunc (S, &DOptNegAX1, 1);
1787 Changes += RunOptFunc (S, &DOptNegAX2, 1);
1788 Changes += RunOptFunc (S, &DOptNegAX3, 1);
1789 Changes += RunOptFunc (S, &DOptNegAX4, 1);
1790 Changes += RunOptFunc (S, &DOptAdd1, 1);
1791 Changes += RunOptFunc (S, &DOptAdd2, 1);
1792 Changes += RunOptFunc (S, &DOptAdd4, 1);
1793 Changes += RunOptFunc (S, &DOptAdd5, 1);
1794 Changes += RunOptFunc (S, &DOptAdd6, 1);
1795 Changes += RunOptFunc (S, &DOptAdd6, 1);
1796 Changes += RunOptFunc (S, &DOptSub1, 1);
1797 Changes += RunOptFunc (S, &DOptSub3, 1);
1798 Changes += RunOptFunc (S, &DOptStore4, 1);
1799 Changes += RunOptFunc (S, &DOptStore5, 1);
1800 Changes += RunOptFunc (S, &DOptShift1, 1);
1801 Changes += RunOptFunc (S, &DOptShift2, 1);
1802 Changes += RunOptFunc (S, &DOptShift3, 1);
1803 Changes += RunOptFunc (S, &DOptShift4, 1);
1804 Changes += RunOptFunc (S, &DOptStore1, 1);
1805 Changes += RunOptFunc (S, &DOptStore2, 5);
1806 Changes += RunOptFunc (S, &DOptStore3, 5);
1808 /* Return the number of changes */
1814 static unsigned RunOptGroup2 (CodeSeg* S)
1815 /* Run one group of optimization steps. This step involves just decoupling
1816 * instructions by replacing them by instructions that do not depend on
1817 * previous instructions. This makes it easier to find instructions that
1821 unsigned Changes = 0;
1823 Changes += RunOptFunc (S, &DOptDecouple, 1);
1825 /* Return the number of changes */
1831 static unsigned RunOptGroup3 (CodeSeg* S)
1832 /* Run one group of optimization steps. These steps depend on each other,
1833 * that means that one step may allow another step to do additional work,
1834 * so we will repeat the steps as long as we see any changes.
1837 unsigned Changes, C;
1843 C += RunOptFunc (S, &DOptNegA1, 1);
1844 C += RunOptFunc (S, &DOptNegA2, 1);
1845 C += RunOptFunc (S, &DOptStackOps, 1);
1846 C += RunOptFunc (S, &DOptSub1, 1);
1847 C += RunOptFunc (S, &DOptSub2, 1);
1848 C += RunOptFunc (S, &DOptSub3, 1);
1849 C += RunOptFunc (S, &DOptAdd5, 1);
1850 C += RunOptFunc (S, &DOptAdd6, 1);
1851 C += RunOptFunc (S, &DOptJumpCascades, 1);
1852 C += RunOptFunc (S, &DOptDeadJumps, 1);
1853 C += RunOptFunc (S, &DOptRTS, 1);
1854 C += RunOptFunc (S, &DOptDeadCode, 1);
1855 C += RunOptFunc (S, &DOptBoolTrans, 1);
1856 C += RunOptFunc (S, &DOptJumpTarget1, 1);
1857 C += RunOptFunc (S, &DOptJumpTarget2, 1);
1858 C += RunOptFunc (S, &DOptCondBranches1, 1);
1859 C += RunOptFunc (S, &DOptCondBranches2, 1);
1860 C += RunOptFunc (S, &DOptRTSJumps1, 1);
1861 C += RunOptFunc (S, &DOptCmp1, 1);
1862 C += RunOptFunc (S, &DOptCmp2, 1);
1863 C += RunOptFunc (S, &DOptCmp3, 1);
1864 C += RunOptFunc (S, &DOptCmp4, 1);
1865 C += RunOptFunc (S, &DOptCmp5, 1);
1866 C += RunOptFunc (S, &DOptCmp6, 1);
1867 C += RunOptFunc (S, &DOptCmp7, 1);
1868 C += RunOptFunc (S, &DOptCmp8, 1);
1869 C += RunOptFunc (S, &DOptCmp9, 1);
1870 C += RunOptFunc (S, &DOptTest1, 1);
1871 C += RunOptFunc (S, &DOptLoad1, 1);
1872 C += RunOptFunc (S, &DOptJumpTarget3, 1); /* After OptCondBranches2 */
1873 C += RunOptFunc (S, &DOptUnusedLoads, 1);
1874 C += RunOptFunc (S, &DOptUnusedStores, 1);
1875 C += RunOptFunc (S, &DOptDupLoads, 1);
1876 C += RunOptFunc (S, &DOptStoreLoad, 1);
1877 C += RunOptFunc (S, &DOptTransfers1, 1);
1878 C += RunOptFunc (S, &DOptTransfers3, 1);
1879 C += RunOptFunc (S, &DOptTransfers4, 1);
1880 C += RunOptFunc (S, &DOptStore1, 1);
1881 C += RunOptFunc (S, &DOptStore5, 1);
1882 C += RunOptFunc (S, &DOptPushPop, 1);
1883 C += RunOptFunc (S, &DOptPrecalc, 1);
1889 /* Return the number of changes */
1895 static unsigned RunOptGroup4 (CodeSeg* S)
1896 /* Run another round of pattern replacements. These are done late, since there
1897 * may be better replacements before.
1900 unsigned Changes = 0;
1902 /* Repeat some of the steps here */
1903 Changes += RunOptFunc (S, &DOptPush1, 1);
1904 Changes += RunOptFunc (S, &DOptPush2, 1);
1905 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1906 Changes += RunOptFunc (S, &DOptTest2, 1);
1907 Changes += RunOptFunc (S, &DOptTransfers2, 1);
1908 Changes += RunOptFunc (S, &DOptLoad2, 1);
1910 /* Return the number of changes */
1916 static unsigned RunOptGroup5 (CodeSeg* S)
1917 /* 65C02 specific optimizations. */
1919 unsigned Changes = 0;
1921 if (CPUIsets[CPU] & CPU_ISET_65SC02) {
1922 Changes += RunOptFunc (S, &DOpt65C02BitOps, 1);
1923 Changes += RunOptFunc (S, &DOpt65C02Ind, 1);
1924 Changes += RunOptFunc (S, &DOpt65C02Stores, 1);
1926 /* The 65C02 replacement codes do often make the use of a register
1927 * value unnecessary, so if we have changes, run another load
1930 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1934 /* Return the number of changes */
1940 static unsigned RunOptGroup6 (CodeSeg* S)
1941 /* This one is quite special. It tries to replace "lda (sp),y" by "lda (sp,x)".
1942 * The latter is ony cycle slower, but if we're able to remove the necessary
1943 * load of the Y register, because X is zero anyway, we gain 1 cycle and
1944 * shorten the code by one (transfer) or two bytes (load). So what we do is
1945 * to replace the insns, remove unused loads, and then change back all insns
1946 * where Y is still zero (meaning that the load has not been removed).
1949 unsigned Changes = 0;
1951 /* This group will only run for a standard 6502, because the 65C02 has a
1952 * better addressing mode that covers this case.
1954 if ((CPUIsets[CPU] & CPU_ISET_65SC02) == 0) {
1955 Changes += RunOptFunc (S, &DOptIndLoads1, 1);
1956 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1957 Changes += RunOptFunc (S, &DOptIndLoads2, 1);
1960 /* Return the number of changes */
1966 static unsigned RunOptGroup7 (CodeSeg* S)
1967 /* The last group of optimization steps. Adjust branches, do size optimizations.
1970 unsigned Changes = 0;
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, &DOptUnusedStores, 1);
1985 Changes += RunOptFunc (S, &DOptJumpTarget1, 5);
1986 Changes += RunOptFunc (S, &DOptStore5, 1);
1989 C = RunOptFunc (S, &DOptSize2, 1);
1992 /* Run some optimization passes again, since the size optimizations
1993 * may have opened new oportunities.
1995 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1996 Changes += RunOptFunc (S, &DOptJumpTarget1, 5);
1997 Changes += RunOptFunc (S, &DOptStore5, 1);
1998 Changes += RunOptFunc (S, &DOptTransfers3, 1);
2001 /* Adjust branch distances */
2002 Changes += RunOptFunc (S, &DOptBranchDist, 3);
2004 /* Replace conditional branches to RTS. If we had changes, we must run dead
2005 * code elimination again, since the change may have introduced dead code.
2007 C = RunOptFunc (S, &DOptRTSJumps2, 1);
2010 Changes += RunOptFunc (S, &DOptDeadCode, 1);
2013 /* Return the number of changes */
2019 void RunOpt (CodeSeg* S)
2020 /* Run the optimizer */
2022 const char* StatFileName;
2024 /* If we shouldn't run the optimizer, bail out */
2029 /* Check if we are requested to write optimizer statistics */
2030 StatFileName = getenv ("CC65_OPTSTATS");
2032 ReadOptStats (StatFileName);
2035 /* Print the name of the function we are working on */
2037 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
2039 Print (stdout, 1, "Running optimizer for global code segment\n");
2042 /* Run groups of optimizations */
2051 /* Write statistics */
2053 WriteOptStats (StatFileName);