1 /*****************************************************************************/
5 /* Optimizer subroutines */
9 /* (C) 2001-2009 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 /*****************************************************************************/
531 /* Optimize stores through pointers */
532 /*****************************************************************************/
536 static unsigned OptPtrStore1Sub (CodeSeg* S, unsigned I, CodeEntry** const L)
537 /* Check if this is one of the allowed suboperation for OptPtrStore1 */
539 /* Check for a label attached to the entry */
540 if (CE_HasLabel (L[0])) {
544 /* Check for single insn sub ops */
545 if (L[0]->OPC == OP65_AND ||
546 L[0]->OPC == OP65_EOR ||
547 L[0]->OPC == OP65_ORA ||
548 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shlax", 5) == 0) ||
549 (L[0]->OPC == OP65_JSR && strncmp (L[0]->Arg, "shrax", 5) == 0)) {
554 } else if (L[0]->OPC == OP65_CLC &&
555 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
556 L[1]->OPC == OP65_ADC &&
557 !CE_HasLabel (L[1])) {
559 } else if (L[0]->OPC == OP65_SEC &&
560 (L[1] = CS_GetNextEntry (S, I)) != 0 &&
561 L[1]->OPC == OP65_SBC &&
562 !CE_HasLabel (L[1])) {
574 static unsigned OptPtrStore1 (CodeSeg* S)
575 /* Search for the sequence:
595 * In case a/x is loaded from the register bank before the pushax, we can even
596 * use the register bank instead of ptr1.
610 unsigned Changes = 0;
612 /* Walk over the entries */
614 while (I < CS_GetEntryCount (S)) {
620 L[0] = CS_GetEntry (S, I);
622 /* Check for the sequence */
623 if (CE_IsCallTo (L[0], "pushax") &&
624 CS_GetEntries (S, L+1, I+1, 3) &&
625 L[1]->OPC == OP65_LDY &&
626 CE_IsConstImm (L[1]) &&
627 !CE_HasLabel (L[1]) &&
628 CE_IsCallTo (L[2], "ldauidx") &&
629 !CE_HasLabel (L[2]) &&
630 (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
631 CS_GetEntries (S, L+3+K, I+3+K, 2) &&
632 L[3+K]->OPC == OP65_LDY &&
633 CE_IsConstImm (L[3+K]) &&
634 !CE_HasLabel (L[3+K]) &&
635 CE_IsCallTo (L[4+K], "staspidx") &&
636 !CE_HasLabel (L[4+K])) {
639 const char* RegBank = 0;
640 const char* ZPLoc = "ptr1";
644 /* Get the preceeding two instructions and check them. We check
651 P[0] = CS_GetEntry (S, I-2);
652 P[1] = CS_GetEntry (S, I-1);
653 if (P[0]->OPC == OP65_LDA &&
654 P[0]->AM == AM65_ZP &&
655 P[1]->OPC == OP65_LDX &&
656 P[1]->AM == AM65_ZP &&
657 !CE_HasLabel (P[1]) &&
658 strncmp (P[0]->Arg, "regbank+", 8) == 0) {
660 unsigned Len = strlen (P[0]->Arg);
662 if (strncmp (P[0]->Arg, P[1]->Arg, Len) == 0 &&
663 P[1]->Arg[Len+0] == '+' &&
664 P[1]->Arg[Len+1] == '1' &&
665 P[1]->Arg[Len+2] == '\0') {
667 /* Ok, found. Use the name of the register bank */
668 RegBank = ZPLoc = P[0]->Arg;
673 /* Insert the load via the zp pointer */
674 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
675 CS_InsertEntry (S, X, I+3);
676 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, ZPLoc, 0, L[2]->LI);
677 CS_InsertEntry (S, X, I+4);
679 /* Insert the store through the zp pointer */
680 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, ZPLoc, 0, L[3]->LI);
681 CS_InsertEntry (S, X, I+6+K);
683 /* Delete the old code */
684 CS_DelEntry (S, I+7+K); /* jsr spaspidx */
685 CS_DelEntry (S, I+2); /* jsr ldauidx */
687 /* Create and insert the stores into the zp pointer if needed */
689 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
690 CS_InsertEntry (S, X, I+1);
691 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
692 CS_InsertEntry (S, X, I+2);
695 /* Delete more old code. Do it here to keep a label attached to
698 CS_DelEntry (S, I); /* jsr pushax */
700 /* Remember, we had changes */
710 /* Return the number of changes made */
716 static unsigned OptPtrStore2 (CodeSeg* S)
717 /* Search for the sequence:
739 unsigned Changes = 0;
741 /* Walk over the entries */
743 while (I < CS_GetEntryCount (S)) {
749 L[0] = CS_GetEntry (S, I);
751 /* Check for the sequence */
752 if (L[0]->OPC == OP65_LDA &&
753 L[0]->AM == AM65_IMM &&
754 CS_GetEntries (S, L+1, I+1, 10) &&
755 L[1]->OPC == OP65_LDX &&
756 L[1]->AM == AM65_IMM &&
757 L[2]->OPC == OP65_CLC &&
758 L[3]->OPC == OP65_ADC &&
759 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
760 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
762 L[4]->JumpTo->Owner == L[6] &&
763 L[5]->OPC == OP65_INX &&
764 CE_IsCallTo (L[6], "pushax") &&
765 L[7]->OPC == OP65_LDX &&
766 L[8]->OPC == OP65_LDA &&
767 L[9]->OPC == OP65_LDY &&
768 CE_IsKnownImm (L[9], 0) &&
769 CE_IsCallTo (L[10], "staspidx") &&
770 !CS_RangeHasLabel (S, I+1, 5) &&
771 !CS_RangeHasLabel (S, I+7, 4) &&
772 /* Check the label last because this is quite costly */
773 (Len = strlen (L[0]->Arg)) > 3 &&
774 L[0]->Arg[0] == '<' &&
775 L[0]->Arg[1] == '(' &&
776 strlen (L[1]->Arg) == Len &&
777 L[1]->Arg[0] == '>' &&
778 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
783 /* We will create all the new stuff behind the current one so
784 * we keep the line references.
786 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
787 CS_InsertEntry (S, X, I+11);
789 X = NewCodeEntry (OP65_LDX, L[7]->AM, L[7]->Arg, 0, L[7]->LI);
790 CS_InsertEntry (S, X, I+12);
792 X = NewCodeEntry (OP65_LDA, L[8]->AM, L[8]->Arg, 0, L[8]->LI);
793 CS_InsertEntry (S, X, I+13);
795 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
797 X = NewCodeEntry (OP65_STA, AM65_ABSY, Label, 0, L[10]->LI);
798 CS_InsertEntry (S, X, I+14);
801 /* Remove the old code */
802 CS_DelEntries (S, I, 11);
804 /* Remember, we had changes */
814 /* Return the number of changes made */
820 static unsigned OptPtrStore3 (CodeSeg* S)
821 /* Search for the sequence:
846 unsigned Changes = 0;
848 /* Walk over the entries */
850 while (I < CS_GetEntryCount (S)) {
856 L[0] = CS_GetEntry (S, I);
858 /* Check for the sequence */
859 if (L[0]->OPC == OP65_LDA &&
860 L[0]->AM == AM65_IMM &&
861 CS_GetEntries (S, L+1, I+1, 11) &&
862 L[1]->OPC == OP65_LDX &&
863 L[1]->AM == AM65_IMM &&
864 L[2]->OPC == OP65_LDY &&
865 L[3]->OPC == OP65_CLC &&
866 L[4]->OPC == OP65_ADC &&
867 L[4]->AM == AM65_ZP_INDY &&
868 strcmp (L[4]->Arg, "sp") == 0 &&
869 (L[5]->OPC == OP65_BCC || L[5]->OPC == OP65_JCC) &&
871 L[5]->JumpTo->Owner == L[7] &&
872 L[6]->OPC == OP65_INX &&
873 CE_IsCallTo (L[7], "pushax") &&
874 L[8]->OPC == OP65_LDX &&
875 L[9]->OPC == OP65_LDA &&
876 L[10]->OPC == OP65_LDY &&
877 CE_IsKnownImm (L[10], 0) &&
878 CE_IsCallTo (L[11], "staspidx") &&
879 !CS_RangeHasLabel (S, I+1, 6) &&
880 !CS_RangeHasLabel (S, I+8, 4) &&
881 /* Check the label last because this is quite costly */
882 (Len = strlen (L[0]->Arg)) > 3 &&
883 L[0]->Arg[0] == '<' &&
884 L[0]->Arg[1] == '(' &&
885 strlen (L[1]->Arg) == Len &&
886 L[1]->Arg[0] == '>' &&
887 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
892 /* We will create all the new stuff behind the current one so
893 * we keep the line references.
895 X = NewCodeEntry (OP65_LDY, L[2]->AM, L[2]->Arg, 0, L[2]->LI);
896 CS_InsertEntry (S, X, I+12);
898 X = NewCodeEntry (OP65_LDX, L[8]->AM, L[8]->Arg, 0, L[8]->LI);
899 CS_InsertEntry (S, X, I+13);
901 X = NewCodeEntry (OP65_LDA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
902 CS_InsertEntry (S, X, I+14);
904 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[4]->LI);
905 CS_InsertEntry (S, X, I+15);
907 X = NewCodeEntry (OP65_LDA, L[8]->AM, L[8]->Arg, 0, L[8]->LI);
908 CS_InsertEntry (S, X, I+16);
910 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
912 X = NewCodeEntry (OP65_STA, AM65_ABSY, Label, 0, L[11]->LI);
913 CS_InsertEntry (S, X, I+17);
916 /* Remove the old code */
917 CS_DelEntries (S, I, 12);
919 /* Remember, we had changes */
929 /* Return the number of changes made */
935 /*****************************************************************************/
936 /* Decouple operations */
937 /*****************************************************************************/
941 static unsigned OptDecouple (CodeSeg* S)
942 /* Decouple operations, that is, do the following replacements:
956 * Provided that the register values are known of course.
959 unsigned Changes = 0;
962 /* Generate register info for the following step */
965 /* Walk over the entries */
967 while (I < CS_GetEntryCount (S)) {
971 /* Get next entry and it's input register values */
972 CodeEntry* E = CS_GetEntry (S, I);
973 const RegContents* In = &E->RI->In;
975 /* Assume we have no replacement */
978 /* Check the instruction */
982 if (RegValIsKnown (In->RegA)) {
983 Arg = MakeHexArg ((In->RegA - 1) & 0xFF);
984 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
989 if (RegValIsKnown (In->RegX)) {
990 Arg = MakeHexArg ((In->RegX - 1) & 0xFF);
991 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
996 if (RegValIsKnown (In->RegY)) {
997 Arg = MakeHexArg ((In->RegY - 1) & 0xFF);
998 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1003 if (RegValIsKnown (In->RegA)) {
1004 Arg = MakeHexArg ((In->RegA + 1) & 0xFF);
1005 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1010 if (RegValIsKnown (In->RegX)) {
1011 Arg = MakeHexArg ((In->RegX + 1) & 0xFF);
1012 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1017 if (RegValIsKnown (In->RegY)) {
1018 Arg = MakeHexArg ((In->RegY + 1) & 0xFF);
1019 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1024 if (E->AM == AM65_ZP) {
1025 switch (GetKnownReg (E->Use & REG_ZP, In)) {
1027 Arg = MakeHexArg (In->Tmp1);
1028 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1032 Arg = MakeHexArg (In->Ptr1Lo);
1033 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1037 Arg = MakeHexArg (In->Ptr1Hi);
1038 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1042 Arg = MakeHexArg (In->SRegLo);
1043 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1047 Arg = MakeHexArg (In->SRegHi);
1048 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1055 if (E->AM == AM65_ZP) {
1056 switch (GetKnownReg (E->Use & REG_ZP, In)) {
1058 Arg = MakeHexArg (In->Tmp1);
1059 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1063 Arg = MakeHexArg (In->Ptr1Lo);
1064 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1068 Arg = MakeHexArg (In->Ptr1Hi);
1069 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1073 Arg = MakeHexArg (In->SRegLo);
1074 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1078 Arg = MakeHexArg (In->SRegHi);
1079 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1086 if (E->AM == AM65_ZP) {
1087 switch (GetKnownReg (E->Use, In)) {
1089 Arg = MakeHexArg (In->Tmp1);
1090 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1094 Arg = MakeHexArg (In->Ptr1Lo);
1095 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1099 Arg = MakeHexArg (In->Ptr1Hi);
1100 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1104 Arg = MakeHexArg (In->SRegLo);
1105 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1109 Arg = MakeHexArg (In->SRegHi);
1110 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1117 if (E->RI->In.RegA >= 0) {
1118 Arg = MakeHexArg (In->RegA);
1119 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, E->LI);
1124 if (E->RI->In.RegA >= 0) {
1125 Arg = MakeHexArg (In->RegA);
1126 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
1131 if (E->RI->In.RegX >= 0) {
1132 Arg = MakeHexArg (In->RegX);
1133 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1138 if (E->RI->In.RegY >= 0) {
1139 Arg = MakeHexArg (In->RegY);
1140 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, E->LI);
1145 /* Avoid gcc warnings */
1150 /* Insert the replacement if we have one */
1152 CS_InsertEntry (S, X, I+1);
1162 /* Free register info */
1165 /* Return the number of changes made */
1171 /*****************************************************************************/
1172 /* Optimize stack pointer ops */
1173 /*****************************************************************************/
1177 static unsigned IsDecSP (const CodeEntry* E)
1178 /* Check if this is an insn that decrements the stack pointer. If so, return
1179 * the decrement. If not, return zero.
1180 * The function expects E to be a subroutine call.
1183 if (strncmp (E->Arg, "decsp", 5) == 0) {
1184 if (E->Arg[5] >= '1' && E->Arg[5] <= '8') {
1185 return (E->Arg[5] - '0');
1187 } else if (strcmp (E->Arg, "subysp") == 0 && RegValIsKnown (E->RI->In.RegY)) {
1188 return E->RI->In.RegY;
1191 /* If we come here, it's not a decsp op */
1197 static unsigned OptStackPtrOps (CodeSeg* S)
1198 /* Merge adjacent calls to decsp into one. NOTE: This function won't merge all
1202 unsigned Changes = 0;
1205 /* Generate register info for the following step */
1208 /* Walk over the entries */
1210 while (I < CS_GetEntryCount (S)) {
1216 /* Get the next entry */
1217 const CodeEntry* E = CS_GetEntry (S, I);
1219 /* Check for decspn or subysp */
1220 if (E->OPC == OP65_JSR &&
1221 (Dec1 = IsDecSP (E)) > 0 &&
1222 (N = CS_GetNextEntry (S, I)) != 0 &&
1223 (Dec2 = IsDecSP (N)) > 0 &&
1224 (Dec1 += Dec2) <= 255 &&
1230 /* We can combine the two */
1232 /* Insert a call to decsp */
1233 xsprintf (Buf, sizeof (Buf), "decsp%u", Dec1);
1234 X = NewCodeEntry (OP65_JSR, AM65_ABS, Buf, 0, N->LI);
1235 CS_InsertEntry (S, X, I+2);
1237 /* Insert a call to subysp */
1238 const char* Arg = MakeHexArg (Dec1);
1239 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, N->LI);
1240 CS_InsertEntry (S, X, I+2);
1241 X = NewCodeEntry (OP65_JSR, AM65_ABS, "subysp", 0, N->LI);
1242 CS_InsertEntry (S, X, I+3);
1245 /* Delete the old code */
1246 CS_DelEntries (S, I, 2);
1248 /* Regenerate register info */
1251 /* Remember we had changes */
1262 /* Free register info */
1265 /* Return the number of changes made */
1271 /*****************************************************************************/
1272 /* struct OptFunc */
1273 /*****************************************************************************/
1277 typedef struct OptFunc OptFunc;
1279 unsigned (*Func) (CodeSeg*); /* Optimizer function */
1280 const char* Name; /* Name of the function/group */
1281 unsigned CodeSizeFactor; /* Code size factor for this opt func */
1282 unsigned long TotalRuns; /* Total number of runs */
1283 unsigned long LastRuns; /* Last number of runs */
1284 unsigned long TotalChanges; /* Total number of changes */
1285 unsigned long LastChanges; /* Last number of changes */
1286 char Disabled; /* True if function disabled */
1291 /*****************************************************************************/
1293 /*****************************************************************************/
1297 /* A list of all the function descriptions */
1298 static OptFunc DOpt65C02BitOps = { Opt65C02BitOps, "Opt65C02BitOps", 66, 0, 0, 0, 0, 0 };
1299 static OptFunc DOpt65C02Ind = { Opt65C02Ind, "Opt65C02Ind", 100, 0, 0, 0, 0, 0 };
1300 static OptFunc DOpt65C02Stores = { Opt65C02Stores, "Opt65C02Stores", 100, 0, 0, 0, 0, 0 };
1301 static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 125, 0, 0, 0, 0, 0 };
1302 static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 200, 0, 0, 0, 0, 0 };
1303 static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 65, 0, 0, 0, 0, 0 };
1304 static OptFunc DOptAdd4 = { OptAdd4, "OptAdd4", 90, 0, 0, 0, 0, 0 };
1305 static OptFunc DOptAdd5 = { OptAdd5, "OptAdd5", 100, 0, 0, 0, 0, 0 };
1306 static OptFunc DOptAdd6 = { OptAdd6, "OptAdd6", 40, 0, 0, 0, 0, 0 };
1307 static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 100, 0, 0, 0, 0, 0 };
1308 static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0, 0 };
1309 static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 42, 0, 0, 0, 0, 0 };
1310 static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 85, 0, 0, 0, 0, 0 };
1311 static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 75, 0, 0, 0, 0, 0 };
1312 static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 75, 0, 0, 0, 0, 0 };
1313 static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 100, 0, 0, 0, 0, 0 };
1314 static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 100, 0, 0, 0, 0, 0 };
1315 static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 85, 0, 0, 0, 0, 0 };
1316 static OptFunc DOptCmp8 = { OptCmp8, "OptCmp8", 50, 0, 0, 0, 0, 0 };
1317 static OptFunc DOptCmp9 = { OptCmp9, "OptCmp9", 85, 0, 0, 0, 0, 0 };
1318 static OptFunc DOptCondBranches1= { OptCondBranches1,"OptCondBranches1", 80, 0, 0, 0, 0, 0 };
1319 static OptFunc DOptCondBranches2= { OptCondBranches2,"OptCondBranches2", 0, 0, 0, 0, 0, 0 };
1320 static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 100, 0, 0, 0, 0, 0 };
1321 static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 100, 0, 0, 0, 0, 0 };
1322 static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 100, 0, 0, 0, 0, 0 };
1323 static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 0, 0, 0, 0, 0, 0 };
1324 static OptFunc DOptIndLoads1 = { OptIndLoads1, "OptIndLoads1", 0, 0, 0, 0, 0, 0 };
1325 static OptFunc DOptIndLoads2 = { OptIndLoads2, "OptIndLoads2", 0, 0, 0, 0, 0, 0 };
1326 static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 100, 0, 0, 0, 0, 0 };
1327 static OptFunc DOptJumpTarget1 = { OptJumpTarget1, "OptJumpTarget1", 100, 0, 0, 0, 0, 0 };
1328 static OptFunc DOptJumpTarget2 = { OptJumpTarget2, "OptJumpTarget2", 100, 0, 0, 0, 0, 0 };
1329 static OptFunc DOptJumpTarget3 = { OptJumpTarget3, "OptJumpTarget3", 100, 0, 0, 0, 0, 0 };
1330 static OptFunc DOptLoad1 = { OptLoad1, "OptLoad1", 100, 0, 0, 0, 0, 0 };
1331 static OptFunc DOptRTS = { OptRTS, "OptRTS", 100, 0, 0, 0, 0, 0 };
1332 static OptFunc DOptRTSJumps1 = { OptRTSJumps1, "OptRTSJumps1", 100, 0, 0, 0, 0, 0 };
1333 static OptFunc DOptRTSJumps2 = { OptRTSJumps2, "OptRTSJumps2", 100, 0, 0, 0, 0, 0 };
1334 static OptFunc DOptNegA1 = { OptNegA1, "OptNegA1", 100, 0, 0, 0, 0, 0 };
1335 static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 100, 0, 0, 0, 0, 0 };
1336 static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 100, 0, 0, 0, 0, 0 };
1337 static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 100, 0, 0, 0, 0, 0 };
1338 static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 100, 0, 0, 0, 0, 0 };
1339 static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 100, 0, 0, 0, 0, 0 };
1340 static OptFunc DOptPrecalc = { OptPrecalc, "OptPrecalc", 100, 0, 0, 0, 0, 0 };
1341 static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 100, 0, 0, 0, 0, 0 };
1342 static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 };
1343 static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 };
1344 static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 };
1345 static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 50, 0, 0, 0, 0, 0 };
1346 static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 60, 0, 0, 0, 0, 0 };
1347 static OptFunc DOptPtrLoad7 = { OptPtrLoad7, "OptPtrLoad7", 140, 0, 0, 0, 0, 0 };
1348 static OptFunc DOptPtrLoad11 = { OptPtrLoad11, "OptPtrLoad11", 92, 0, 0, 0, 0, 0 };
1349 static OptFunc DOptPtrLoad12 = { OptPtrLoad12, "OptPtrLoad12", 50, 0, 0, 0, 0, 0 };
1350 static OptFunc DOptPtrLoad13 = { OptPtrLoad13, "OptPtrLoad13", 65, 0, 0, 0, 0, 0 };
1351 static OptFunc DOptPtrLoad14 = { OptPtrLoad14, "OptPtrLoad14", 108, 0, 0, 0, 0, 0 };
1352 static OptFunc DOptPtrLoad15 = { OptPtrLoad15, "OptPtrLoad15", 86, 0, 0, 0, 0, 0 };
1353 static OptFunc DOptPtrLoad16 = { OptPtrLoad16, "OptPtrLoad16", 100, 0, 0, 0, 0, 0 };
1354 static OptFunc DOptPtrLoad17 = { OptPtrLoad17, "OptPtrLoad17", 190, 0, 0, 0, 0, 0 };
1355 static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 100, 0, 0, 0, 0, 0 };
1356 static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 40, 0, 0, 0, 0, 0 };
1357 static OptFunc DOptPtrStore3 = { OptPtrStore3, "OptPtrStore3", 50, 0, 0, 0, 0, 0 };
1358 static OptFunc DOptPush1 = { OptPush1, "OptPush1", 65, 0, 0, 0, 0, 0 };
1359 static OptFunc DOptPush2 = { OptPush2, "OptPush2", 50, 0, 0, 0, 0, 0 };
1360 static OptFunc DOptPushPop = { OptPushPop, "OptPushPop", 0, 0, 0, 0, 0, 0 };
1361 static OptFunc DOptShift1 = { OptShift1, "OptShift1", 100, 0, 0, 0, 0, 0 };
1362 static OptFunc DOptShift2 = { OptShift2, "OptShift2", 100, 0, 0, 0, 0, 0 };
1363 static OptFunc DOptShift3 = { OptShift3, "OptShift3", 110, 0, 0, 0, 0, 0 };
1364 static OptFunc DOptShift4 = { OptShift4, "OptShift4", 200, 0, 0, 0, 0, 0 };
1365 static OptFunc DOptSize1 = { OptSize1, "OptSize1", 100, 0, 0, 0, 0, 0 };
1366 static OptFunc DOptSize2 = { OptSize2, "OptSize2", 100, 0, 0, 0, 0, 0 };
1367 static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 100, 0, 0, 0, 0, 0 };
1368 static OptFunc DOptStackPtrOps = { OptStackPtrOps, "OptStackPtrOps", 50, 0, 0, 0, 0, 0 };
1369 static OptFunc DOptStore1 = { OptStore1, "OptStore1", 70, 0, 0, 0, 0, 0 };
1370 static OptFunc DOptStore2 = { OptStore2, "OptStore2", 220, 0, 0, 0, 0, 0 };
1371 static OptFunc DOptStore3 = { OptStore3, "OptStore3", 120, 0, 0, 0, 0, 0 };
1372 static OptFunc DOptStore4 = { OptStore4, "OptStore4", 50, 0, 0, 0, 0, 0 };
1373 static OptFunc DOptStore5 = { OptStore5, "OptStore5", 100, 0, 0, 0, 0, 0 };
1374 static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 0, 0, 0, 0, 0, 0 };
1375 static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 };
1376 static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 };
1377 static OptFunc DOptSub3 = { OptSub3, "OptSub3", 100, 0, 0, 0, 0, 0 };
1378 static OptFunc DOptTest1 = { OptTest1, "OptTest1", 65, 0, 0, 0, 0, 0 };
1379 static OptFunc DOptTest2 = { OptTest2, "OptTest2", 50, 0, 0, 0, 0, 0 };
1380 static OptFunc DOptTransfers1 = { OptTransfers1, "OptTransfers1", 0, 0, 0, 0, 0, 0 };
1381 static OptFunc DOptTransfers2 = { OptTransfers2, "OptTransfers2", 60, 0, 0, 0, 0, 0 };
1382 static OptFunc DOptTransfers3 = { OptTransfers3, "OptTransfers3", 65, 0, 0, 0, 0, 0 };
1383 static OptFunc DOptTransfers4 = { OptTransfers4, "OptTransfers4", 65, 0, 0, 0, 0, 0 };
1384 static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 0, 0, 0, 0, 0, 0 };
1385 static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 0, 0, 0, 0, 0, 0 };
1388 /* Table containing all the steps in alphabetical order */
1389 static OptFunc* OptFuncs[] = {
1479 #define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
1483 static int CmpOptStep (const void* Key, const void* Func)
1484 /* Compare function for bsearch */
1486 return strcmp (Key, (*(const OptFunc**)Func)->Name);
1491 static OptFunc* FindOptFunc (const char* Name)
1492 /* Find an optimizer step by name in the table and return a pointer. Return
1493 * NULL if no such step is found.
1496 /* Search for the function in the list */
1497 OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
1503 static OptFunc* GetOptFunc (const char* Name)
1504 /* Find an optimizer step by name in the table and return a pointer. Print an
1505 * error and call AbEnd if not found.
1508 /* Search for the function in the list */
1509 OptFunc* F = FindOptFunc (Name);
1512 AbEnd ("Optimization step `%s' not found", Name);
1519 void DisableOpt (const char* Name)
1520 /* Disable the optimization with the given name */
1522 if (strcmp (Name, "any") == 0) {
1524 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1525 OptFuncs[I]->Disabled = 1;
1528 GetOptFunc(Name)->Disabled = 1;
1534 void EnableOpt (const char* Name)
1535 /* Enable the optimization with the given name */
1537 if (strcmp (Name, "any") == 0) {
1539 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1540 OptFuncs[I]->Disabled = 0;
1543 GetOptFunc(Name)->Disabled = 0;
1549 void ListOptSteps (FILE* F)
1550 /* List all optimization steps */
1553 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1554 fprintf (F, "%s\n", OptFuncs[I]->Name);
1560 static void ReadOptStats (const char* Name)
1561 /* Read the optimizer statistics file */
1566 /* Try to open the file */
1567 FILE* F = fopen (Name, "r");
1569 /* Ignore the error */
1573 /* Read and parse the lines */
1575 while (fgets (Buf, sizeof (Buf), F) != 0) {
1583 unsigned long TotalRuns;
1584 unsigned long TotalChanges;
1589 /* Remove trailing white space including the line terminator */
1592 while (Len > 0 && IsSpace (B[Len-1])) {
1597 /* Remove leading whitespace */
1598 while (IsSpace (*B)) {
1602 /* Check for empty and comment lines */
1603 if (*B == '\0' || *B == ';' || *B == '#') {
1607 /* Parse the line */
1608 if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
1613 /* Search for the optimizer step. */
1614 Func = FindOptFunc (Name);
1620 /* Found the step, set the fields */
1621 Func->TotalRuns = TotalRuns;
1622 Func->TotalChanges = TotalChanges;
1626 /* Close the file, ignore errors here. */
1632 static void WriteOptStats (const char* Name)
1633 /* Write the optimizer statistics file */
1637 /* Try to open the file */
1638 FILE* F = fopen (Name, "w");
1640 /* Ignore the error */
1644 /* Write a header */
1646 "; Optimizer Total Last Total Last\n"
1647 "; Step Runs Runs Chg Chg\n");
1650 /* Write the data */
1651 for (I = 0; I < OPTFUNC_COUNT; ++I) {
1652 const OptFunc* O = OptFuncs[I];
1654 "%-20s %10lu %10lu %10lu %10lu\n",
1662 /* Close the file, ignore errors here. */
1668 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
1669 /* Run one optimizer function Max times or until there are no more changes */
1671 unsigned Changes, C;
1673 /* Don't run the function if it is disabled or if it is prohibited by the
1676 if (F->Disabled || F->CodeSizeFactor > S->CodeSizeFactor) {
1680 /* Run this until there are no more changes */
1684 /* Run the function */
1691 F->TotalChanges += C;
1692 F->LastChanges += C;
1694 } while (--Max && C > 0);
1696 /* Return the number of changes */
1702 static unsigned RunOptGroup1 (CodeSeg* S)
1703 /* Run the first group of optimization steps. These steps translate known
1704 * patterns emitted by the code generator into more optimal patterns. Order
1705 * of the steps is important, because some of the steps done earlier cover
1706 * the same patterns as later steps as subpatterns.
1709 unsigned Changes = 0;
1711 Changes += RunOptFunc (S, &DOptStackPtrOps, 5);
1712 Changes += RunOptFunc (S, &DOptPtrStore1, 1);
1713 Changes += RunOptFunc (S, &DOptPtrStore2, 1);
1714 Changes += RunOptFunc (S, &DOptPtrStore3, 1);
1715 Changes += RunOptFunc (S, &DOptAdd3, 1); /* Before OptPtrLoad5! */
1716 Changes += RunOptFunc (S, &DOptPtrLoad1, 1);
1717 Changes += RunOptFunc (S, &DOptPtrLoad2, 1);
1718 Changes += RunOptFunc (S, &DOptPtrLoad3, 1);
1719 Changes += RunOptFunc (S, &DOptPtrLoad4, 1);
1720 Changes += RunOptFunc (S, &DOptPtrLoad5, 1);
1721 Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
1722 Changes += RunOptFunc (S, &DOptPtrLoad7, 1);
1723 Changes += RunOptFunc (S, &DOptPtrLoad11, 1);
1724 Changes += RunOptFunc (S, &DOptPtrLoad12, 1);
1725 Changes += RunOptFunc (S, &DOptPtrLoad13, 1);
1726 Changes += RunOptFunc (S, &DOptPtrLoad14, 1);
1727 Changes += RunOptFunc (S, &DOptPtrLoad15, 1);
1728 Changes += RunOptFunc (S, &DOptPtrLoad16, 1);
1729 Changes += RunOptFunc (S, &DOptPtrLoad17, 1);
1730 Changes += RunOptFunc (S, &DOptNegAX1, 1);
1731 Changes += RunOptFunc (S, &DOptNegAX2, 1);
1732 Changes += RunOptFunc (S, &DOptNegAX3, 1);
1733 Changes += RunOptFunc (S, &DOptNegAX4, 1);
1734 Changes += RunOptFunc (S, &DOptAdd1, 1);
1735 Changes += RunOptFunc (S, &DOptAdd2, 1);
1736 Changes += RunOptFunc (S, &DOptAdd4, 1);
1737 Changes += RunOptFunc (S, &DOptAdd5, 1);
1738 Changes += RunOptFunc (S, &DOptAdd6, 1);
1739 Changes += RunOptFunc (S, &DOptAdd6, 1);
1740 Changes += RunOptFunc (S, &DOptSub1, 1);
1741 Changes += RunOptFunc (S, &DOptSub3, 1);
1742 Changes += RunOptFunc (S, &DOptStore4, 1);
1743 Changes += RunOptFunc (S, &DOptStore5, 1);
1744 Changes += RunOptFunc (S, &DOptShift1, 1);
1745 Changes += RunOptFunc (S, &DOptShift2, 1);
1746 Changes += RunOptFunc (S, &DOptShift3, 1);
1747 Changes += RunOptFunc (S, &DOptShift4, 1);
1748 Changes += RunOptFunc (S, &DOptStore1, 1);
1749 Changes += RunOptFunc (S, &DOptStore2, 5);
1750 Changes += RunOptFunc (S, &DOptStore3, 5);
1752 /* Return the number of changes */
1758 static unsigned RunOptGroup2 (CodeSeg* S)
1759 /* Run one group of optimization steps. This step involves just decoupling
1760 * instructions by replacing them by instructions that do not depend on
1761 * previous instructions. This makes it easier to find instructions that
1765 unsigned Changes = 0;
1767 Changes += RunOptFunc (S, &DOptDecouple, 1);
1769 /* Return the number of changes */
1775 static unsigned RunOptGroup3 (CodeSeg* S)
1776 /* Run one group of optimization steps. These steps depend on each other,
1777 * that means that one step may allow another step to do additional work,
1778 * so we will repeat the steps as long as we see any changes.
1781 unsigned Changes, C;
1787 C += RunOptFunc (S, &DOptNegA1, 1);
1788 C += RunOptFunc (S, &DOptNegA2, 1);
1789 C += RunOptFunc (S, &DOptStackOps, 1);
1790 C += RunOptFunc (S, &DOptSub1, 1);
1791 C += RunOptFunc (S, &DOptSub2, 1);
1792 C += RunOptFunc (S, &DOptSub3, 1);
1793 C += RunOptFunc (S, &DOptAdd5, 1);
1794 C += RunOptFunc (S, &DOptAdd6, 1);
1795 C += RunOptFunc (S, &DOptJumpCascades, 1);
1796 C += RunOptFunc (S, &DOptDeadJumps, 1);
1797 C += RunOptFunc (S, &DOptRTS, 1);
1798 C += RunOptFunc (S, &DOptDeadCode, 1);
1799 C += RunOptFunc (S, &DOptBoolTrans, 1);
1800 C += RunOptFunc (S, &DOptJumpTarget1, 1);
1801 C += RunOptFunc (S, &DOptJumpTarget2, 1);
1802 C += RunOptFunc (S, &DOptCondBranches1, 1);
1803 C += RunOptFunc (S, &DOptCondBranches2, 1);
1804 C += RunOptFunc (S, &DOptRTSJumps1, 1);
1805 C += RunOptFunc (S, &DOptCmp1, 1);
1806 C += RunOptFunc (S, &DOptCmp2, 1);
1807 C += RunOptFunc (S, &DOptCmp3, 1);
1808 C += RunOptFunc (S, &DOptCmp4, 1);
1809 C += RunOptFunc (S, &DOptCmp5, 1);
1810 C += RunOptFunc (S, &DOptCmp6, 1);
1811 C += RunOptFunc (S, &DOptCmp7, 1);
1812 C += RunOptFunc (S, &DOptCmp8, 1);
1813 C += RunOptFunc (S, &DOptCmp9, 1);
1814 C += RunOptFunc (S, &DOptTest1, 1);
1815 C += RunOptFunc (S, &DOptLoad1, 1);
1816 C += RunOptFunc (S, &DOptJumpTarget3, 1); /* After OptCondBranches2 */
1817 C += RunOptFunc (S, &DOptUnusedLoads, 1);
1818 C += RunOptFunc (S, &DOptUnusedStores, 1);
1819 C += RunOptFunc (S, &DOptDupLoads, 1);
1820 C += RunOptFunc (S, &DOptStoreLoad, 1);
1821 C += RunOptFunc (S, &DOptTransfers1, 1);
1822 C += RunOptFunc (S, &DOptTransfers3, 1);
1823 C += RunOptFunc (S, &DOptTransfers4, 1);
1824 C += RunOptFunc (S, &DOptStore1, 1);
1825 C += RunOptFunc (S, &DOptStore5, 1);
1826 C += RunOptFunc (S, &DOptPushPop, 1);
1827 C += RunOptFunc (S, &DOptPrecalc, 1);
1833 /* Return the number of changes */
1839 static unsigned RunOptGroup4 (CodeSeg* S)
1840 /* 65C02 specific optimizations. */
1842 unsigned Changes = 0;
1844 if (CPUIsets[CPU] & CPU_ISET_65SC02) {
1845 Changes += RunOptFunc (S, &DOpt65C02BitOps, 1);
1846 Changes += RunOptFunc (S, &DOpt65C02Ind, 1);
1847 Changes += RunOptFunc (S, &DOpt65C02Stores, 1);
1849 /* The 65C02 replacement codes do often make the use of a register
1850 * value unnecessary, so if we have changes, run another load
1853 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1857 /* Return the number of changes */
1863 static unsigned RunOptGroup5 (CodeSeg* S)
1864 /* Run another round of pattern replacements. These are done late, since there
1865 * may be better replacements before.
1868 unsigned Changes = 0;
1870 /* Repeat some of the steps here */
1871 Changes += RunOptFunc (S, &DOptPush1, 1);
1872 Changes += RunOptFunc (S, &DOptPush2, 1);
1873 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1874 Changes += RunOptFunc (S, &DOptTest2, 1);
1875 Changes += RunOptFunc (S, &DOptTransfers2, 1);
1877 /* Return the number of changes */
1883 static unsigned RunOptGroup6 (CodeSeg* S)
1884 /* This one is quite special. It tries to replace "lda (sp),y" by "lda (sp,x)".
1885 * The latter is ony cycle slower, but if we're able to remove the necessary
1886 * load of the Y register, because X is zero anyway, we gain 1 cycle and
1887 * shorten the code by one (transfer) or two bytes (load). So what we do is
1888 * to replace the insns, remove unused loads, and then change back all insns
1889 * where Y is still zero (meaning that the load has not been removed).
1892 unsigned Changes = 0;
1894 /* This group will only run for a standard 6502, because the 65C02 has a
1895 * better addressing mode that covers this case.
1897 if ((CPUIsets[CPU] & CPU_ISET_65SC02) == 0) {
1898 Changes += RunOptFunc (S, &DOptIndLoads1, 1);
1899 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1900 Changes += RunOptFunc (S, &DOptIndLoads2, 1);
1903 /* Return the number of changes */
1909 static unsigned RunOptGroup7 (CodeSeg* S)
1910 /* The last group of optimization steps. Adjust branches, do size optimizations.
1913 unsigned Changes = 0;
1916 /* Optimize for size, that is replace operations by shorter ones, even
1917 * if this does hinder further optimizations (no problem since we're
1920 C = RunOptFunc (S, &DOptSize1, 1);
1923 /* Run some optimization passes again, since the size optimizations
1924 * may have opened new oportunities.
1926 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1927 Changes += RunOptFunc (S, &DOptUnusedStores, 1);
1928 Changes += RunOptFunc (S, &DOptJumpTarget1, 5);
1929 Changes += RunOptFunc (S, &DOptStore5, 1);
1932 C = RunOptFunc (S, &DOptSize2, 1);
1935 /* Run some optimization passes again, since the size optimizations
1936 * may have opened new oportunities.
1938 Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
1939 Changes += RunOptFunc (S, &DOptJumpTarget1, 5);
1940 Changes += RunOptFunc (S, &DOptStore5, 1);
1941 Changes += RunOptFunc (S, &DOptTransfers3, 1);
1944 /* Adjust branch distances */
1945 Changes += RunOptFunc (S, &DOptBranchDist, 3);
1947 /* Replace conditional branches to RTS. If we had changes, we must run dead
1948 * code elimination again, since the change may have introduced dead code.
1950 C = RunOptFunc (S, &DOptRTSJumps2, 1);
1953 Changes += RunOptFunc (S, &DOptDeadCode, 1);
1956 /* Return the number of changes */
1962 void RunOpt (CodeSeg* S)
1963 /* Run the optimizer */
1965 const char* StatFileName;
1967 /* If we shouldn't run the optimizer, bail out */
1972 /* Check if we are requested to write optimizer statistics */
1973 StatFileName = getenv ("CC65_OPTSTATS");
1975 ReadOptStats (StatFileName);
1978 /* Print the name of the function we are working on */
1980 Print (stdout, 1, "Running optimizer for function `%s'\n", S->Func->Name);
1982 Print (stdout, 1, "Running optimizer for global code segment\n");
1985 /* Run groups of optimizations */
1994 /* Write statistics */
1996 WriteOptStats (StatFileName);